Вы находитесь на странице: 1из 9

Sample C Program To Swap Two Numbers Using Temporary Variables.

#include <stdio.h> #include <conio.h> main() { int x, y, temp; printf("Enter the value of x and y "); scanf("%d %d", &x, &y); printf("Before Swapping\nx = %d\ny = %d\n",x,y); temp = x; x = y; y = temp; printf("After Swapping\nx = %d\ny = %d\n",x,y); getch(); return 0; } OUTPUT: Enter the value of x and y 2 4 Before Swapping x=2 y=4 After Swapping x=4 y=2

Sample C Program To Swap Two Numbers Without Using Temp Variable.


#include <stdio.h> main() { int a, b; printf("Enter two numbers to swap "); scanf("%d %d", &a, &b); a = a + b; b = a - b; a = a - b;

printf("a = %d\nb = %d\n",a,b); return 0; } OUTPUT: Enter two numbers to swap 2 4 a=4 b=2

Sample C Program To Find Factorial Of A Number.


#include <stdio.h> #include <conio.h> void main() { int n, i, fact=1; clrscr(); printf(" Enter any no: "); scanf("%d", &n); for( i = n; i >= 1; i-- ) { fact = fact * i; } printf(" Factorial =%d",fact); getch(); } OUTPUT: Enter any no: 5 Factorial = 120

Sample C Program To Print Fibonacci Series Upto 100.


#include <stdio.h> #include <conio.h> void main() { int a = 1, b = 1, c = 0, i; clrscr(); printf(" %d\t %d\t ", a, b);

for( i = 0; i <= 10; i++) { c = a + b; if(c < 100) { printf("%d\t",c); } a = b; b = c; } getch(); } OUTPUT: 1 1 2 3 5 8 13 21 34 55 89

Sample C Program To Find The Greatest Among Three Numbers.


#include <stdio.h> #include <conio.h> void main() { int a, b, c; clrscr(); printf(" Enter value of a, b & c: "); scanf("%d %d %d",&a, &b, &c); if( ( a>b ) && ( a>c ) ) printf(" a is greatest."); if( ( b>c ) && ( b>a ) ) printf(" b is greatest."); if( ( c>a ) && ( c>b )) printf(" c is greatest."); getch(); } OUTPUT: Enter value of a, b & c: 2 5 10

c is greatest.

Sample C Program To Find The Length Of A String.


#include <stdio.h> #include <conio.h> #include <string.h> main() { char a[100]; int length; printf(" Enter a string to calculate it's length "); gets(a); length = strlen(a); printf(" Length of entered string is = %d\n",length); getch(); return 0; } OUTPUT: Enter a string to calculate it's length: noon Length of entered string is = 4

Sample C Program To Print Number Of Vowels, Consonants, Characters, Words & Spaces In A Line Of Text.
#include <stdio.h> #include <conio.h> #include <ctype.h> main() { clrscr(); char line[80], c; int i, vow, cons, dig, word, whites, other; i = 0; vow = 0; cons = 0; dig = 0; word = 0; whites = 0; other = 0;

printf ( " Enter a line of text: \n" ); scanf ( " % [ ^ \n ] ", line); while ( ( c = tolower ( line [ i++ ] ) ) ! = '\0' ) { if ( c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u' ) ++vow; else if ( c >= 'a' && c <= 'z' ) ++cons; else if ( c >= '0' && c <= '9' ) ++dig; else if ( c == ' ' ) { ++word; ++whites; while ( ( line[i] == ' ' || line[i] == '\t' ) ) { i++; whites++; } } else ++other; } ++word; printf ( " \n\n Total number of :\n " ); printf( " Vowels = %d\n ", vow ); printf( " Consonants = %d\n ", cons ); printf( " Numeric digits = %d\n ", dig ); printf( " Other characters = %d\n ", other ); printf( " Words = %d\n ", word ); printf( " White spaces = %d\n ", whites ); return 0; } OUTPUT: Enter a line of text: Thank you Total number of : Vowels = 3 Consonants = 5 Numeric digits = 0 Other characters = 0

Words = 2 White spaces = 1

Sample C Program To Print A Star Pyramid.


#include <stdio.h> #include <conio.h> void main() { int row, c, n, temp; printf(" Enter the number of rows in pyramid of stars you wish to see: "); scanf(" %d ", &n); temp = n; for ( row = 1 ; row <= n ; row++ ) { for ( c = 1 ; c < temp ; c++ ) printf(" "); temp--; for ( c = 1 ; c <= 2 * row - 1 ; c++ ) printf(" * "); printf(" \n "); } getch(); return 0; } OUTPUT: Enter the number of rows in pyramid of stars you wish to see 5 * *** ***** ******* *********

Sample C Program To Concatenate Two Strings.

#include <stdio.h> #include <conio.h> #include <string.h> int main() { char a[100], b[100]; printf(" Enter the first string " ); gets(a); printf(" Enter the second string " ); gets(b); strcat( a, b ); printf(" String obtained on concatenation is %s\n ", a ); getch(); return 0; } OUTPUT: Enter the first string Team Enter the second string mates String obtained on concatenation is Teammates

Sample C Program To Swap Two Strings.


#include #include #include #include main() { char first[100], second[100], *temp; printf(" Enter the first string " ); gets(first); printf(" Enter the second string " ); gets(second); printf(" \nBefore Swapping\n " ); printf(" First string: %s\n ", first ); printf(" Second string: %s\n\n ", second ); <stdio.h> <conio.h> <malloc.h> <string.h>

temp = ( char* )malloc( 100 ); strcpy( temp, first ); strcpy( first, second ); strcpy( second, temp ); printf(" After Swapping\n " ); printf(" First string: %s\n ", first ); printf(" Second string: %s\n ", second ); getch(); return 0; } OUTPUT: Enter the first string little Enter the second string flower Before Swapping First string: little Second string: flower After Swapping First string: flower Second string: little

Sample C Program To Find The Reverse Of A Number.


#include <stdio.h> #include <conio.h> void main() { int n, a, r=0; clrscr(); printf(" Enter any number to get its reverse: "); scanf("%d",&n); while(n>=1) { a = n % 10; r = r * 10 + a; n = n / 10; } printf(" Reverse = %d",r); getch(); }

OUTPUT: Enter any number to get its reverse: 65 Reverse = 56

Sample C Program To Check Whether A String Is Palindrome Or Not.


#include <stdio.h> #include <conio.h> #include <string.h> int main() { char a[100], b[100]; printf(" Enter the string to check if it is a palindrome "); gets(a); strcpy(b , a); strrev(b); if (strcmp(a , b) == 0 ) printf(" Entered string is a palindrome.\n"); else printf(" Entered string is not a pailndrome .\n"); getch(); return 0; } OUTPUT: Enter the string to check if it is a palindrome: noon Entered string is a palindrome

Вам также может понравиться