Sample PGM
Sample PGM
Sample PGM
#include <stdio.h> #include <conio.h> main() { int num, sum = 0, rem; printf("Enter a number\n"); scanf("%d",&num); while( num != 0 ) { rem = num % 10; sum = sum + rem; num = num / 10; } printf("Sum of digits of entered number = %d\n",sum); getch(); return 0; }
Reverse 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
#include <math.h> void main() { float a, b, c, d, realp, imgp, r1, r2; clrscr(); printf(" Enter the 3 numbers\n "); scanf(" %f %f %f " ,&a, &b, &c); if ( a == 0 || b == 0 || c == 0 ) { printf(" Error input only non zero numbers\n "); } else { d = b * b - 4 * a * c; if ( d == 0 ) { printf(" Roots are equal\n "); r1 = r2 = - b / ( 2 * a ); printf(" Root1 = %f, Root2 = %f ", r1, r2 ); } else if(d>0) { printf( "Roots are real & distinct\n" ); r1 = ( - b + sqrt ( fabs ( d ) ) ) / ( 2 * a ); r2 = ( - b - sqrt ( fabs ( d ) ) ) / ( 2 * a ); printf(" Root1 = %f, Root2 = %f", r1, r2); } else { printf(" Roots are imaginary\n "); realp = - b / ( 2 * a ); imgp = sqrt ( fabs ( d ) ) / ( 2 * a ); printf(" Root1 = %f + i%f, Root2 = %f - i%f ",realp, imgp, realp, imgp); } } getch(); }
OUTPUT: Enter the 3 numbers 1 4 2 Roots are real & distinct Root1 = - 0.585786 , Root2 = - 3.414214
OUTPUT: Enter the Number: 22699 Largest Digit is = 9 Occurences of Largest Digit 9 is = 2
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
int arr[10]; int *a; a = arr; for ( i = 0; i < 10; i++ ) { printf ( " Enter the number %d: ", i+1 ); scanf ( " %d ", &arr[i] ); } for ( i = 0; i < 10; i++ ) { printf ( " %d--- ", *a ); total = total + *a; a = a + 1; } printf ("\nTotal = %d \n",total); } OUTPUT: Enter Enter Enter Enter Enter Enter Enter Enter Enter Enter the number 1: 1 the number 2: 2 the number 3: 3 the number 4: 4 the number 5: 5 the number 6: 6 the number 7: 7 the number 8: 8 the number 9: 9 the number 10: 10
#include <stdio.h> #include <conio.h> main() { int n, sum = 0, temp, r; printf( " Enter a number " ); scanf( " %d ", &n); temp = n; while( temp != 0 ) { r = temp % 10; sum = sum + r * r * r; temp = temp / 10; } if ( n == sum ) printf( " Entered number is an armstrong number. " ); else printf( " Entered number is not an armstrong number. " ); getch(); return 0; } OUTPUT: Enter a number6 Entered number is not an Armstrong number.
int main() { struct complex a, b, c; printf(" Enter a and b where a + ib is the first complex number. "); printf(" \na = "); scanf(" %d ", &a.real); printf(" b = "); scanf(" %d ", &a.img); printf(" Enter c and d where c + id is the second complex number. "); printf(" \nc = "); scanf(" %d ", &b.real); printf(" d = "); scanf(" %d ", &b.img); c.real = a.real + b.real; c.img = a.img + b.img; if ( c.img >= 0 ) printf(" Sum of two complex numbers = %d + %di ", c.real, c.img); else printf(" Sum of two complex numbers = %d %di ", c.real, c.img); getch(); return 0; } OUTPUT: Enter a and b where a + ib is the first complex number. a=6 b=2 Enter c and d where c + id is the second complex number. c=2 d=6 Sum of two complex numbers = 8 + 8i
} OUTPUT: Enter the number of rows in pyramid of stars you wish to see 5 * *** ***** ******* *********
Transpose A Matrix.
#include <stdio.h> #include <conio.h> int main() { int m, n, i, j; int mat[10][10], trans[10][10]; printf(" Enter the number of rows and columns of matrix "); scanf(" %d %d ", &m, &n); printf(" Enter the elements of matrix \n "); for( i = 0 ; i < m ; i++ ) { for( j = 0 ; j < n ; j++ ) { scanf(" %d ", &mat[i][j] ); } } for( i = 0 ; i < m ; i++ ) { for( j = 0 ; j < n ; j++ ) { trans[j][i] = mat[i][j]; } } printf(" Transpose of entered matrix :-\n "); for( i = 0 ; i < n ; i++ ) { for( j = 0 ; j < m ; j++ ) { printf(" %d\t ", trans[i][j] ); } printf(" \n ");
} getch(); return 0; } OUTPUT: Enter the number of rows and columns of matrix 2 2 Enter the elements of matrix 1 2 3 4 Transpose of entered matrix :1 3 2 4