Sie sind auf Seite 1von 36

I DECISION MAKING / SELECTION LOGIC Program 1 /* Swapping two numbers Example for Simple if */ #include<stdio.h> #include<conio.

o.h> void main() { int a, b, t; clrscr(); printf(Enter two Numbers:); scanf(%d%d, &a, &b); if(a>b) { t = a; a = b; b = t; } printf(The values of a = %d and b = %d, a, b); getch(); } Output: Enter two Numbers: 8 5 The values of a = 5 and b = 8 Program 2 /* Leap Year Example for if else */ #include<stdio.h> #include<conio.h> void main() { int year; clrscr(); printf(Enter a Year:); scanf(%d, &year); if(year % 4 == 0) { printf(%d is a Leap Year, year); } else { printf(%d is not a Leap Year, year); } getch(); } Output: Enter a Year: 2000 2000 is a Leap Year Program 3 /* Biggest among two numbers Example for if else */ #include<stdio.h> #include<conio.h> void main() {

int a,b; clrscr(); printf(Enter two numbers:); scanf(%d%d, &a, &b); if(a>b) printf(%d is Big, a); else printf(%d is Big, b); getch(); } Output: Enter two numbers: 20 30 30 is Big Program 4 /* Display the types of character Example for Nested if - else */ #include<stdio.h> #include<conio.h> void main() { char ch; clrscr(); printf(Enter a character:); scanf(%c, &ch); if((ch >= a && ch = z) || (ch >= A && ch < = Z)) printf(%c is an Alphabet.\n, ch); else if(ch >= 0 && ch <= 9) printf(%c is a Digit.\n, ch); else printf(%c is a Special Character, ch); getch(); } Output: Enter a character: * * is a Special Character Muthayammal Engineering College, Rasipuram - 637 408 2 Program 5 /* Arithmetic Operations (Calculator) Example for switch case */ #include<stdio.h> #include<conio.h> void main() { char ch; int a, b, ans; clrscr(); printf(Calculator:\n + : Addition\n - : Subtract \n * : Multiply\n / : Division\n % : Mod Division\n); printf(Enter the code:); scanf(%c, &ch); printf(\nEnter two values: ); scanf(%d%d, &a, &b);

switch(ch) { case + : ans = a+b; break; case - : ans = a-b; break; case * : ans = a*b; break; case / : ans = a/b; break; case % : ans = a%b; break; default: printf(\nInvalid Operator); ans = 0; } printf(\nThe Result is %d, ans); getch(); } Output: Calculator: + : Addition - : Subtract * : Multiply / : Division % : Mod Division Enter the Code: * Enter two values: 8 2 The Result is 16. Program 6 /* Prime Number Program Example for goto label */ #include<stdio.h> #include<conio.h> #include<math.h> void main() { int n, i, r; clrscr(); printf(Enter the positive integer value); scanf(%d, &n); i=2; step1: if(i<=sqrt(n)) { r=n%i; if(r==0) { printf(%d is not a prime, n);

goto end; } } else { i++; goto step1; } printf(%d is prime, n); getch(); end: printf( ); } Output: Enter the positive integer value 3 3 is prime 3 LOOPING / ITERATION / REPETITION Program 1 /* Sum of numbers upto 10 Example for while loop */ #include<stdio.h> #include<conio.h> void main() { int sum=0, i =1; clrscr(); while(i<=10) { sum = sum + i; i++; } printf(The sum of numbers upto 10 is %d, sum); getch(); } Output: The sum of numbers upto 10 is 55 Program 2 /* Reversing given number & Checking Palindrome Example for while loop */ #include<stdio.h> #include<conio.h> void main() { int rev=0, n, num, digit; clrscr(); printf(\nEnter the number: ); scanf(%d, &n); num = n; while(n!=0) { digit = n % 10; rev = rev * 10 + digit; n = n/10;

} printf(\nThe Reversed Number is %d, rev); if (rev == num) printf(\nThe Number is Palindrome); else printf(\nThe Number is not a Palindrome); getch(); } Output: Enter the number : 1221 The Reversed Number is 1221 The Number is Palindrome Program 3 /* Sum of numbers upto 10Example for dowhile loop */ #include<stdio.h> #include<conio.h> void main() { int sum=0, i =1; clrscr(); do { sum = sum + i; i++; } while(i<=10); printf(The sum of numbers upto 10 is %d, sum); getch(); } Output: The sum of numbers upto 10 is 55 Program 4 /* Sum of numbers upto 10 Example for for loop */ #include<stdio.h> #include<conio.h> void main() { int sum=0, i; clrscr(); for(i=1; i<=10; i++) { sum = sum + i; } printf(The sum of numbers upto 10 is %d, sum); getch(); } Output: The sum of numbers upto 10 is 55 Program 5 /* Print numbers upto 5 Example for break stat. */ #include<stdio.h> void main() { int i; for(i=1; i<=10; i++) { if (i==6) break;

printf(%d \t, i); } } Output: 12345 4 Program 6 /* Sum of +ve numbers Example for continue stat. */ #include<stdio.h> void main() { int i, n, sum = 0; for(i=1; i<=5; i++) { printf(\n Enter a number); scanf(%d, &n); if(n<0) continue; else sum = sum + n; } printf(\nThe Sum is %d, sum); } Output: /* Negative Numbers are neglected */ Enter a number 10 Enter a number -12 Enter a number 12 Enter a number -10 Enter a number 5 The Sum is 27 Program 7 /* Printing Number Pyramid Example for nested for loop */ #include<stdio.h> #include<conio.h> void main() { int i, j, n; clrscr(); printf(\nEnter a number : ); scanf(%d, &n); for(i=1; i<=n; i++) { for(j=i; j<=n; j++) printf(%d\t, j); printf(\n); } getch(); } Output: Enter a number : 5 12345 2345

345 45 5 Program 8 /* Generation of Fibonacci Series Example for while loop */ #include<stdio.h> #include<conio.h> void main() { int f1 = -1, f2 = 1, n, newterm = 0; clrscr(); printf(\nEnter the final term of series : ); scanf(%d, &n); while(newterm <= n) { newterm = f1 + f2; f1 = f2; f2 = newterm; printf(%d\t, newterm); } getch(); } Output: Enter the final term of series: 21 0 1 1 2 3 5 8 13 21 Program 9 /* Sum of digits of a number Example for while loop */ #include<stdio.h> #include<conio.h> void main() { int sum=0, num, rem; clrscr(); printf(\nEnter the number: ); scanf(%d, &num); while(num!=0) { rem = num % 10; sum = sum + rem; num = num/10; } printf(\nThe Sum of digits is %d, sum); getch(); } Output: Enter the number : 1221 The Sum of digits is 6 5 FUNCTIONS AND RECURSION Program 1 /* Sum of two numbers Example for global variable */ #include<stdio.h>

#include<conio.h> int a, b; void main() { void sum(void); clrscr(); printf(\nEnter two numbers: ); scanf(%d%d, &a, &b); sum(); getch(); } void sum (void) { printf(The Sum is %d, a+b); } Output: Enter two numbers : 12 21 The Sum is 33 Program 2 /* Sum of two numbers Example for Function with no arguments and no return value */ #include<stdio.h> #include<conio.h> void main() { void sum(void); clrscr(); sum(); /* Function Call */ getch(); } void sum (void) { int a, b; printf(\nEnter two numbers: ); scanf(%d%d, &a, &b); printf(The Sum is %d, a+b); } Output: Enter two numbers : 10 21 The Sum is 31 Program 3 /* Sum of two numbers Example for Function with arguments and no return value Call by value */ #include<stdio.h> #include<conio.h> void main() { void sum(int, int); int a, b; clrscr(); printf(\nEnter two numbers: ); scanf(%d%d, &a, &b); sum(a, b); /* Function Call */

getch(); } void sum (int a, int b) { printf(The Sum is %d, a+b); } Output: Enter two numbers : 10 21 The Sum is 31 Program 4 /* Sum of two numbers Example for Function with arguments and with return value Call by value */ #include<stdio.h> #include<conio.h> void main() { int sum(int, int); /*Function Prototype */ int a, b; clrscr(); printf(\nEnter two numbers: ); scanf(%d%d, &a, &b); printf(The Sum is %d, sum(a, b)); /* Call by value */ getch(); } int sum (int a, int b) { return a+b; } Output: Enter two numbers : 10 21 The Sum is 31 6 Program 5 /* Sum of two numbers Example for Function with no arguments and with return value */ #include<stdio.h> #include<conio.h> void main() { int sum(void); clrscr(); printf(The Sum is %d, sum()); /* Function Call */ getch(); } int sum (void) { int a, b; printf(\nEnter two numbers: ); scanf(%d%d, &a, &b); return a+b; } Output: Enter two numbers : 10 21

The Sum is 31 Program 6 /* Sum of two numbers Example for Function Call by Reference */ #include<stdio.h> #include<conio.h> void main() { int sum(int*, int*); /*Function Prototype */ int a, b; clrscr(); printf(\nEnter two numbers: ); scanf(%d%d, &a, &b); printf(The Sum is %d, sum(&a, &b)); getch(); } int sum (int *x, int *y) { return (*x + *y); } Output: Enter two numbers : 10 21 The Sum is 31 Program 7 /*Sum of two numbersExample for Pointer Function*/ #include<stdio.h> #include<conio.h> void main() { int* sum(int*, int*); /*Function Prototype */ int *s; clrscr(); printf(\nEnter two numbers: ); scanf(%d%d, &a, &b); s = sum(&a, &b); printf(The Sum is %d, *s); getch(); } int* sum (int *x, int *y) { int *z; z = *x + *y; return (&z); /* Returns only address not value */ } Output: Enter two numbers : 10 21 The Sum is 31 Program 8 /* Finding Factorial Example for Function */ #include<stdio.h> #include<conio.h> void main() {

int fact(int); int n; clrscr(); printf(\nEnter a number: ); scanf(%d, &n); printf(The Factorial is %d, fact(n)); getch(); } int fact (int num) { int f=1, i; for(i = num; i>0; i--) f = f * i; return f; } Output: Enter a numbers : 5 The Factorial is 120 7 Program 9 /* Finding Factorial Example for Recursive Function */ #include<stdio.h> #include<conio.h> void main() { int fact(int); int n; clrscr(); printf(\nEnter a number: ); scanf(%d, &n); printf(The Factorial is %d, fact(n)); getch(); } int fact (int num) { int f; if (num == 1) return 1; else f = num * fact(num 1); / * Recursive call */ return f; } Output: Enter a numbers : 5 The Factorial is 120 Program 10 /* Reversing String Example for Recursive Function */ #include<stdio.h> #include<conio.h> void main() { void reverse(void); clrscr();

printf(\nEnter the Text: ); reverse(); getch(); } void reverse (void) { char c; if(( c = getchar( ) ) != \n) reverse(); /* Recursive call */ purchar(c); } Output: Enter the Text : ARUNAI IANURA Program 11 /* Tower of Honoi Example for Recursive Function */ #include<stdio.h> #include<conio.h> void main() { void honoi(int, char, char, char); int n; clrscr(); printf(\nHow many disks: ); scanf(%d, &n); honoi(n, L, R, C); getch(); } void honoi (int n, char from, char to, char temp) { if(n > 0) { honoi(n-1, from, temp, to); printf(Move Disk %d from %c to %c\n, n, from, to); honoi(n-1, temp, to, from); } } Output: How many disks: 3 Move Disk 1 from L to R Move Disk 2 from L to C Move Disk 1 from R to C Move Disk 3 from L to R Move Disk 1 from C to L Move Disk 2 from C to R Move Disk 3 from L to R Program 12 /* Example for static variable & empty for loop */ #include<stdio.h> void main() { void show(void); for( ; ; )

show(); } void show (void) { int static n; printf(%d\t, ++n); if(n == 5) exit(1); } Output: 12345 8 DERIVED DATA TYPES /ARRAYS Program 1 /* Sum of numbers Example for Array */ #include<stdio.h> void main() { int sum=0, i, a[5]; printf(\nEnter 5 Numbers: ); for(i=1; i<=10; i++) { scanf(%d, &a[i]); sum = sum + a[i]; } printf(The sum of numbers is %d, sum); } Output: Enter 5 Numbers: 10 20 30 40 50 The sum of numbers is 150 Program 2 /* Printing Matrix Example for 2-dimensional Array */ #include<stdio.h> void main() { int i, j, a[3][3]; printf(\nEnter Elements for Matrix\n ); for(i=0; i<3; i++) for(j=0; j<3; j++) { printf(Enter element (%d, %d):, i, j); scanf(%d, &a[i][j]); } printf(\nThe Entered Matrix is\n ); for(i=0; i<3; i++) { for(j=0; j<3; j++) printf(%d\t, a[i][j]); printf(\n); } } Output: Enter Elements for Matrix Enter element (0,0): 2 Enter element (0,1): 4 Enter element (0,2): 1 Enter element (1,0): 3

Enter element (1,1): 6 Enter element (1,2): 3 Enter element (2,0): 2 Enter element (2,1): 5 Enter element (2,2): 0 The Entered Matrix is 241 363 250 Program 3 /* Matrix Multiplication */ #include<stdio.h> void main() { int i, j, k, a[5][5], b[5][5], c[5][5], r1, r2, c1, c2; step1: printf(\nEnter the size of Matrix A\n ); scanf(%d%d, &r1, &c1); printf(\nEnter the size of Matrix B\n); scanf(%d%d, &r2, &c2); if(c1==r2) goto step2; else printf(\nMultiplication Not possible); goto step1; step2: printf(\nEnter elements for Matrix A\n); for(i=0; i < r1; i++) for(j=0; j < c1; j++) scanf(%d, &a[i][j]); printf(\nEnter elements for Matrix B\n ); for(i=0; i < r2; i++) for(j=0; j < c2; j++) scanf(%d, &b[i][j]); for(i=0; i<r1; i++) /*Matrix Multiplication */ { for(j=0; j<c1; j++) { c[i][j] = 0; for(k=0; k<c1; k++) c[i][j] = c[i][j] + a[i][k] * b[k][j]; } } printf(\nThe resultant Matrix is\n); for(i=0; i<r1; i++) { for(j=0; j<c1; j++) printf(%d\t, c[i][j]); printf(\n); } } Output: Enter the size of Matrix A 3 3 Enter the size of Matrix B 3 3

Enter the elements for Matrix A 222222222 Enter the elements for Matrix B 333333333 The resultant Matrix is 18 18 18 18 18 18 18 18 18 9 Program 4 /* Matrix Addition */ #include<stdio.h> void main() { int i, j, a[5][5], b[5][5], c[5][5]; printf(\nEnter elements for Matrix A\n); for(i=0; i < r1; i++) for(j=0; j < c1; j++) scanf(%d, &a[i][j]); printf(\nEnter elements for Matrix B\n ); for(i=0; i < r2; i++) for(j=0; j < c2; j++) scanf(%d, &b[i][j]); for(i=0; i<r1; i++) for(j=0; j<c1; j++) c[i][j] = a[i][j] + b[i][j]; printf(\nThe resultant Matrix is\n); for(i=0; i<r1; i++) { for(j=0; j<c1; j++) printf(%d\t, c[i][j]); printf(\n); } } Output: Enter the elements for Matrix A 222222222 Enter the elements for Matrix B 333333333 The resultant Matrix is 555 555 555 Program 5 /* Sum of numbers Example for Passing Arrays to Function */ #include<stdio.h> void main() { int i, a[5], add(int [ ]); printf(\nEnter 5 Numbers: ); for(i=0; i<5; i++) scanf(%d, &a[i]); printf(\nThe Sum of Numbers is %d, add(a)); }

int add(int b[ ]) { int i, sum = 0; for(i=0; i<5; i++) sum = sum + a[i]; return(sum); } Output: Enter 5 Numbers: 10 20 30 40 50 The Sum of Numbers is 150 Program 6 /* Sorting array of numbers */ #include<stdio.h> void main() { int i, n, t, j, a[10]; printf(\nHow many Numbers: ); scanf(%d, &n); printf(\nEnter the Numbers:); for(i=0; i<n; i++) scanf(%d, &a[i]); for(i=0; i< n-1; i++) for(j=i; j<=n-1; j++) if(a[i] > a[j]) { t = a[i]; a[i] = a[j]; a[j] = t; } printf(\nThe Numbers in ascending order are :\n); for(i=0; i < n; i++) printf(%d\t, a[i]); } Output: How many Numbers: 5 Enter the Numbers: 20 1 3 44 15 The Numbers is ascending order are: 1 3 15 20 44 Program 6 /* Linear Search */ #include<stdio.h> void main() { int i, n, k, a[10], flag = 1; printf(\nHow many Numbers: ); scanf(%d, &n); printf(\nEnter the Numbers:); for(i=0; i<n; i++) scanf(%d, &a[i]); printf(\nEnter the number to search:); scanf(%d, &k) for(i=0; i< n; i++) if( k == a[i]) { printf(The element %d is at position %d, k, i+1); flag = 0; break;

} if(flag == 1) printf(\nThe Number not found in the array); } Output: How many Numbers: 5 Enter the Numbers: 20 1 3 44 15 Enter the number to search: 44 The element 44 is at position 4 10 DERIVED DATA TYPES /POINTERS Program 1 /* Print address and value of variable Simple Example for pointer */ #include<stdio.h> void main() { int a=10, *b; b = &a; printf(\nThe Value of a = %d, *b); printf(\nAddress of a = %u, b); } Output: The Value of a = 10 Address of a = 2028 Program 2 /* Swapping values of variable Simple Example for Call by Reference (Using pointers with function) */ #include<stdio.h> void main() { int a, b; void swap(int *, int *); printf(\nEnter two number); scanf(%d%d, &a, &b); printf(\nBefore Exchange a = %d, b = %d, a, b); swap(&a, &b); printf(\nAfter Exchange a = %d, b = %d, a, b); } void swap(int *x, int *y) { int t; t = *x; x = *y; y = t; } Output: Enter two number 10 23 Before Exchange a = 10, b = 23 After Exchange a = 23, b = 10 Program 3 /* Printing Array Elements Simple Example for using pointers with arrays */ #include<stdio.h> void main() { int *p, a[5] = {1, 2, 3, 4, 5};

for(p = a; p < a+5; p++) printf(%d\t, *p); } Output: 12 345 Program 4 /* Sum of N numbers Example for Using pointers with Arrays */ #include<stdio.h> void main() { int *p, sum=0, a[5]; printf(\nEnter five numbers); for(p = a; p < a+5; p++) scanf(%d, p); for(p = a; p < a+5; p++) sum = sum + *p; printf(\nThe Sum is %d, sum); } Output: Enter five numbers 10 20 30 40 50 The Sum is 150 Program 5 /* Finding Biggest Example for Function pointer (Function that returns the address not value)*/ #include<stdio.h> void main() { int *big, a, b; int *biggest(int *, int *); /* function returns address */ printf(\nEnter two numbers); scanf(%d%d, &a, &b); big = biggest(&a, &b); /* address of biggest no. is stored in big*/ printf(\nThe Biggest Number is %d, *big); } int *biggest(int *m, int *n) { if(*m > *n) return (m); /*returns address stored in m */ else return (n); /* returns address stored in n */ } Output: Enter two numbers 30 50 The Biggest Number is 50 Program 6 /* Example for Array of pointers */ #include<stdio.h> void main() { int a = 4, b = 3, c = 2, i; int *p[3]={&a, &b, &c}; printf(\nThe Values are: ); for(i=0; i < 3; i++) printf(%d\t, *p[i]); } Output: The Values are: 4 3 2 11

Program 7 /* Pointer Arithmetic Example for Operations on Pointers*/ #include<stdio.h> void main() { int *p, a[5]; p = a; printf(\nAddress of a = %u, p); printf(\nPostincrement of address = %u, p++); printf(\nPreincrement of address = %u, ++p); printf(\nPredecrement of address = %u, --p); printf(\nPostdecrement of address = %u, p--); printf(\nAddress stored in p = %u, p); } Output: Address of a = 3022 Postincrement of Address = 3022 Preincrement of Address = 3026 Predecrement of Address = 3024 Postdecrement of Address = 3024 Address stored in p = 3022 DYNAMIC MEMORY ALLOCATION Program 8 /* Addition Simple Example for Dynamic Memory Allocation */ #include<stdio.h> #include<stdlib.h> #include<conio.h> void main() { int *a, *b; clrscr(); a = (int *) malloc (sizeof(int)); b = (int *) malloc (sizeof(int)); printf(\nEnter value for a:); scanf(%d, a); printf(\nEnter value for b:); scanf(%d, b); printf(\nThe Sum is %d, *a + *b); getch(); } Output: Enter value for a: 3 Enter value for b: 8 The Sum is 11 Program 9 /* Example for memory allocation functions usage */ #include<stdio.h> #include<stdlib.h> #include<string.h> void main() {

char *p; p = (char *) malloc (7); /* allocates memory */ strcpy(p, MADRAS); printf(\nMemory Contains: %s, p); p = (char *) realloc(8); /* reallocates memory */ strcpy(p, CHENNAI); printf(\nMemory now Contains: %s, p); free(p); /* Releases the memory */ } Output: Memory Contains: MADRAS Memory now Contains: CHENNAI Program 10 /* Sum of Array elements Example for Dynamic Memory Allocation of Arrays */ #include<stdio.h> #include<stdlib.h> #include<conio.h> void main() { int *a, sum = 0, n, i; clrscr(); printf(\nHow many numbers: ); scanf(%d, &n); a = (int *) malloc (n * sizeof(int)); printf(\nEnter %d values \n, n); for( i = 0; i < n; i++) scanf(%d, a+i); for( i = 0; i < n; i++) sum = sum + *(a+i); printf(\nThe Sum is %d, sum); getch(); } Output: How many numbers: 5 Enter 5 values 53281 The Sum is 19 12 USER DEFINED DATA TYPES Program 1 /* Complex Number Addition Example for structures */ #include<stdio.h> #include<conio.h> void main() { struct complex { int real, imag; } c1, c2, c3; clrscr(); printf(\nEnter first complex number: ); scanf(%d%d, &c1.real, &c1.imag); printf(\nEnter second complex number: ); scanf(%d%d, &c2.real, &c2.imag);

c3.real = c1.real + c2.real; c3.imag = c1.imag + c2.imag; printf(\nResultant complex number is %d+%d i , c3.real, c3.imag); getch(); } Output: Enter first complex number: 3 4 Enter second complex number: 4 2 Resultant complex number is 7+ 6i Program 2 /*Student StructureExample for Array of structures*/ #include<stdio.h> void main() { struct student { char name[20]; char sex[2]; int age; } s[2]; int i; printf(\nEnter Student Details (Name, sex, age): ); for( i = 0; i<2; i++) scanf(%s%s%d, s[i].name, s[i].sex, &s[i].age); printf(\nThe Student Details are:\n); printf(\n Name\t Sex \t Age\n); for( i = 0; i<2; i++) printf(%s\t%s\t%d, s[i].name, s[i].sex, &s[i].age); } Output: Enter Student Details (Name, sex, age): Ram M 19 Geetha F 19 The Student Details are: Name Sex Age Ram M 19 Geetha F 19 Program 3 /*Student StructureExample for Array of structures*/ #include<stdio.h> void main() { struct student { char name[20]; char sex[2]; int age; } s[2]; int i; printf(\nEnter Student Details (Name, sex, age): ); for( i = 0; i<2; i++) scanf(%s%s%d, s[i].name, s[i].sex, &s[i].age); printf(\nThe Student Details are:\n); printf(\n Name\t Sex \t Age\n); for( i = 0; i<2; i++)

printf(%s\t%s\t%d, s[i].name, s[i].sex, &s[i].age); } Output: Enter Student Details (Name, sex, age): Ram M 19 Geetha F 19 The Student Details are: Name Sex Age Ram M 19 Geetha F 19 Program 4 /*Example for structures with pointers*/ #include<stdio.h> void main() { struct student { char name[20]; char sex[2]; int age; } s, *sp; sp = &s; printf(\nEnter Student Details (Name, sex, age): ); scanf(%s%s%d, sp->name, sp->sex, &sp->age); printf(\nThe Student Details are:\n); printf(\n Name\t Sex \t Age\n); printf(%s\t%s\t%d, sp->.name, sp->sex, sp->age); } Output: Enter Student Details (Name, sex, age): Ram M 19 The Student Details are: Name Sex Age Ram M 19 13 Program 5 /* Example for union */ #include<stdio.h> void main() { union num { int a; float b; } n; n.a = 5; printf(\nThe value of a is %d , n.a); n.b = 17.23; printf(\nThe value of a is %f , n.b); printf(\nThe size of n is %d, sizeof(n)); } Output: The value of a is 5 The value of a is 17.23 The size of n is 4 Program 6

/*Complex Addition Example Passing structures to function */ #include<stdio.h> #include<conio.h> void main() { typedef struct complex { int real, imag; }; complex c1, c2, c3; complex add(complex, complex); clrscr(); printf(\nEnter first complex number: ); scanf(%d%d, &c1.real, &c1.imag); printf(\nEnter second complex number: ); scanf(%d%d, &c2.real, &c2.imag); c3 = add(c1, c2); printf(\nResultant complex number is %d+%d i , c3.real, c3.imag); getch(); } complex add(complex a, complex b) { complex c; c.real = a.real + b.real; c.imag = a.imag + b.imag; return (c); } Output: Enter first complex number: 3 4 Enter second complex number: 4 2 Resultant complex number is 7+ 6i ENUMERATED DATA TYPES Program 7 /*Example for enum data typeDefaultly starts from 0*/ #include<stdio.h> void main() { enum number{Zero, One, Two, Three}; printf( ZERO = %d, Zero); printf(ONE = %d, One); printf(TWO = %d, Two); printf(THREE = %d, Three); } Output: ZERO = 0 ONE = 1 TWO = 2 THREE = 3 Program 8 /*Example for enum data typeDefaultly starts from 0 Example for switch case */ #include<stdio.h> void main() { enum country

{ INDIA = 91, AMERICA = 1, AUSTRALIA = 61, ITALY = 39 }; int code; printf(\nPlease Enter a Country Code:); scanf(%d, &code); switch(code) { case 91: printf(\nThis code is belongs to India); break; case 1: printf(\nThis code is belongs to America); break; case 61: printf(\nThis code is belongs to Australia); break; case 39: printf(\nThis code is belongs to Italy); default: printf(\nNo such code in database); } } Output 1: Please Enter a Country Code: 39 This code is belongs to Italy Output 2: Please Enter a Country Code: 121 No such code in database 14 FILE HANDLING Program 1 /* Writing text into Files Example for fprintf, fputs, fputc functions */ #include<stdio.h> void main() { FILE *fp = fopen(file1.txt, w); fprintf(fp, India is my country); fputc(fp, &); fputs(fp, I Love my country); fclose(fp); } Output: /*The file file1.txt will have the following lines*/ India is my country & I Love my country Program 2 /* Reading text from Files Example for fscanf */ #include<stdio.h> void main() { int i; /* assume file2.txt contains only 30 */ FILE* fp = fopen(file2.txt, r);

fscanf(fp, %d, &i); printf(\nThe integer in file2.txt is %d, i); fclose(fp); } Output: The integer in file2.txt is 30 Program 3 /* Program to Read and Write to a file Example for file operations*/ #include<stdio.h> void main() { /* studata is the file stored in secondary storage */ unsigned int mark; char name[20]; /* Opening file for write (append) */ FILE* fp = fopen(studata, a); printf(Enter Students name and mark:\n ); printf(Use Ctrl + Z to stop entry\n); while((scanf(%s%u, name, &mark)) != EOF) fprintf(fp, %s %u, name, mark); fclose(fp); /* Opening file for read */ fp = fopen (studata, rt); printf(\t Name \t Mark\n); printf(-------------------------); while ((fscanf(fp, %s %u, name, &mark)) != EOF) printf(%-10s %3u\n, name, mark); fclose(fp); } Output: Enter Students name and mark: Raman 98 Divya 90 ^Z Name Mark ------------------Raman 98 Divya 90 Program 4 /* Program to Read and Write records from a file Example for using structure in file and also for fwrite and fread */ #include<stdio.h> typedef struct student { unsigned int regno; char name[20]; unsigned int mark; }; void main() { /* classrec is the file contains all the records */ student rec; FILE* fp = fopen(classrec, wb); printf(Enter Students Reg.No., name and mark:\n ); printf(Use Ctrl + Z to stop entry\n); while((scanf(%u%s%u, &rec.regno, rec.name,

&rec.mark)) != EOF) fwrite(&rec, sizeof(rec), 1, fp); fclose(fp); printf(\n); fp = fopen (classrec, rb); printf(Reg.No. \t Name \t Mark\n); printf(---------------------------------------------); while ((fread(&rec, sizeof(rec), 1, fp)) printf(%5u %-10s %3u\n, rec.regno, rec.name, rec.mark); fclose(fp); } Output: Enter Students Reg.No., name and mark: 345 Raman 98 321 Vel 88 456 Divya 90 210 Arul 30 ^Z Reg.No. Name Mark -------------------------------345 Raman 98 321 Vel 88 456 Divya 90 210 Arul 30 Entered data will be stored in sturec file. These data are retrieved again for read operation 15 LINKED LIST / STACK / QUEUE Program 1 /* Linked List Operations */ #include<stdio.h> #include<stdlib.h> typedef struct node { int data; node *next; }; node *head=0; void main() { int ch; void addfirst(void); void addmid(void); void addlast(void); void delfirst(void); void delmid(void); void dellast(void); void display(void); int isempty(void); while(1) {

printf("\n Single Linked List - Menu\n"); printf("\n 1. Add at First\n 2. Add at Middle\n 3. Add at Last\n 4.Delete First\n 5.Delete Middle\n 6.Delete Last\n 7.Exit\n"); printf("\nEnter your Choice... "); scanf("%d", &ch); switch(ch) { case 1: addfirst(); display(); break; case 2: addmid(); display(); break; case 3: addlast(); display(); break; case 4: if(!isempty()) { delfirst(); display(); } else printf("\nList Empty"); break; case 5: if(!isempty()) { delmid(); display(); } else printf("\nList Empty"); break; case 6: if(!isempty()) { dellast(); display(); } else printf("\nList Empty"); break; case 7: exit(0); default: printf("\nInvalid Choice\n"); } }

} int isempty(void) { return head==NULL; } void addfirst() { node *temp; temp = (node *) malloc(sizeof(node)); printf("\nEnter the Data for the node... "); scanf("%d", &temp->data); temp->next = head; head = temp; } 16 void addmid() { int i=1, pos; node *temp, *cur=head; printf("\nEnter the position to insert..."); scanf("%d", &pos); while(pos!=i && cur!=NULL) { cur = cur->next; i++; } if(pos==i) { temp = (node *) malloc(sizeof(node)); printf("\nEnter the Data for the node..."); scanf("%d",&temp->data); temp->next = cur->next; cur->next = temp; } } void addlast() { node *temp, *cur=head->next; temp=(node *) malloc(sizeof(node)); printf("\nEnter the Data for the node..."); scanf("%d", &temp->data); while(cur->next!=NULL) { cur=cur->next; } temp->next=cur->next; cur->next = temp; } void delfirst() { node *temp; temp=head; head = head->next;

free(temp); } void delmid() { int i=1, pos; node *temp, *cur=head; printf("\nEnter the position to delete..."); scanf("%d", &pos); /*take 3*/ while(pos!=i+1 && cur->next!=NULL) { cur = cur->next; i++; } if(pos==i+1) { temp=cur->next; cur->next=temp->next; free(temp); } } void dellast() { node *temp, *cur=head->next; while(cur->next->next!=NULL) { cur=cur->next; } temp=cur->next; cur->next=NULL; free(temp); } void display() { node *cur=head->next; printf("\nhead->"); while(cur!=NULL) { printf("%d->", cur->data); cur = cur->next; } printf("NULL\n"); } 17 Program 2 /* Simple Linked List Operations */ #include<stdio.h> #include<stdlib.h> typedef struct node { int data; node *next; };

node *head=0; void main() { int ch; void add(void); void del(void); void display(void); int isempty(void); while(1) { printf("\n Single Linked List - Menu\n"); printf("\n 1. Add \n2.Delete\n 3.Exit\n"); printf("\nEnter your Choice... "); scanf("%d", &ch); switch(ch) { case 1: add(); display(); break; case 2: if(!isempty()) { del(); display(); } else printf("\nList Empty"); break; case 3: exit(0); default: printf("\nInvalid Choice\n"); } } } int isempty(void) { return head==NULL; } void add() { int i=1, pos; node *cur=head->next , *temp; temp = (node *) malloc(sizeof(node)); printf("\nEnter the Data for the node... "); scanf("%d", &temp->data); if (head == NULL) { temp->next = head; head = temp; } else

{ temp->next = cur->next; cur->next = temp; } } void del() { node *temp; temp=head; head = head->next; free(temp); } void display() { node *cur=head->next; printf("\nhead->"); while(cur!=NULL) { printf("%d->", cur->data); cur = cur->next; } printf("NULL\n"); } 18 Program 3 /* Stack Operations */ #include<stdio.h> #include<stdlib.h> typedef struct node { int data; node *next; }; node *head=0; void main() { int empty(void), ch; void push(void); void pop(void); void display(void); while(1) { printf("\n Stack - Menu\n"); printf("\n 1. Push \n 2. Pop \n 3.Exit\n"); printf("\nEnter your Choice... "); scanf("%d", &ch); switch(ch) { case 1: push(); display(); break; case 2: if(! empty()) { pop(); display(); } else

printf("\nStack Empty"); break; case 3: exit(0); default: printf("\nInvalid Choice\n"); }}} int empty(void) { return head==NULL; } void push() { node *temp; temp = (node *) malloc(sizeof(node)); printf("\nEnter the Data to push... "); scanf("%d", &temp->data); temp->next = head; head = temp; } void pop() { node *temp; temp=head; head = head->next; free(temp); } void display() { node *cur=head->next; printf("\nhead->"); while(cur!=NULL) { printf("%d->", cur->data); cur = cur->next; } printf("NULL\n"); } Program 4 /* Queue Operations */ #include<stdio.h> #include<stdlib.h> typedef struct node { int data; node *next; }; node *head=0; void main() { int empty(void), ch; void enqueue(void); void dequeue(void); void display(void); while(1) { printf("\n Queue - Menu\n"); printf("\n 1. Enqueue\n 2.Dequeue\n 3.Exit\n"); printf("\nEnter your Choice... "); scanf("%d", &ch); switch(ch) { case 1: enqueue(); display();

break; case 2: if(! empty()) { dequeue(); display(); } else printf("\nList Empty"); break; case 3: exit(0); default: printf("\nInvalid Choice\n"); }}} int empty(void) { return head==NULL; } void enqueue() { node *temp; temp = (node *) malloc(sizeof(node)); printf("\nEnter the Data for the node... "); scanf("%d", &temp->data); temp->next = head; head = temp; } void dequeue() { node *temp, *cur=head->next; while(cur->next->next!=NULL) cur=cur->next; temp=cur->next; cur->next=NULL; free(temp); } void display() { node *cur=head->next; printf("\nhead->"); while(cur!=NULL) { printf("%d->", cur->data); cur = cur->next; } 19 printf("NULL\n"); } OTHER PROGRAMS Program 1 /* Calculation of Simple Interest */ #include<stdio.h> void main() { float p, t, r, simple; printf(\nEnter Principal, time, and rate: ); scanf(%f%f%f, &p, &t, &r); simple = p * t * r /100; printf(The simple interest is: %f\n, simple); } Output: Enter principal, time and rate: 1000 2 10

The simple interest is: 200.000000 Program 2 /* Temperature Conversion */ #include<stdio.h> void main() { float c, f; printf(\nEnter temperature in Celsius: ); scanf(%f, &c); f = 1.8 * c + 32; printf(\nEquivalent Fahrenheit = %f\n, f); printf(\nEnter temperature in Fahrenheit: ); scanf(%f, &f); c = (f-32) /1.8; printf(Equivalent Celsius = %f\n, c); } Output: Enter temperature in Celsius: 10 Equivalent Fahrenheit = 50.000000 Enter temperature in Fahrenheit: 50 Equivalent Celsius = 10.000000 Program 3 /* Area of a triangle Given three sides */ #include<stdio.h> void main() { float a, b, c, s, area; printf(\nEnter the 3 sides: ); scanf(%f%f%f, &a, &b, &c); s = (a+b+c)/2; area = sqrt(s*(s-a)*(s-b)*(s-c)); printf(\nArea of the triangle is %f\n, area); } Output: Enter the 3 sides: 4 5 6 Area of the triangle is 9.921567 Program 4 /* Area of triangle Given base and height */ #include<stdio.h> void main() { float b, h, area; printf(\nEnter the base and height: ); scanf(%f%f, &b, &h); area = b * h / 2 printf(Area of the triangle is %f\n, area); } Output: Enter the base and height: 3 2 Area of the triangle is 3.000000 Program 5 /* Program to print Pascal Triangle */ #include<stdio.h> void main() {

int binom=1, p, q=0, r, x; printf(Input number of rows: ); scanf(%d, &p); printf(Pascal Triangle: \n); while(q < p) { for( r=40 3 * q; r > 0; r--) printf( ); for( x = 0; x <= q; x++) { if((x==0 ) || (q==0)) binom = 1; else binom = (binom * (q-x+1)) / x; printf(%6d, binom); } printf( \n); q++; } } Output: Input number of rows: 6 Pascal Triangle: 1 1 1 1 2 1 1 3 3 1 20 1 4 6 4 1 1 5 10 10 5 1 Program 6 /* Roots of a quadratic equation */ #include<stdio.h> #include<math.h> void main() { float a, b, c, real, num, imag, root1, root2, disc; int k; printf(\nEnter value for a, b and c ); scanf(%f%f%f, &a, &b, &c); if(a != 0) { disc = b * b 4 * a * c; printf(Discriminant = %5.2f\n, disc); if(disc < 0) k = 1; else if (disc == 0) k = 2; else if (disc > 0) k = 3; switch(k) { case 1: printf(Roots are imaginary\n); real = -b / (2*a); disc = -disc;

num = pow((double) disc, (double) 0.5); imag = num/ (2*a); printf(Root 1 = %5.2f + j%5.2f\n, real, imag); printf(Root 2 = %5.2f j%5.2f\n, real, imag); break; case 2: printf(Roots are real and equal\n); root1 = -b/(2*a); printf(Root1= Root2 = %7.2f\n, root1); break; case 3: printf(Roots are real and unequal\n); root1 = (-b + sqrt((double) disc))/(2*a); root2 = (-b sqrt((double)disc))/(2*a); printf(Root 1=%7.2f Root2=%7.2f\n, root1, root2); break; } } else printf(Equation is linear\n); } Output 1: Enter value for a, b and c 1 2 7 Discriminant = -24.00 Roots are imaginary Root 1 = -1.00 + j 2.45 Root 2 = -1.00 j 2.45 Output 2: Enter value for a, b and c 1 2 1 Discriminant = 0.00 Roots are real and equal Root 1 = Root = -1.00

Das könnte Ihnen auch gefallen