You are on page 1of 20

Important C Programs

DECISION MAKING / SELECTION LOGIC

Program – 1 Program – 3

/* Swapping two numbers – Example for Simple if */ /* Biggest among two numbers – Example for if – else */
#include<stdio.h> #include<stdio.h>
#include<conio.h> #include<conio.h>
void main() void main()
{ {
int a, b, t; int a,b;
clrscr(); clrscr();
printf(“Enter two Numbers:”); printf(“Enter two numbers:”);
scanf(“%d%d”, &a, &b); scanf(“%d%d”, &a, &b);
if(a>b) if(a>b)
{ printf(“%d is Big”, a);
t = a; else
a = b; printf(“%d is Big”, b);
b = t; getch();
} }
printf(“The values of a = %d and b = %d”, a, 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 Program – 4

/* Leap Year – Example for if – else */ /* Display the types of character – Example for Nested
#include<stdio.h> if - else */
#include<conio.h> #include<stdio.h>
void main() #include<conio.h>
{ void main()
int year; {
clrscr(); char ch;
printf(“Enter a Year:”); clrscr();
scanf(“%d”, &year); printf(“Enter a character:”);
if(year % 4 == 0) scanf(“%c”, &ch);
{ if((ch >= ‘a’ && ch = ‘z’) || (ch >= ‘A’ && ch < = ‘Z’))
printf(“%d is a Leap Year”, year); printf(“%c is an Alphabet.\n”, ch);
} else if(ch >= ‘0’ && ch <= ‘9’)
else printf(“%c is a Digit.\n”, ch);
{ else
printf(“%d is not a Leap Year”, year); printf(“%c is a Special Character”, ch);
} getch();
getch(); }
}
Output:
Output: Enter a character: *
Enter a Year: 2000 * is a Special Character
2000 is a Leap Year
Roy Antony
Digitally signed by Roy Antony
Arnold G
DN: cn=Roy Antony Arnold G,

Arnold G
o=Photon Soft Solutions, ou,
email=arnoldindia@aol.in, c=IN
Date: 2009.08.11 19:33:05 -08'00'
Panimalar Engineering College MCA Dept. C Programs

Program – 5 Program – 6

/* Arithmetic Operations (Calculator) – Example for /* Prime Number Program – Example for goto label */
switch – case */ #include<stdio.h>
#include<stdio.h> #include<conio.h>
#include<conio.h> #include<math.h>
void main() void main()
{ {
char ch; int n, i, r;
int a, b, ans; clrscr();
clrscr(); printf(“Enter the positive integer value…”);
printf(“Calculator:\n + : Addition\n - : Subtract \n * : scanf(“%d”, &n);
Multiply\n / : Division\n % : Mod Division\n”); i=2;
printf(“Enter the code:”); step1:
scanf(“%c”, &ch); if(i<=sqrt(n))
printf(“\nEnter two values: “); {
scanf(“%d%d”, &a, &b); r=n%i;
switch(ch) if(r==0)
{ {
case ‘+’ : printf(“%d is not a prime”, n);
ans = a+b; goto end;
break; }
case ‘-‘ : }
ans = a-b; else
break; {
case ‘*’ : i++;
ans = a*b; goto step1;
break; }
case ‘/’ : printf(“%d is prime”, n);
ans = a/b; getch();
break; end:
case ‘%’ : printf(“ “);
ans = a%b;
break; }
default:
printf(“\nInvalid Operator”); Output:
ans = 0; Enter the positive integer value… 3
} 3 is prime
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
2
Panimalar Engineering College MCA Dept. C Programs

LOOPING / ITERATION / REPETITION


Program – 1 Program – 3

/* Sum of numbers upto 10 – Example for while loop */ /* Sum of numbers upto 10–Example for do–while loop */
#include<stdio.h> #include<stdio.h>
#include<conio.h> #include<conio.h>
void main() void main()
{ { int sum=0, i =1;
int sum=0, i =1; clrscr();
clrscr(); do
while(i<=10) { sum = sum + i;
{ i++;
sum = sum + i; } while(i<=10);
i++; printf(“The sum of numbers upto 10 is %d”, sum);
} getch();
printf(“The sum of numbers upto 10 is %d”, sum); }
getch(); Output:
} The sum of numbers upto 10 is 55

Output:
Program – 4
The sum of numbers upto 10 is 55
/* Sum of numbers upto 10 – Example for ‘for’ loop */
Program – 2 #include<stdio.h>
#include<conio.h>
/* Reversing given number & Checking Palindrome – void main()
Example for while loop */ {
#include<stdio.h> int sum=0, i;
#include<conio.h> clrscr();
void main() for(i=1; i<=10; i++)
{ {
int rev=0, n, num, digit; sum = sum + i;
clrscr(); }
printf(“\nEnter the number: “); printf(“The sum of numbers upto 10 is %d”, sum);
scanf(“%d”, &n); getch();
num = n; }
while(n!=0) Output:
{ The sum of numbers upto 10 is 55
digit = n % 10;
rev = rev * 10 + digit;
n = n/10; Program – 5
}
printf(“\nThe Reversed Number is %d”, rev); /* Print numbers upto 5 – Example for break stat. */
if (rev == num) #include<stdio.h>
printf(“\nThe Number is Palindrome”); void main()
else { int i;
printf(“\nThe Number is not a Palindrome”); for(i=1; i<=10; i++)
getch(); { if (i==6)
} break;
printf(“%d \t”, i);
}
Output:
Enter the number : 1221 }
The Reversed Number is 1221 Output:
The Number is Palindrome 1 2 3 4 5

Compiled by – GRAA
3
Panimalar Engineering College MCA Dept. C Programs

Program – 6 Program – 8

/* Sum of +ve numbers – Example for continue stat. */ /* Generation of Fibonacci Series – Example for while
#include<stdio.h> loop */
void main() #include<stdio.h>
{ #include<conio.h>
int i, n, sum = 0; void main()
for(i=1; i<=5; i++) {
{ int f1 = -1, f2 = 1, n, newterm = 0;
printf(“\n Enter a number…”); clrscr();
scanf(“%d”, &n); printf(“\nEnter the final term of series : “);
if(n<0) scanf(“%d”, &n);
continue; while(newterm <= n)
else {
sum = sum + n; newterm = f1 + f2;
} f1 = f2;
printf(“\nThe Sum is … %d”, sum); f2 = newterm;
} printf(“%d\t”, newterm);
}
Output: /* Negative Numbers are neglected */ getch();
Enter a number … 10 }
Enter a number … -12
Enter a number … 12 Output:
Enter a number … -10 Enter the final term of series: 21
Enter a number … 5 0 1 1 2 3 5 8 13 21
The Sum is … 27
Program – 9
Program – 7
/* Sum of digits of a number – Example for while loop */
/* Printing Number Pyramid – Example for nested for #include<stdio.h>
loop */ #include<conio.h>
#include<stdio.h> void main()
#include<conio.h> {
void main() int sum=0, num, rem;
{ clrscr();
int i, j, n; printf(“\nEnter the number: “);
clrscr(); scanf(“%d”, &num);
printf(“\nEnter a number : “); while(num!=0)
scanf(“%d”, &n); {
for(i=1; i<=n; i++) rem = num % 10;
{ sum = sum + rem;
for(j=i; j<=n; j++) num = num/10;
printf(“%d\t”, j); }
printf(“\nThe Sum of digits is %d”, sum);
printf(“\n”); getch();
} }
getch();
} Output:
Enter the number : 1221
Output: The Sum of digits is 6
Enter a number : 5
1 2 3 4 5
2 3 4 5
3 4 5
4 5
5

Compiled by – GRAA
4
Panimalar Engineering College MCA Dept. C Programs

FUNCTIONS AND RECURSION


Program – 1 Program – 3

/* Sum of two numbers – Example for global variable */ /* Sum of two numbers – Example for Function with
#include<stdio.h> arguments and no return value – Call by value */
#include<conio.h> #include<stdio.h>
int a, b; #include<conio.h>
void main() void main()
{ {
void sum(void); void sum(int, int);
clrscr(); int a, b;
printf(“\nEnter two numbers: “); clrscr();
scanf(“%d%d”, &a, &b); printf(“\nEnter two numbers: “);
sum(); scanf(“%d%d”, &a, &b);
getch(); sum(a, b); /* Function Call */
} getch();
}
void sum (void)
{ void sum (int a, int b)
printf(“The Sum is %d”, a+b); {
} printf(“The Sum is %d”, a+b);
}
Output:
Enter two numbers : 12 21 Output:
The Sum is 33 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 */ /* Sum of two numbers – Example for Function with
#include<stdio.h> arguments and with return value – Call by value */
#include<conio.h> #include<stdio.h>
void main() #include<conio.h>
{ void main()
void sum(void); {
clrscr(); int sum(int, int); /*Function Prototype */
sum(); /* Function Call */ int a, b;
getch(); clrscr();
} printf(“\nEnter two numbers: “);
scanf(“%d%d”, &a, &b);
void sum (void) printf(“The Sum is %d”, sum(a, b)); /* Call by value */
{ getch();
int a, b; }
printf(“\nEnter two numbers: “);
scanf(“%d%d”, &a, &b); int sum (int a, int b)
printf(“The Sum is %d”, a+b); {
} return a+b;
}
Output:
Enter two numbers : 10 21 Output:
The Sum is 31 Enter two numbers : 10 21
The Sum is 31

Compiled by – GRAA
5
Panimalar Engineering College MCA Dept. C Programs

Program – 5 Program – 7

/* Sum of two numbers – Example for Function with no /*Sum of two numbers–Example for Pointer Function*/
arguments and with return value */ #include<stdio.h>
#include<stdio.h> #include<conio.h>
#include<conio.h> void main()
void main() {
{ int* sum(int*, int*); /*Function Prototype */
int sum(void); int *s;
clrscr(); clrscr();
printf(“The Sum is %d”, sum()); /* Function Call */ printf(“\nEnter two numbers: “);
getch(); scanf(“%d%d”, &a, &b);
} s = sum(&a, &b);
printf(“The Sum is %d”, *s);
int sum (void) getch();
{ }
int a, b;
printf(“\nEnter two numbers: “); int* sum (int *x, int *y)
scanf(“%d%d”, &a, &b); {
return a+b; 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

/* Sum of two numbers – Example for Function Call by Program – 8


Reference */
#include<stdio.h> /* Finding Factorial – Example for Function */
#include<conio.h> #include<stdio.h>
void main() #include<conio.h>
{ void main()
int sum(int*, int*); /*Function Prototype */ {
int a, b; int fact(int);
clrscr(); int n;
printf(“\nEnter two numbers: “); clrscr();
scanf(“%d%d”, &a, &b); printf(“\nEnter a number: “);
printf(“The Sum is %d”, sum(&a, &b)); scanf(“%d”, &n);
getch(); printf(“The Factorial is %d”, fact(n));
} getch();
}
int sum (int *x, int *y)
{ int fact (int num)
return (*x + *y); {
} int f=1, i;
for(i = num; i>0; i--)
Output: f = f * i;
Enter two numbers : 10 21 return f;
The Sum is 31 }

Output:
Enter a numbers : 5
The Factorial is 120

Compiled by – GRAA
6
Panimalar Engineering College MCA Dept. C Programs

Program – 9 Program – 11

/* Finding Factorial – Example for Recursive Function */ /* Tower of Honoi – Example for Recursive Function */
#include<stdio.h> #include<stdio.h>
#include<conio.h> #include<conio.h>
void main() void main()
{ {
int fact(int); void honoi(int, char, char, char);
int n; int n;
clrscr(); clrscr();
printf(“\nEnter a number: “); printf(“\nHow many disks: “);
scanf(“%d”, &n); scanf(“%d”, &n);
printf(“The Factorial is %d”, fact(n)); honoi(n, ‘L’, ‘R’, ‘C’);
getch(); getch();
} }

int fact (int num) void honoi (int n, char from, char to, char temp)
{ {
int f; if(n > 0)
if (num == 1) {
return 1; honoi(n-1, from, temp, to);
else printf(“Move Disk %d from %c to %c\n”, n, from, to);
f = num * fact(num – 1); / * Recursive call */ honoi(n-1, temp, to, from);
return f; }
} }

Output: Output:
Enter a numbers : 5 How many disks: 3
The Factorial is 120 Move Disk 1 from L to R
Move Disk 2 from L to C
Move Disk 1 from R to C
Program – 10
Move Disk 3 from L to R
Move Disk 1 from C to L
/* Reversing String – Example for Recursive Function */ Move Disk 2 from C to R
#include<stdio.h>
Move Disk 3 from L to R
#include<conio.h>
void main()
{ Program – 12
void reverse(void);
clrscr(); /* Example for static variable & empty for loop */
printf(“\nEnter the Text: “); #include<stdio.h>
reverse(); void main()
getch(); {
} void show(void);
for( ; ; )
void reverse (void) show();
{ }
char c;
if(( c = getchar( ) ) != ‘\n’) void show (void)
reverse(); /* Recursive call */ {
purchar(c); int static n;
} printf(“%d\t”, ++n);
if(n == 5)
exit(1);
Output:
}
Enter the Text : ARUNAI
IANURA
Output:
1 2 3 4 5

Compiled by – GRAA
7
Panimalar Engineering College MCA Dept. C Programs

DERIVED DATA TYPES /ARRAYS


Program – 1 Program – 3

/* Sum of numbers – Example for Array */ /* Matrix Multiplication */


#include<stdio.h> #include<stdio.h>
void main() void main()
{ int sum=0, i, a[5]; { int i, j, k, a[5][5], b[5][5], c[5][5], r1, r2, c1, c2;
printf(“\nEnter 5 Numbers: “); step1:
for(i=1; i<=10; i++) printf(“\nEnter the size of Matrix A\n “);
{ scanf(“%d”, &a[i]); scanf(“%d%d”, &r1, &c1);
sum = sum + a[i]; printf(“\nEnter the size of Matrix B\n”);
} scanf(“%d%d”, &r2, &c2);
printf(“The sum of numbers is %d”, sum); if(c1==r2)
} goto step2;
Output: else
Enter 5 Numbers: 10 20 30 40 50 printf(“\nMultiplication Not possible”);
The sum of numbers is 150 goto step1;
step2:
printf(“\nEnter elements for Matrix A\n”);
Program – 2
for(i=0; i < r1; i++)
for(j=0; j < c1; j++)
/* Printing Matrix – Example for 2-dimensional Array */
scanf(“%d”, &a[i][j]);
#include<stdio.h>
printf(“\nEnter elements for Matrix B\n “);
void main()
for(i=0; i < r2; i++)
{ int i, j, a[3][3];
for(j=0; j < c2; j++)
printf(“\nEnter Elements for Matrix\n “);
scanf(“%d”, &b[i][j]);
for(i=0; i<3; i++)
for(i=0; i<r1; i++) /*Matrix Multiplication */
for(j=0; j<3; j++)
{
{ printf(“Enter element (%d, %d):”, i, j);
for(j=0; j<c1; j++)
scanf(“%d”, &a[i][j]);
{
}
c[i][j] = 0;
printf(“\nThe Entered Matrix is\n “);
for(k=0; k<c1; k++)
for(i=0; i<3; i++)
c[i][j] = c[i][j] + a[i][k] * b[k][j];
{
}
for(j=0; j<3; j++)
}
printf(“%d\t”, a[i][j]);
printf(“\nThe resultant Matrix is…\n”);
printf(“\n”);
for(i=0; i<r1; i++)
}
{
}
for(j=0; j<c1; j++)
Output:
printf(“%d\t”, c[i][j]);
Enter Elements for Matrix
printf(“\n”);
Enter element (0,0): 2
}
Enter element (0,1): 4
}
Enter element (0,2): 1
Output:
Enter element (1,0): 3
Enter the size of Matrix A 3 3
Enter element (1,1): 6
Enter the size of Matrix B 3 3
Enter element (1,2): 3
Enter the elements for Matrix A
Enter element (2,0): 2
222222222
Enter element (2,1): 5
Enter the elements for Matrix B
Enter element (2,2): 0
333333333
The resultant Matrix is…
The Entered Matrix is
18 18 18
2 4 1
18 18 18
3 6 3
18 18 18
2 5 0

Compiled by – GRAA
8
Panimalar Engineering College MCA Dept. C Programs

Program – 4 Program – 6

/* Matrix Addition */ /* Sorting array of numbers */


#include<stdio.h> #include<stdio.h>
void main() void main()
{ int i, j, a[5][5], b[5][5], c[5][5]; { int i, n, t, j, a[10];
printf(“\nEnter elements for Matrix A\n”); printf(“\nHow many Numbers: “);
for(i=0; i < r1; i++) scanf(“%d”, &n);
for(j=0; j < c1; j++) printf(“\nEnter the Numbers:”);
scanf(“%d”, &a[i][j]); for(i=0; i<n; i++)
printf(“\nEnter elements for Matrix B\n “); scanf(“%d”, &a[i]);
for(i=0; i < r2; i++) for(i=0; i< n-1; i++)
for(j=0; j < c2; j++) for(j=i; j<=n-1; j++)
scanf(“%d”, &b[i][j]); if(a[i] > a[j])
for(i=0; i<r1; i++) {
for(j=0; j<c1; j++) t = a[i];
c[i][j] = a[i][j] + b[i][j]; a[i] = a[j];
printf(“\nThe resultant Matrix is…\n”); a[j] = t;
for(i=0; i<r1; i++) }
{ printf(“\nThe Numbers in ascending order are :\n”);
for(j=0; j<c1; j++) for(i=0; i < n; i++)
printf(“%d\t”, c[i][j]); printf(“%d\t”, a[i]);
printf(“\n”); }
} Output:
} How many Numbers: 5
Output: Enter the Numbers: 20 1 3 44 15
Enter the elements for Matrix A The Numbers is ascending order are:
222222222 1 3 15 20 44
Enter the elements for Matrix B
333333333
Program – 6
The resultant Matrix is…
5 5 5
/* Linear Search */
5 5 5
#include<stdio.h>
5 5 5
void main()
{ int i, n, k, a[10], flag = 1;
Program – 5 printf(“\nHow many Numbers: “);
scanf(“%d”, &n);
/* Sum of numbers – Example for Passing Arrays to printf(“\nEnter the Numbers:”);
Function */ for(i=0; i<n; i++)
#include<stdio.h> scanf(“%d”, &a[i]);
void main() printf(“\nEnter the number to search:”);
{ int i, a[5], add(int [ ]); scanf(“%d”, &k)
printf(“\nEnter 5 Numbers: “); for(i=0; i< n; i++)
for(i=0; i<5; i++) if( k == a[i])
scanf(“%d”, &a[i]); { printf(“The element %d is at position %d”, k, i+1);
printf(“\nThe Sum of Numbers is %d”, add(a)); flag = 0;
} break;
int add(int b[ ]) }
{ int i, sum = 0; if(flag == 1)
for(i=0; i<5; i++) printf(“\nThe Number not found in the array”);
sum = sum + a[i]; }
return(sum); Output:
} How many Numbers: 5
Output: Enter the Numbers: 20 1 3 44 15
Enter 5 Numbers: 10 20 30 40 50 Enter the number to search: 44
The Sum of Numbers is 150 The element 44 is at position 4

Compiled by – GRAA
9
Panimalar Engineering College MCA Dept. C Programs

DERIVED DATA TYPES /POINTERS


Program – 1 Program – 4

/* Print address and value of variable – Simple Example /* Sum of N numbers – Example for Using pointers with
for pointer */ Arrays */
#include<stdio.h> #include<stdio.h>
void main() void main()
{ int a=10, *b; { int *p, sum=0, a[5];
b = &a; printf(“\nEnter five numbers”);
printf(“\nThe Value of a = %d”, *b); for(p = a; p < a+5; p++)
printf(“\nAddress of a = %u”, b); scanf(“%d”, p);
} for(p = a; p < a+5; p++)
Output: sum = sum + *p;
The Value of a = 10 printf(“\nThe Sum is %d”, sum);
Address of a = 2028 }
Output:
Enter five numbers 10 20 30 40 50
Program – 2
The Sum is 150
/* Swapping values of variable – Simple Example for Call
by Reference (Using pointers with function) */ Program – 5
#include<stdio.h>
void main() /* Finding Biggest – Example for Function pointer
{ int a, b; (Function that returns the address not value)*/
void swap(int *, int *); #include<stdio.h>
printf(“\nEnter two number”); void main()
scanf(“%d%d”, &a, &b); { int *big, a, b;
printf(“\nBefore Exchange a = %d, b = %d”, a, b); int *biggest(int *, int *); /* function returns address */
swap(&a, &b); printf(“\nEnter two numbers”);
printf(“\nAfter Exchange a = %d, b = %d”, a, b); scanf(“%d%d”, &a, &b);
} big = biggest(&a, &b); /* address of biggest no. is stored in big*/
void swap(int *x, int *y) printf(“\nThe Biggest Number is %d”, *big);
{ int t; }
t = *x; int *biggest(int *m, int *n)
x = *y; { if(*m > *n) return (m); /*returns address stored in m */
y = t; else return (n); /* returns address stored in n */
} }
Output: Output:
Enter two number 10 23 Enter two numbers 30 50
Before Exchange a = 10, b = 23 The Biggest Number is 50
After Exchange a = 23, b = 10
Program – 6
Program – 3
/* Example for Array of pointers */
/* Printing Array Elements– Simple Example for using #include<stdio.h>
pointers with arrays */ void main()
#include<stdio.h> { int a = 4, b = 3, c = 2, i;
void main() int *p[3]={&a, &b, &c};
{ int *p, a[5] = {1, 2, 3, 4, 5}; printf(“\nThe Values are: “);
for(p = a; p < a+5; p++) for(i=0; i < 3; i++)
printf(“%d\t”, *p); printf(“%d\t”, *p[i]);
} }
Output: Output:
1 2 3 4 5 The Values are: 4 3 2

Compiled by – GRAA
10
Panimalar Engineering College MCA Dept. C Programs

Program – 7 Program – 9

/* Pointer Arithmetic – Example for Operations on /* Example for memory allocation functions usage */
Pointers*/ #include<stdio.h>
#include<stdio.h> #include<stdlib.h>
void main() #include<string.h>
{ void main()
int *p, a[5]; {
p = a; char *p;
printf(“\nAddress of a = %u”, p); p = (char *) malloc (7); /* allocates memory */
printf(“\nPostincrement of address = %u”, p++); strcpy(p, “MADRAS”);
printf(“\nPreincrement of address = %u”, ++p); printf(“\nMemory Contains: %s”, p);
printf(“\nPredecrement of address = %u”, --p); p = (char *) realloc(8); /* reallocates memory */
printf(“\nPostdecrement of address = %u”, p--); strcpy(p, “CHENNAI”);
printf(“\nAddress stored in p = %u”, p); printf(“\nMemory now Contains: %s”, p);
} free(p); /* Releases the memory */
}
Output:
Address of a = 3022 Output:
Postincrement of Address = 3022 Memory Contains: MADRAS
Preincrement of Address = 3026 Memory now Contains: CHENNAI
Predecrement of Address = 3024
Postdecrement of Address = 3024
Program – 10
Address stored in p = 3022
/* Sum of Array elements – Example for Dynamic
DYNAMIC MEMORY ALLOCATION Memory Allocation of Arrays */
#include<stdio.h>
#include<stdlib.h>
Program – 8 #include<conio.h>
void main()
/* Addition – Simple Example for Dynamic Memory {
Allocation */ int *a, sum = 0, n, i;
#include<stdio.h> clrscr();
#include<stdlib.h> printf(“\nHow many numbers: “);
#include<conio.h> scanf(“%d”, &n);
void main() a = (int *) malloc (n * sizeof(int));
{ printf(“\nEnter %d values \n”, n);
int *a, *b; for( i = 0; i < n; i++)
clrscr(); scanf(“%d’, a+i);
a = (int *) malloc (sizeof(int)); for( i = 0; i < n; i++)
b = (int *) malloc (sizeof(int)); sum = sum + *(a+i);
printf(“\nEnter value for a:”); printf(“\nThe Sum is %d”, sum);
scanf(“%d’, a); getch();
printf(“\nEnter value for b:”); }
scanf(“%d”, b);
printf(“\nThe Sum is %d”, *a + *b); Output:
getch(); How many numbers: 5
} Enter 5 values
53281
Output: The Sum is 19
Enter value for a: 3
Enter value for b: 8
The Sum is 11

Compiled by – GRAA
11
Panimalar Engineering College MCA Dept. C Programs

USER DEFINED DATA TYPES


Program – 1 Program – 3

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

Compiled by – GRAA
12
Panimalar Engineering College MCA Dept. C Programs

Program – 5 ENUMERATED DATA TYPES


/* Example for union */
Program – 7
#include<stdio.h>
void main()
/*Example for enum data type–Defaultly starts from 0*/
{ union num
#include<stdio.h>
{ int a;
void main()
float b;
{ enum number{Zero, One, Two, Three};
} n;
printf(“ ZERO = %d”, Zero);
n.a = 5;
printf(“ONE = %d”, One);
printf(“\nThe value of a is %d “, n.a);
printf(“TWO = %d”, Two);
n.b = 17.23;
printf(“THREE = %d”, Three);
printf(“\nThe value of a is %f “, n.b);
}
printf(“\nThe size of n is %d”, sizeof(n));
} Output:
Output: ZERO = 0
The value of a is 5 ONE = 1
The value of a is 17.23 TWO = 2
The size of n is 4 THREE = 3

Program – 6 Program – 8

/*Complex Addition – Example Passing structures to /*Example for enum data type–Defaultly starts from 0 –
function */ Example for switch – case */
#include<stdio.h> #include<stdio.h>
#include<conio.h> void main()
void main() { enum country
{ typedef struct complex { INDIA = 91,
{ int real, imag; AMERICA = 1,
}; AUSTRALIA = 61,
complex c1, c2, c3; ITALY = 39
complex add(complex, complex); };
clrscr(); int code;
printf(“\nEnter first complex number: “); printf(“\nPlease Enter a Country Code:”);
scanf(“%d%d”, &c1.real, &c1.imag); scanf(“%d”, &code);
printf(“\nEnter second complex number: “); switch(code)
scanf(“%d%d”, &c2.real, &c2.imag); { case 91:
c3 = add(c1, c2); printf(“\nThis code is belongs to India”);
printf(“\nResultant complex number is %d+%d i ”, break;
c3.real, c3.imag); case 1:
getch(); printf(“\nThis code is belongs to America”);
} break;
case 61:
complex add(complex a, complex b) printf(“\nThis code is belongs to Australia”);
{ break;
complex c; case 39:
c.real = a.real + b.real; printf(“\nThis code is belongs to Italy”);
c.imag = a.imag + b.imag; default:
return (c); printf(“\nNo such code in database”);
} }
Output: }
Enter first complex number: 3 4 Output 1:
Enter second complex number: 4 2 Please Enter a Country Code: 39
Resultant complex number is 7+ 6i This code is belongs to Italy
Output 2:
Please Enter a Country Code: 121
No such code in database

Compiled by – GRAA
13
Panimalar Engineering College MCA Dept. C Programs

FILE HANDLING
Output:
Program – 1
Enter Student’s name and mark:
Raman 98
/* Writing text into Files – Example for fprintf, fputs,
Divya 90 ^Z
fputc functions */
Name Mark
#include<stdio.h>
-------------------
void main()
Raman 98
{ FILE *fp = fopen(“file1.txt”, “w”);
Divya 90
fprintf(fp, “India is my country”);
fputc(fp, ‘&’);
fputs(fp, “I Love my country”); Program – 4
fclose(fp);
} /* Program to Read and Write records from a file –
Output: /*The file file1.txt will have the following lines*/ Example for using structure in file and also for fwrite
India is my country and fread */
& #include<stdio.h>
I Love my country typedef struct student
{
unsigned int regno;
Program – 2
char name[20];
unsigned int mark;
/* Reading text from Files – Example for fscanf */
};
#include<stdio.h>
void main()
void main()
{ /* classrec is the file contains all the records */
{ int i; /* assume file2.txt contains only 30 */
student rec;
FILE* fp = fopen(“file2.txt”, “r”);
FILE* fp = fopen(“classrec”, “wb”);
fscanf(fp, “%d”, &i);
printf(“Enter Student’s Reg.No., name and mark:\n “);
printf(“\nThe integer in file2.txt is %d”, i);
printf(“Use Ctrl + Z to stop entry\n”);
fclose(fp);
while((scanf(“%u%s%u”, &rec.regno, rec.name,
}
&rec.mark)) != EOF)
Output: fwrite(&rec, sizeof(rec), 1, fp);
The integer in file2.txt is 30 fclose(fp);
printf(“\n”);
Program – 3 fp = fopen (“classrec”, “rb”);
printf(“Reg.No. \t Name \t Mark\n”);
/* Program to Read and Write to a file – Example for printf(“---------------------------------------------“);
file operations*/ while ((fread(&rec, sizeof(rec), 1, fp))
#include<stdio.h> printf(“%5u %-10s %3u\n”, rec.regno, rec.name,
void main() rec.mark);
{ /* studata is the file stored in secondary storage */ fclose(fp);
unsigned int mark; }
char name[20]; /* Opening file for write (append) */ Output:
FILE* fp = fopen(“studata”, “a”); Enter Student’s Reg.No., name and mark:
printf(“Enter Student’s name and mark:\n “); 345 Raman 98
printf(“Use Ctrl + Z to stop entry\n”); 321 Vel 88 Entered data will be stored in
sturec file. These data are
while((scanf(“%s%u”, name, &mark)) != EOF) 456 Divya 90
retrieved again for read
fprintf(fp, “%s %u”, name, mark); 210 Arul 30 ^Z operation
fclose(fp); /* Opening file for read */
fp = fopen (“studata”, “rt”); Reg.No. Name Mark
printf(“\t Name \t Mark\n”); --------------------------------
printf(“-------------------------“); 345 Raman 98
while ((fscanf(fp, “%s %u”, name, &mark)) != EOF) 321 Vel 88
printf(“%-10s %3u\n”, name, mark); 456 Divya 90
fclose(fp); 210 Arul 30
}

Compiled by – GRAA
14
Panimalar Engineering College MCA Dept. C Programs

LINKED LIST / STACK / QUEUE


Program – 1 case 4:
if(!isempty())
/* Linked List Operations */ {
delfirst();
#include<stdio.h> display();
#include<stdlib.h> }
else
typedef struct node printf("\nList Empty");
{ break;
int data;
node *next; case 5:
}; if(!isempty())
{
node *head=0; delmid();
display();
void main() }
{ else
int ch; printf("\nList Empty");
void addfirst(void); break;
void addmid(void);
void addlast(void); case 6:
void delfirst(void); if(!isempty())
void delmid(void); {
void dellast(void); dellast();
void display(void); display();
int isempty(void); }
else
while(1) printf("\nList Empty");
{ break;
printf("\n Single Linked List - Menu\n");
printf("\n 1. Add at First\n 2. Add at Middle\n 3. case 7:
Add at Last\n 4.Delete First\n 5.Delete Middle\n 6.Delete exit(0);
Last\n 7.Exit\n");
default:
printf("\nEnter your Choice... "); printf("\nInvalid Choice\n");
scanf("%d", &ch); }
}
switch(ch) }
{
case 1: int isempty(void)
addfirst(); {
display(); return head==NULL;
break; }

case 2: void addfirst()


addmid(); {
display(); node *temp;
break; temp = (node *) malloc(sizeof(node));
printf("\nEnter the Data for the node... ");
case 3: scanf("%d", &temp->data);
addlast(); temp->next = head;
display(); head = temp;
break; }

Compiled by – GRAA
15
Panimalar Engineering College MCA Dept. C Programs

void addmid() scanf("%d", &pos); /*take 3*/


{
int i=1, pos; while(pos!=i+1 && cur->next!=NULL)
node *temp, *cur=head; {
cur = cur->next;
printf("\nEnter the position to insert..."); i++;
scanf("%d", &pos); }

while(pos!=i && cur!=NULL) if(pos==i+1)


{ {
cur = cur->next; temp=cur->next;
i++; cur->next=temp->next;
} free(temp);
}
if(pos==i) }
{
temp = (node *) malloc(sizeof(node)); void dellast()
{
printf("\nEnter the Data for the node..."); node *temp, *cur=head->next;
scanf("%d",&temp->data);
while(cur->next->next!=NULL)
temp->next = cur->next; {
cur->next = temp; cur=cur->next;
} }
}
temp=cur->next;
void addlast() cur->next=NULL;
{ free(temp);
node *temp, *cur=head->next; }
temp=(node *) malloc(sizeof(node));
void display()
printf("\nEnter the Data for the node..."); {
scanf("%d", &temp->data); node *cur=head->next;

while(cur->next!=NULL) printf("\nhead->");
{
cur=cur->next; while(cur!=NULL)
} {
printf("%d->", cur->data);
temp->next=cur->next; cur = 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...");

Compiled by – GRAA
16
Panimalar Engineering College MCA Dept. C Programs

Program – 2 }
void add()
/* Simple Linked List Operations */ {
int i=1, pos;
#include<stdio.h> node *cur=head->next , *temp;
#include<stdlib.h> temp = (node *) malloc(sizeof(node));
printf("\nEnter the Data for the node... ");
typedef struct node scanf("%d", &temp->data);
{ if (head == NULL)
int data; {
node *next; temp->next = head;
}; head = temp;
}
node *head=0; else
{
void main() temp->next = cur->next;
{ cur->next = temp;
int ch; }
void add(void); }
void del(void);
void display(void); void del()
int isempty(void); {
node *temp;
while(1) temp=head;
{ head = head->next;
printf("\n Single Linked List - Menu\n"); free(temp);
printf("\n 1. Add \n2.Delete\n 3.Exit\n"); }

printf("\nEnter your Choice... "); void display()


scanf("%d", &ch); {
node *cur=head->next;
switch(ch)
{ printf("\nhead->");
case 1:
add(); while(cur!=NULL)
display(); {
break; printf("%d->", cur->data);
case 2: cur = cur->next;
if(!isempty()) }
{
del(); printf("NULL\n");
display(); }
}
else
printf("\nList Empty");
break;
case 3:
exit(0);
default:
printf("\nInvalid Choice\n");
}
}
}

int isempty(void)
{
return head==NULL;

Compiled by – GRAA
17
Panimalar Engineering College MCA Dept. C Programs

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

Compiled by – GRAA
18
Panimalar Engineering College MCA Dept. C Programs

printf("NULL\n"); }

OTHER PROGRAMS
Area of the triangle is 9.921567
Program – 1

/* Calculation of Simple Interest */ Program – 4


#include<stdio.h>
void main() /* Area of triangle – Given base and height */
{ #include<stdio.h>
float p, t, r, simple; void main()
printf(“\nEnter Principal, time, and rate: “); {
scanf(“%f%f%f”, &p, &t, &r); float b, h, area;
simple = p * t * r /100; printf(“\nEnter the base and height: “);
printf(“The simple interest is: %f\n”, simple); scanf(“%f%f”, &b, &h);
} area = b * h / 2
Output: printf(“Area of the triangle is %f\n”, area);
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

/* Temperature Conversion */ Program – 5


#include<stdio.h>
void main() /* Program to print Pascal Triangle */
{ float c, f; #include<stdio.h>
printf(“\nEnter temperature in Celsius: “); void main()
scanf(“%f”, &c); {
f = 1.8 * c + 32; int binom=1, p, q=0, r, x;
printf(“\nEquivalent Fahrenheit = %f\n”, f); printf(“Input number of rows: “);
printf(“\nEnter temperature in Fahrenheit: “); scanf(“%d”, &p);
scanf(“%f”, &f); printf(“Pascal Triangle: \n”);
c = (f-32) /1.8; while(q < p)
printf(“Equivalent Celsius = %f\n”, c); {
} for( r=40 – 3 * q; r > 0; r--)
Output: printf(“ “);
Enter temperature in Celsius: 10 for( x = 0; x <= q; x++)
Equivalent Fahrenheit = 50.000000 {
Enter temperature in Fahrenheit: 50 if((x==0 ) || (q==0))
Equivalent Celsius = 10.000000 binom = 1;
else
binom = (binom * (q-x+1)) / x;
Program – 3
printf(“%6d”, binom);
}
/* Area of a triangle – Given three sides */
printf(“ \n”);
#include<stdio.h>
q++;
void main()
}
{ float a, b, c, s, area;
}
printf(“\nEnter the 3 sides: “);
scanf(“%f%f%f”, &a, &b, &c);
Output:
s = (a+b+c)/2;
Input number of rows: 6
area = sqrt(s*(s-a)*(s-b)*(s-c));
Pascal Triangle:
printf(“\nArea of the triangle is %f\n”, area);
1
}
1 1
Output:
1 2 1
Enter the 3 sides: 4 5 6
1 3 3 1

Compiled by – GRAA
19
Panimalar Engineering College MCA Dept. C Programs

1 4 6 4 1 Roots are real and equal


1 5 10 10 5 1 Root 1 = Root = -1.00

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

Compiled by – GRAA
20

You might also like