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

#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; } #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 Add Two Numbers Using Pointers. #include <stdio.h> #include <conio.h> int main() { int x, y, *p, *q, sum; printf(" Enter two integers to add " ); scanf(" %d %d ", &x, &y ); p = &x; q = &y; sum = *p + *q; printf(" Sum of entered numbers = %d\n ", sum ); getch(); return 0; } OUTPUT: Enter two integers to add 3 6 Sum of entered numbers = 9 Sample C Program To Print 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 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 <stdio.h> #include <conio.h> #include <malloc.h> #include <string.h> 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 ); 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 Multiply Two Matrices. #include <stdio.h> #include <conio.h> int main() { int m, n, p, q, i, j, k, sum = 0; int first[10][10], second[10][10], mul[10][10]; printf(" Enter the number of rows and columns of first matrix " ); scanf(" %d %d ", &m, &n ); printf(" Enter the elements of first matrix\n " ); for ( i = 0 ; i < m ; i++ ) for ( j = 0 ; j < n ; j++ ) scanf(" %d ", &first[i][j] ); printf(" Enter the number of rows and columns of first matrix " ); scanf(" %d %d ", &p, &q ); if ( n != p ) printf(" Matrices with entered orders can't be multiplied with each other." ); else { printf(" Enter the elements of second matrix\n " ); for ( i = 0 ; i < p ; i++ ) for ( j = 0 ; j < q ; j++ )

scanf(" %d ", &second[i][j] ); for ( i = 0 ; i < m ; i++ ) { for ( j = 0 ; j < n ; j++ ) { for ( k = 0 ; k < p ; k++ ) { sum = sum + first[ i ] [ k ] * second[ k ] [ j ]; } mul[ i ] [ j ] = sum; sum = 0; } } printf(" Product of entered matrices:-\n " ); for ( i = 0 ; i < m ; i++ ) { for ( j = 0 ; j < q ; j++ ) printf(" %d \t ", mul[ i ] [ j ] ); printf("\n"); } } getch(); return 0; } OUTPUT: Enter the number of rows and columns of first matrix 2 2 Enter the elements of first matrix 1 5 6 4 Enter the number of rows and columns of second matrix 2 2 Enter the elements of second matrix 1 2 4 6 Product of entered matrices:21 32 22 36 Sample C Program To Add Two Matrices.

#include <stdio.h> #include <conio.h> int main() { int m, n, i, j; int first[10][10], second[10][10], sum[10][10]; printf(" Enter the number of rows and columns of matrix "); scanf(" %d %d ", &m, &n); printf(" Enter the elements of first matrix\n "); for ( i = 0 ; i < m ; i++ ) for ( j = 0 ; j < n ; j++ ) scanf(" %d ", &first[i][j] ); printf("Enter the elements of second matrix\n"); for ( i = 0 ; i < m ; i++ ) for ( j = 0 ; j < n ; j++ ) scanf(" %d ", &second[i][j] ); for ( i = 0 ; i < m ; i++ ) for ( j = 0 ; j < n ; j++ ) sum[i][j] = first[i][j] + second[i][j]; printf(" Sum of entered matrices:-\n "); for ( i = 0 ; i < m ; i++ ) { for ( j = 0 ; j < n ; j++ ) printf(" %d\t ", sum[i][j] ); printf(" \n "); } getch(); return 0; } OUTPUT: Enter the number of rows and columns of matrix 3 3 Enter the elements of first matrix 1 2 3 4 5 6 7 8 9 Enter the elements of second matrix 9 8

7 6 5 4 3 2 1 Sum of entered matrices:10 10 10 10 10 10 10 10 10 Sample C Program 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 #include <stdio.h> #include <conio.h> void main() { int n, i, sum = 0; clrscr(); printf(" Enter any number: " ); scanf(" %d ", &n); for(i = 1; i<n; i = i + 2 ) { printf(" %d + ", i); sum = sum + i; } printf(" %d ", n); printf(" \nSum = %d ", sum + n ); getch(); } OUTPUT: Enter any number: 9 1+3+5+7+9 Sum = 25 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 * *** ***** ******* ********* #include <stdio.h> #include <conio.h> void main() { int n; clrscr(); printf(" Enter any year: "); scanf(" %d ", &n); if( n % 4 == 0 ) printf(" Year is a leap year "); else printf(" Year is not a leap year "); getch(); } OUTPUT: Enter any year: 2004 Year is a leap year Sample C Program 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: * ** *** **** ***** Sample C Program To Find The Square Of A Number. #include <stdio.h> #include <conio.h> void main() { int rev( int ); int r, a; clrscr(); printf(" Enter any number: " ); scanf(" %d ", &a); r = rev( a ); printf(" Square is : %d ", r); getch(); } int rev(int x) { return( x * x ); } OUTPUT: Enter any number: 25 Square is : 625

Sample C Program To Find Gross Salary. #include <stdio.h> #include <conio.h> void main() { int gs, bs, da, ta; clrscr(); printf(" Enter basic salary: "); scanf("%d",&bs); da = ( 10 * bs ) / 100; ta = ( 12 * bs ) / 100; gs = bs + da + ta; printf(" Gross salary = %d ", gs); getch(); } OUTPUT: Enter basic salary: 1000 Gross salary = 1220

Sample C Program On Use Of Array & Pointer Concept Together.

#include <stdio.h> #include <conio.h> #define S 5 main() { int arr[S], i; int *ptr = arr; printf(" Enter %d elements of array \n ", S); for( i = 0; i < S; i++ ) scanf(" %d ", ptr + i ); printf(" Array elements are \n " ); for( i = 0; i < S; i++ ) printf(" %d %d %d %d \n ", *(ptr + i), *(i + ptr),ptr[i],i[ptr] );

return 0; } OUTPUT: Enter 5 elements of array 1 2 4 7 5 Array elements are 1111 2222 4444 7777 5555

Sample C Program 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 the number 1: 1 Enter the number 2: 2 Enter the number 3: 3

Enter the number 4: 4 Enter the number 5: 5 Enter the number 6: 6 Enter the number 7: 7 Enter the number 8: 8 Enter the number 9: 9 Enter the number 10: 10 1---2---3---4---5---6---7---8---9---10--Total = 55 Sample C Program On Strings Into Array Of Pointers #include <stdio.h> #include <conio.h> #include <alloc.h> #include <string.h> #define S 4 main() { char*arr_str[S]; char temp[20]; int i, n, len; clrscr(); for( i = 0; i < S; i++ ) { printf(" Enter a string %d\n ", i + 1); gets( temp ); len = strlen( temp ); arr_str[i] = (char*)malloc(len); strcpy(arr_str[i], temp); } printf(" The entered strings are\n "); for(i = 0; i < S; i++) printf(" %s\n ", arr_str[i]); getch(); return 0; } OUTPUT: Enter a string 1 Do or Die Enter a string 2 Love is life Enter a string 3

Live and let live Enter a string 4 Work is worship The entered strings are Do or Die Love is life Live and let live Work is worship 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 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 Convert Celsius Temperature To Fahrenheit. #include <stdio.h> #include <conio.h> void main() { float c, f; clrscr(); printf(" Enter temp in centigrade: "); scanf("%f",&c); f = ( 1.8 * c ) + 32; printf(" Temperature in Fahrenheit = %f", f); getch(); } OUTPUT: Enter temperature in centigrade: 25 Temperature in Fahrenheit = 77.000000 Sample C Program To Check Whether A Number Is Even Or Odd. #include <stdio.h> #include <conio.h> void main() {

int n; clrscr(); printf(" Enter any number: "); scanf(" %d ",&n); if( n % 2 == 0) printf(" Number is even "); else printf(" Number is odd "); getch(); } OUTPUT: Enter any number: 6 Number is even Sample C Program To Specify Size Of Commonly Used Data Types. #include <stdio.h> #include <conio.h> main () { clrscr(); printf ("\n An int is %d bytes", sizeof (int)); printf ("\n A char is %d bytes", sizeof (char)); printf ("\n A short is %d bytes", sizeof (short)); printf ("\n A long is %d bytes", sizeof (long)); printf ("\n A float is %d bytes", sizeof (float)); printf ("\n A double is %d bytes", sizeof (double)); printf ("\n An unsigned char is %d bytes", sizeof (unsigned char)); printf ("\n An unsigned int is %d bytes", sizeof (unsigned int)); return 0; } OUTPUT: An int is 2 bytes A char is 1 bytes A short is 2 bytes A long is 4 bytes A float is 4 bytes A double is 8 bytes An unsigned char is 1 bytes An unsigned int is 2 bytes Sample C Program To Input & Multiply Two Numbers.

#include <stdio.h> #include <conio.h> void main() { clrscr(); int a, b, c; printf(" Enter the numbers to be multiplied: "); scanf(" %d %d ", &a, &b); c = a * b; printf(" The product of the numbers is: %d ",c); getch(); } OUTPUT: Enter the numbers to be multiplied: 10 5 The product of the numbers is: 50

Sample C Program 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

Sample C Program 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 Sample C Program 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

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 Find The Area Of A Rectangle. #include <stdio.h> #include <conio.h> #include <math.h> float area_rect( float, float ); main() { clrscr(); float l, b, area; printf( " \nEnter the length and breadth of the rectangle " ); scanf( " %f %f ", &l, &b ); area = area_rect ( l , b ); printf( " \nArea of rectangle is : %f square units. ", area ); return 0; }

float area_rect ( float l, float b ) { float area; area = l * b; return( area ); } OUTPUT: Enter the length and breadth of the rectangle 6 4 Area of rectangle is : 24.000000 square units. Sample C Program To Find The Sum Of N Numbers. #include <stdio.h> #include <conio.h> main() { int n, sum = 0, c, var; printf( " Enter the number of integers you want to add\n " ); scanf( " %d ", &n); printf( " Enter %d numbers\n ", n ); for ( c = 1 ; c <= n ; c++ ) { scanf( " %d ", &var ); sum = sum + var; } printf( " Sum of entered numbers = %d\n ", sum ); getch(); return 0; }

OUTPUT: Enter the number of integers you want to add: 3 Enter 3 numbers 1 2 3 Sum of entered numbers = 6

Sample C Program 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 the number 1: 1 Enter the number 2: 2 Enter the number 3: 3 Enter the number 4: 4 Enter the number 5: 5 Enter the number 6: 6

Enter the number 7: 7 Enter the number 8: 8 Enter the number 9: 9 Enter the number 10: 10 1---2---3---4---5---6---7---8---9---10--Total = 55 Sample C Program To Check Whether A Number Is Armstrong Or Not. #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.

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