Important C Programs DECISION MAKING / S / SELECTION LOGIC Program – 1
/* Swapping two numbers – Example for Simple if */ #include
#include 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(); }
Program – 3
/* Biggest among two numbers – Example for if – else */ #include #include 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
Output: Enter two Numbers: 8 5 The values of a = 5 and b = 8 Program – 2
/* Leap Year – Example for if – else */ #include #include 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 – 4
/* Display the types of character – Example for Nested if - else */ #include #include 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
Roy Antony Arnold G
Digitally signed by Roy Antony Arnold G DN: cn=Roy Antony Arnold G, o=Photon Soft Solutions, ou, [email protected], c=IN Date: 2009.08.11 19:33:05 -08'00'
P a n i m a l a r E n g i n e e r i n g Co l l e g e
MCA Dept.
Program – 5
C
Programs
Program – 6
/* Arithmetic Operations (Calculator) – Example for switch – case */ #include #include 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(); }
/* Prime Number Program – Example for goto label */ #include #include #include 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
Output: Calculator: + : Addition - : Subtract * : Multiply / : Division % : Mod Division Enter the Code: * Enter two values: 8 2 The Result is 16.
2
Compiled by – GRAA
P a n i m a l a r E n g i n e e r i n g Co l l e g e
MCA Dept.
C
Programs
LOOPING / I / ITERATION / R / REPETITION Program – 1
Program – 3
/* Sum of numbers upto 10 – Example for while loop */ #include #include 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(); }
/* Sum of numbers upto 10–Example for do–while loop */ #include #include 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
Output: The sum of numbers upto 10 is 55
Program – 4
/* Sum of numbers upto 10 – Example for ‘for’ loop */ #include #include 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 – 2
/* Reversing given number & Checking Palindrome – Example for while loop */ #include #include 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(); }
Program – 5
/* Print numbers upto 5 – Example for break stat. */ #include void main() { int i; for(i=1; i<=10; i++) { if (i==6) break; printf(“%d \t”, i); } } Output: 1 2 3 4 5
Output: Enter the number : 1221 The Reversed Number is 1221 The Number is Palindrome 3
Compiled by – GRAA
P a n i m a l a r E n g i n e e r i n g Co l l e g e
MCA Dept.
Program – 6
C
Programs
Program – 8
/* Sum of +ve numbers – Example for continue stat. */ #include 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); }
/* Generation of Fibonacci Series – Example for while loop */ #include #include 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: /* 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
Output: Enter the final term of series: 21 0 1 1 2 3 5 8 13 21 Program – 9
Program – 7
/* Sum of digits of a number – Example for while loop */ #include #include 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(); }
/* Printing Number Pyramid – Example for nested for loop */ #include #include 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 the number : 1221 The Sum of digits is 6
} Output: Enter a number : 5 1 2 3 4 5 2 3 4 5 3 4 5 4 5 5
4
Compiled by – GRAA
MCA Dept.
P a n i m a l a r E n g i n e e r i n g Co l l e g e
C
Programs
FUNCTIONS AND RECURSION Program – 1
Program – 3
/* Sum of two numbers – Example for global variable */ #include #include int a, b; void main() { void sum(void); clrscr(); printf(“\nEnter two numbers: “); scanf(“%d%d”, &a, &b); sum(); getch(); }
/* Sum of two numbers – Example for Function with arguments and no return value – Call by value */ #include #include 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 (void) { printf(“The Sum is %d”, a+b); }
void sum (int a, int b) { printf(“The Sum is %d”, a+b); }
Output: Enter two numbers : 12 21 The Sum is 33
Output: Enter two numbers : 10 21 The Sum is 31
Program – 2 Program – 4
/* Sum of two numbers – Example for Function with no arguments and no return value */ #include #include void main() { void sum(void); clrscr(); sum(); /* Function Call */ getch(); }
/* Sum of two numbers – Example for Function with arguments and with return value – Call by value */ #include #include 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, sum(a, b)); /* Call by value */ getch(); }
void sum (void) { int a, b; printf(“\nEnter two numbers: “); scanf(“%d%d”, &a, &b); printf(“The Sum is %d”, a+b); }
int sum (int a, int b) { return a+b; }
Output: Enter two numbers : 10 21 The Sum is 31
Output: Enter two numbers : 10 21 The Sum is 31
5
Compiled by – GRAA
P a n i m a l a r E n g i n e e r i n g Co l l e g e
MCA Dept.
Program – 5
C
Programs
Program – 7
/* Sum of two numbers – Example for Function with no arguments and with return value */ #include #include void main() { int sum(void); clrscr(); printf(“The Sum is %d”, sum()); sum()); /* Function Call */ getch(); }
/*Sum of two numbers–Example for Pointer Function*/ #include #include 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 (void) { int a, b; printf(“\nEnter two numbers: “); scanf(“%d%d”, &a, &b); return a+b; }
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
Output: Enter two numbers : 10 21 The Sum is 31
Program – 6
Program – 8
/* Sum of two numbers – Example for Function Call by Reference */ #include #include 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(); }
/* Finding Factorial – Example for Function */ #include #include void main() { int fact(int); int n; clrscr(); printf(“\nEnter a number: “); scanf(“%d”, &n); printf(“The Factorial is %d”, fact(n)); getch(); }
int sum (int *x, int *y) { return (*x + *y); }
int fact (int num) { int f=1, i; for(i = num; i>0; i--) f = f * i; return f; }
Output: Enter two numbers : 10 21 The Sum is 31
Output: Enter a numbers : 5 The Factorial is 120
6
Compiled by – GRAA
P a n i m a l a r E n g i n e e r i n g Co l l e g e
MCA Dept.
C
Program – 9
Programs
Program – 11
/* Finding Factorial – Example for Recursive Function */ #include #include void main() { int fact(int); int n; clrscr(); printf(“\nEnter a number: “); scanf(“%d”, &n); printf(“The Factorial is %d”, fact(n)); getch(); }
/* Tower of Honoi – Example for Recursive Function */ #include #include void main() { void honoi(int, char, char, char); int n; clrscr(); printf(“\nHow many disks: “); scanf(“%d”, &n); honoi(n, ‘L’, ‘R’, ‘C’); getch(); }
int fact (int num) { int f; if (num == 1) return 1; else f = num * fact(num – 1); / * Recursive call */ return f; }
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: Enter a numbers : 5 The Factorial is 120
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 – 10
/* Reversing String – Example for Recursive Function */ #include #include void main() { void reverse(void); clrscr(); printf(“\nEnter the Text: “); reverse(); getch(); }
Program – 12
/* Example for static variable & empty for loop */ #include void main() { void show(void); for( ; ; ) show(); }
void reverse (void) { char c; if(( c = getchar( ) ) != ‘\n’) reverse(); /* Recursive call */ purchar(c); }
void show (void) { int static n; printf(“%d\t”, ++n); if(n == 5) exit(1); }
Output: Enter the Text : ARUNAI IANURA
Output: 1 2 3
7
4
5
Compiled by – GRAA
P a n i m a l a r E n g i n e e r i n g Co l l e g e
MCA Dept.
C
Programs
DERIVED DATA TYPES / ARRAYS Program – 1
Program – 3
/* Sum of numbers – Example for Array */ #include 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
/* Matrix Multiplication */ #include void main() { int i, j, k, a[5][5], b[5][5], c[5][5], r1, r2, c1, 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
Program – 2
/* Printing Matrix – Example for 2-dimensional Array */ #include 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 2 4 1 3 6 3 2 5 0
8
Compiled by – GRAA
P a n i m a l a r E n g i n e e r i n g Co l l e g e
MCA Dept.
Program – 4
C
Programs
Program – 6
/* Matrix Addition */ #include 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
/* Sorting array of numbers */ #include void main() { int i, n, t, j, a[10]; printf(“\nHow many Numbers: “); scanf(“%d”, &n); printf(“\nEnter the Numbers:”); for(i=0; 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 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
Program – 5
/* Sum of numbers – Example for Passing Arrays to Function */ #include 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: 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
Output: Enter 5 Numbers: 10 20 30 40 50 The Sum of Numbers is 150
9
Compiled by – GRAA
P a n i m a l a r E n g i n e e r i n g Co l l e g e
MCA Dept.
C
Programs
DERIVED DATA TYPES /POINTERS Program – 1
Program – 4
/* Print address and value of variable – Simple Example for pointer */ #include void main() { int a=10, *b; b = &a; printf(“\nThe Value of a = %d”, *b); printf(“\nAddress of a = %u”, b); }
/* Sum of N numbers – Example for Using pointers with Arrays */ #include 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: The Value of a = 10 Address of a = 2028
Output: Enter five numbers 10 20 30 40 50 The Sum is 150
Program – 2
/* Swapping values of variable – Simple Example for Call by Reference (Using pointers with function) */ #include 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; }
Program – 5
/* Finding Biggest – Example for Function pointer (Function that returns the address not value)*/ #include 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 number 10 23 Before Exchange a = 10, b = 23 After Exchange a = 23, b = 10
Output: Enter two numbers 30 50 The Biggest Number is 50 Program – 6
Program – 3
/* Example for Array of pointers */ #include 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
/* Printing Array Elements– Simple Example for using pointers with arrays */ #include void main() { int *p, a[5] = {1, 2, 3, 4, 5}; for(p = a; p < a+5; p++) printf(“%d\t”, *p); }
Output: 1 2 3
4
5
10
Compiled by – GRAA
P a n i m a l a r E n g i n e e r i n g Co l l e g e
MCA Dept.
Program – 7
C
Programs
Program – 9
/* Pointer Arithmetic – Example for Operations on Pointers*/ #include 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); }
/* Example for memory allocation functions usage */ #include #include #include 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: 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
Output: Memory Contains: MADRAS Memory now Contains: CHENNAI Program – 10
/* Sum of Array elements – Example for Dynamic Memory Allocation of Arrays */ #include #include #include 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(); }
DYNAMIC MEMORY ALLOCATION Program – 8
/* Addition – Simple Example for Dynamic Memory Allocation */ #include #include #include void main() { int *a, *b; clrscr(); a = (int *) mal loc (sizeof(int)); b = (int *) mallo c (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: How many numbers: 5 Enter 5 values 53281 The Sum is 19
Output: Enter value for a: 3 Enter value for b: 8 The Sum is 11
11
Compiled by – GRAA
MCA Dept.
P a n i m a l a r E n g i n e e r i n g Co l l e g e
C
Programs
USER DEFINED DATA TYPES Program – 1
Program – 3
/* Complex Number Addition – Example for structures */ #include #include 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
/*Student Structure–Example for Array of structures*/ #include 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”, 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 – 2
/*Student Structure–Example for Array of structures*/ #include 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”, printf(“%s\t%s\t%d”, s[i].name, s[i].sex, &s[i].age); }
Program – 4
/*Example for structures with pointers*/ #include 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”, 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
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
12
Compiled by – GRAA
P a n i m a l a r E n g i n e e r i n g Co l l e g e
MCA Dept.
C
Programs
ENUMERATED DATA TYPES
Program – 5
/* Example for union */ #include 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 – 7
/*Example for enum data type–Defaultly starts from 0*/ #include 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
Program – 6
/*Example for enum data type–Defaultly starts from 0 – Example for switch – case */ #include 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
/*Complex Addition – Example Passing structures to function */ #include #include 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
13
Compiled by – GRAA
MCA Dept.
P a n i m a l a r E n g i n e e r i n g Co l l e g e
C
Programs
FILE HANDLING Output: Enter Student’s name and mark: Raman 98 Divya 90 ^Z Name Mark ------------------Raman 98 Divya 90
Program – 1
/* Writing text into Files – Example for fprintf, fputs, fputc functions */ #include 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 – 4
/* Program to Read and Write records from a file – Example for using structure in file and also for fwrite and fread */ #include 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 Student’s 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(“---------------------printf(“-----------------------------------------------------------“); --------“); while ((fread(&rec, sizeof(rec), 1, fp)) printf(“%5u %-10s %3u\n”, rec.regno, rec.name, rec.mark); fclose(fp); } Output: Enter Student’s Reg.No., name and mark: 345 Raman 98 Entered data will be stored in 321 Vel 88 sturec file. These data are 456 Divya 90 retrieved again for read 210 Arul 30 ^Z o era eration ion
Program – 2
/* Reading text from Files – Example for fscanf */ #include 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 void main() { /* studata studata is the file file stored stored in in secondary secondary storage */ unsigned int mark; char name[20]; /* Opening file for write (append) */ FILE* fp = fopen(“studata”, “a”); printf(“Enter Student’s 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); }
Reg.No. Name Mark -------------------------------345 Raman 98 321 Vel 88 456 Divya 90 210 Arul 30
14
Compiled by – GRAA
MCA Dept.
P a n i m a l a r E n g i n e e r i n g Co l l e g e
C
Programs
LINKED LIST / STACK / QUEUE case 4: if(!isempty()) { delfirst(); display(); } else printf("\nList Empty"); break;
Program – 1
/* Linked List Operations */
#include #include typedef struct node { int data; node *next; };
case 5: if(!isempty()) { delmid(); display(); } else printf("\nList Empty"); break;
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);
case 6: if(!isempty()) { dellast(); display(); } else printf("\nList Empty"); break;
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");
case 7: exit(0); default: printf("\nInvalid Choice\n");
printf("\nEnter your Choice... "); scanf("%d", &ch);
} } }
switch(ch) { case 1: addfirst(); display(); break;
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; }
case 2: addmid(); display(); break; case 3: addlast(); display(); break;
15
Compiled by – GRAA
P a n i m a l a r E n g i n e e r i n g Co l l e g e
MCA Dept.
void addmid() { int i=1, pos; node *temp, *cur=head;
C
Programs
scanf("%d", &pos); /*take 3*/ while(pos!=i+1 && cur->next!=NULL) { cur = cur->next; i++; }
printf("\nEnter the position to insert..."); scanf("%d", &pos); while(pos!=i && cur!=NULL) { cur = cur->next; i++; }
if(pos==i+1) { temp=cur->next; cur->next=temp->next; free(temp); }
if(pos==i) { temp = (node *) malloc(sizeof(node));
} void dellast() { node *temp, *cur=head->next;
printf("\nEnter the Data for the node..."); scanf("%d",&temp->data);
while(cur->next->next!=NULL) { cur=cur->next; }
temp->next = cur->next; cur->next = temp; } }
temp=cur->next; cur->next=NULL; free(temp);
void addlast() { node *temp, *cur=head->next; temp=(node *) malloc(sizeof(node));
} void display() { node *cur=head->next;
printf("\nEnter the Data for the node..."); scanf("%d", &temp->data); while(cur->next!=NULL) { cur=cur->next; }
printf("\nhead->"); while(cur!=NULL) { printf("%d->", cur->data); cur = cur->next; }
temp->next=cur->next; cur->next = temp; }
printf("NULL\n"); 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...");
16
Compiled by – GRAA
P a n i m a l a r E n g i n e e r i n g Co l l e g e
MCA Dept.
C
Programs
} 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; } }
Program – 2
/* Simple Linked List Operations */
#include #include 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);
void del() { node *temp; temp=head; head = head->next; free(temp); }
while(1) { printf("\n Single Linked List - Menu\n"); printf("\n 1. Add \n2.Delete\n 3.Exit\n");
void display() { node *cur=head->next;
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"); }
printf("\nhead->"); while(cur!=NULL) { printf("%d->", cur->data); cur = cur->next; } printf("NULL\n"); }
} } int isempty(void) { return head==NULL;
17
Compiled by – GRAA
P a n i m a l a r E n g i n e e r i n g Co l l e g e
MCA Dept.
C
Programs
} Program – 4 Program – 3 /* Queue Operations */ #include #include 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; }
/* Stack Operations */ #include #include 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");
18
Compiled by – GRAA
MCA Dept.
P a n i m a l a r E n g i n e e r i n g Co l l e g e
printf("NULL\n");
C
Programs
}
OTHER PROGRAMS Area of the triangle is 9.921567
Program – 1
Program – 4
/* Calculation of Simple Interest */ #include 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); }
/* Area of triangle – Given base and height */ #include 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 principal, time and rate: 1000 2 10 The simple interest is: 200.000000
Output: Enter the base and height: 3 2 Area of the triangle is 3.000000
Program – 2
Program – 5
/* Temperature Conversion */ #include 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); }
/* Program to print Pascal Triangle */ #include 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: 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 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)); 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
Output: Input number of rows: 6 Pascal Triangle: 1 1 1 1 2 1 3 3
19
1 1
Compiled by – GRAA
MCA Dept.
P a n i m a l a r E n g i n e e r i n g Co l l e g e
1 1
4 5
6 10
4 10
1 5
C
Programs
Roots are real and equal Root 1 = Root = -1.00
1
Program – 6
/* Roots of a quadratic equation */ #include #include 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
20
Compiled by – GRAA