Sei sulla pagina 1di 8

Sum of digits

#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; }

Print system date


#include <stdio.h> #include <conio.h> #include <dos.h> int main() { struct date d; getdate(&d); printf("Current system date is %d / %d / %d", d.da_day, d.da_mon, d.da_year); getch(); return 0; } OUTPUT: Current system date is 26/7/2011

Prime number or not


#include <stdio.h> #include <conio.h> void main() { int i, n ,r=0; clrscr(); printf(" Enter any no: "); scanf("%d",&n); for( i = 2; i <= n - 1; i++ ) { if( n % i == 0) r = 1; break; } if( r == 0) printf(" Is prime "); else printf(" Not prime "); getch(); }

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

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

To Count The Number Of Times A Digit Occurs In A Number Array


#include <stdio.h> #include <conio.h> void main( ) { int a[50], i, n, s, t=0; clrscr( ); printf("\n\n Enter the number of elements you want to enter "); scanf(" %d ",&n); printf("\n\n Enter the numbers you desire "); for( i = 0; i < n; i++ ) scanf(" %d ", &a[i]); printf(" Enter the element to be counted "); scanf(" %d ", &s); for( i = 0; i < n; i++ ) { if( s == a[i] ) t++; } printf(" The number you entered has occurred %d number of times ", t); getch( ); } OUTPUT: Enter the number of elements you want to enter: 3 Enter the numbers you desire: 6 5 6 Enter the element to be counted: 6 The number you entered has occurred 2 number of times

To Find The Roots Of A Quadratic Equation.


#include <stdio.h> #include <conio.h>

#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

To Count The Number Of Times The Largest Digit Occurs In A Number


#include <stdio.h> #include <conio.h> void main() { long int n, r, m, max = 0, count = 0; clrscr(); printf( "Enter the Number:" ); scanf(" %ld ",&n); m = n; while( n > 0 ) { r = n % 10; if( r > max ) max = r; n = n / 10; } printf( " \nLargest Digit is = %ld ", max); while( m > 0) { r = m % 10; if( r == max ) count++; m = m / 10; } printf( "\n\nOccurences of Largest Digit %ld is = %ld", max, count); getch(); }

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

To Accept & Add Ten Numbers Using Pointers.


#include <stdio.h> #include <conio.h> void main () { int i, total;

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

1---2---3---4---5---6---7---8---9---10--Total = 55 Armstrong number:


If the sum of cube of digits of a number form the same no-> it is Armstrong no eg. 153 Cube(1) +Cube (5)+ Cube (3)= 1+125+27=153

#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.

To Add Complex Numbers.


#include <stdio.h> #include <conio.h> #include <stdlib.h> struct complex { int real; int img; };

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

To Display A Sequence Using Stars.


#include <stdio.h> #include <conio.h> void main() { int i, j; clrscr(); for( i = 1; i<=5; i++) { for( j = 1; j<=i; j++) printf(" * "); printf(" \n "); } getch(); } OUTPUT: * ** *** **** *****

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 * *** ***** ******* *********

Floyds Triangle. Floyds Triangle.


#include <stdio.h> #include <conio.h> main() { int n, i, c, number = 1; printf(" Enter the number of rows of Floyd's triangle you want " ); scanf(" %d ", &n ); for ( i = 1 ; i <= n ; i++ ) { for ( c = 1 ; c <= i ; c++ ) { printf(" %d ", number ); number++; } printf(" \n " ); } getch(); return 0; } OUTPUT: Enter the number of rows of Floyds triangle you want 6 1 23 456 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21

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

Potrebbero piacerti anche