Professional Documents
Culture Documents
Important C Programs
Important C Programs
Program 4
#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
Digitally signed by Roy Antony Arnold G DN: cn=Roy Antony Arnold G, o=Photon Soft Solutions, ou, email=arnoldindia@aol.in, c=IN Date: 2009.08.11 19:33:05 -08'00'
C Programs
#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.
Compiled by GRAA
MCA Dept.
C Programs
/* 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
C Programs
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
Compiled by GRAA
MCA Dept.
C Programs
/* 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 no arguments and no return value */
#include<stdio.h> #include<conio.h> 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 */
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
#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
Compiled by GRAA
MCA Dept.
C Programs
Program 5
Program 7
/* 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
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
Compiled by GRAA
C Programs
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
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
void reverse (void) { char c; if(( c = getchar( ) ) != \n) reverse(); /* Recursive call */ purchar(c); } Output: Enter the Text : ARUNAI IANURA
void show (void) { int static n; printf(%d\t, ++n); if(n == 5) exit(1); } Output: 1 2 3
Compiled by GRAA
MCA Dept.
C Programs
/* 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
Compiled by GRAA
MCA Dept.
C Programs
Program 4
Program 6
/* 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 5 5 5 5 5 5 5 5 5 Program 5
#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
Compiled by GRAA
MCA Dept.
C Programs
/* 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
/* 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
/* 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: 1 2 3 4 5
10
Compiled by GRAA
C Programs
11
Compiled by GRAA
MCA Dept.
C Programs
#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
12
Compiled by GRAA
MCA Dept.
C Programs
#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
/*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
13
Compiled by GRAA
MCA Dept.
C Programs
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: Enter Students name and mark: Raman 98 Divya 90 ^Z Name Mark ------------------Raman 98 Divya 90 Program 4
Output: /*The file file1.txt will have the following lines*/ India is my country & I Love my country Program 2
/* Program to Read and Write records from a file Example for using structure in file and also for fwrite and fread */
#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); }
#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 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 operation Reg.No. Name Mark -------------------------------345 Raman 98 321 Vel 88 456 Divya 90 210 Arul 30
14
Compiled by GRAA
MCA Dept.
C Programs
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; }
15
Compiled by GRAA
Panimalar Engineering College 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...");
MCA Dept. 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"); }
C Programs
16
Compiled by GRAA
C Programs
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"); }
17
Compiled by GRAA
MCA Dept.
}
C Programs
Program 3
/* Stack Operations */ /* Queue Operations */
Program 4
#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");
#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; }
18
Compiled by GRAA
MCA Dept.
C Programs
OTHER PROGRAMS
Program 1 Area of the triangle is 9.921567 Program 4
#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
#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
#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 2 1 3
1 1 3 1
19
Compiled by GRAA
MCA Dept. Roots are real and equal Root 1 = Root = -1.00
C Programs
Program 6
#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
20
Compiled by GRAA