Sample C Program To Swap Two Numbers Using Temporary Variables
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
printf("a = %d\nb = %d\n",a,b); return 0; } OUTPUT: Enter two numbers to swap 2 4 a=4 b=2
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
c is greatest.
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
#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
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