Simple Programs

/* Swapping two numbers (using two variables) - SWAPTWO.C */ # include <stdio.h> # include <conio.h> void main() { int a, b ; clrscr() ; printf("Enter two numbers : ") ; scanf("%d %d", &a, &b) ; printf("\nBefore swapping : \n\n") ; printf("a = %d \t b = %d", a, b) ; a = a + b ; b = a - b ; a = a - b ; printf("\n\nAfter swapping : \n\n") ; printf("a = %d \t b = %d", a, b) ; getch() ; } RUN 1 : ~~~~~~~ Enter two numbers : 10 Before swapping : a = 10 b = 20 20

After swapping : a = 20 b = 10

A.2 Programs in C
/* Swapping two numbers (using three variables) - SWAPTHRE.C */ # include <stdio.h> # include <conio.h> void main() { int a, b, c ; clrscr() ; printf("Enter two numbers : ") ; scanf("%d %d", &a, &b) ; printf("\nBefore swapping : \n\n") ; printf("a = %d \t b = %d", a, b) ; c = a ; a = b ; b = c ; printf("\n\nAfter swapping : \n\n") ; printf("a = %d \t b = %d", a, b) ; getch(); } RUN 1 : ~~~~~~~ Enter two numbers : 10 Before swapping : a = 10 b = 20 20

After swapping : a = 20 b = 10

B.Bhuvaneswaran A.3 /* Performing arithmetic operations using switch...case - ARITH.C */ #include<stdio.h> #include<conio.h> void main() { int n1, n2, ch ; clrscr() ; printf("Enter the first number : ") ; scanf("%d", &n1) ; printf("\nEnter the second number : ") ; scanf("%d", &n2) ; printf("\n[1] -> Addition ") ; printf("\n[2] -> Subtraction ") ; printf("\n[3] -> Multiplication ") ; printf("\n[4] -> Division ") ; printf("\n\nEnter your choice <1...4> : ") ; scanf("%d", &ch) ; switch(ch) { case 1 : printf("\n%d + %d = %d", n1, n2, n1 + n2) ; break ; case 2 : printf("\n%d - %d = %d", n1, n2, n1 - n2) ; break ; case 3 : printf("\n%d * %d = %d", n1, n2, n1 * n2); break ; case 4 : printf("\n%d / %d = %.2f", n1, n2, (float)n1 / n2); break ; default : printf("\nInvalid choice"); break ; } getch(); }

4> : 3 10 * 5 = 50 .A.4 Programs in C RUN 1 : ~~~~~~~ Enter the first number : 10 Enter the second number : 5 [1] [2] [3] [4] -> -> -> -> Addition Subtraction Multiplication Division Enter your choice <1...

h> void main() { int r. getch() .5 /* Program to print the multiplication table . printf("The multiplication table is :\n\n") . r = r + 1 . y) . do { y = r * c . clrscr() . printf("%5d".MULTABLE. do { c = 1 . c. c = c + 1 . r = 1 . } while(c <= 10) .B.Bhuvaneswaran A.h> # include <conio. printf("\n\n") . } while(r <= 10) .C */ # include <stdio. y . } RUN 1 : ~~~~~~~ The multiplication table is : 1 2 3 4 5 6 7 8 9 10 2 4 6 8 10 12 14 16 18 20 3 6 9 12 15 18 21 24 27 30 4 8 12 16 20 24 28 32 36 40 5 10 15 20 25 30 35 40 45 50 6 12 18 24 30 36 42 48 54 60 7 14 21 28 35 42 49 56 63 70 8 16 24 32 40 48 56 64 72 80 9 18 27 36 45 54 63 72 81 90 10 20 30 40 50 60 70 80 90 100 .

A.h> void main() { int rs. break . e. a. printf("\n\nThe no.h> # include <conio. g.6 Programs in C /* Finding the number of 500. of fifties are : %d". } while(rs >= 20) { d = rs / 20 . 1 . h . of hundreds are : %d". c) . clrscr() . printf("\n\nThe no. of five hundreds are : %d". while(rs >= 500) { a = rs / 500 . printf("\n\nThe no. 20. c. b. of twenties are : %d". of fives are : %d".C */ # include <stdio. f) . break . a) . } . b) . } while(rs >= 10) { e = rs / 10 . scanf("%d".RUPEE. 50. 5. 2. printf("\n\nThe no. printf("\n\nThe no. break . of tens are : %d". d) . d. } while(rs >= 5) { f = rs / 5 . e) . printf("\nThe no. printf("Enter the amount in Rupees : ") . } while(rs >= 50) { c = rs / 50 . break . } while(rs >= 100) { b = rs / 100 . break . f. &rs) . break . 100. 10.

7 while(rs >= 2) { g = rs / 2 . of ones are : %d". of twenties are : 8 The no. } getch() .B. break . printf("\n\nThe no. of fives are : 35 The no. } while(rs >= 1) { h = rs / 1 .Bhuvaneswaran A. of Twos are : %d". of ones are : 179 . of tens are : 17 The no. h) . of Twos are : 89 The no. of fifties are : 3 The no. printf("\n\nThe no. g) . of hundreds are : 1 The no. break . } RUN 1 : ~~~~~~~ Enter the amount in Rupees : 179 The no.

i.A.h> # include <conio. i++) printf("%4d + %4d = %4d\n". &n) . } RUN 1 : ~~~~~~~ Enter the table : 5 Enter the limit : 15 The table is : 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 + + + + + + + + + + + + + + + 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 = = = = = = = = = = = = = = = 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 . getch() . i <= n . printf("\nEnter the limit : ") . printf("\nThe table is :\n\n") .C */ # include <stdio.8 Programs in C /* Printing addition table of the given number . i + t) . scanf("%d". scanf("%d". t. n . &t) . t. printf("Enter the table : ") . for(i = 1 .ADDITION.h> void main() { int i. clrscr() .

i.h> void main() { int i. t.B.9 /* Program to print the multiplication table . printf("\nEnter the limit : ") . &n) . i++) printf("%4d x %4d = %4d\n". &t) .C */ # include <stdio.Bhuvaneswaran A. } RUN 1 : ~~~~~~~ Enter the table : 5 Enter the limit : 15 The table is : 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 x x x x x x x x x x x x x x x 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 = = = = = = = = = = = = = = = 5 10 15 20 25 30 35 40 45 50 55 60 65 70 75 . getch() . i * t) . printf("\nThe table is :\n\n") . printf("Enter the table : ") .h> # include <conio.MULTIPLY. clrscr() . t. for(i = 1 . scanf("%d". n . i <= n . scanf("%d".

c .BIGIFEL.. } else { if(c > b) printf("\n%d is the biggest number". c) . &a. if(a > b) { if(a > c) printf("\n%d is the biggest number". else printf("\n%d is the biggest number".h> void main() { int a. &c) . b. &b..10 Programs in C /* Finding the biggest of 3 numbers using if. else printf("\n%d is the biggest number". scanf("%d %d %d".h> # include <conio. a) . } getch() . c) . printf("Enter three numbers : ") .else .A.C */ # include <stdio. } RUN 1 : ~~~~~~~ Enter three numbers : 10 30 is the biggest number RUN 2 : ~~~~~~~ Enter three numbers : 20 30 is the biggest number RUN 3 : ~~~~~~~ Enter three numbers : 30 30 is the biggest number 10 20 30 10 20 30 . b) . clrscr() .

printf("\nThe biggest number is : %d". } RUN 1 : ~~~~~~~ Enter three numbers : 10 The biggest number is : 30 RUN 2 : ~~~~~~~ Enter three numbers : 20 The biggest number is : 30 RUN 3 : ~~~~~~~ Enter three numbers : 30 The biggest number is : 30 10 20 30 10 20 30 . &b. c.B.C */ # include <stdio. scanf("%d %d %d".11 /* Biggest of 3 numbers using ternary operator .h> void main() { int a. big . getch() . &c) . big = a > b ? (a > c ? a : c) : (b > c ? b : c) . clrscr() . &a.Bhuvaneswaran A. big) .h> # include <conio. printf("Enter three numbers : ") .BIGTER. b.

avg) . } RUN 1 : ~~~~~~~ Enter the limit : 5 Average of first 5 numbers is : 3. printf("\nAverage of first %d numbers is : %. printf("Enter the limit : ") . float sum = 0. scanf("%d". avg = sum / n .C */ # include <stdio.A. i++) sum = sum + i . i . getch() .12 Programs in C /* To find the average of first n natural numbers . clrscr() .00 .2f".h> # include <conio.h> void main() { int n. i <= n . &n) . avg . for(i = 1 .AVERAGE. n.

scanf("%d". clrscr() .Bhuvaneswaran A. } RUN 1 : ~~~~~~~ Enter a number : 179 The number of digits is : 3 . while(n > 0) { count++ .C */ # include <stdio. &n) .h> # include <conio. } printf("\nThe number of digits is : %d". printf("Enter a number : ") .13 /* To count the number of digits in an integer . getch() .B. count) .h> void main() { int n. count = 0 . n = n / 10 .DIGCOUNT.

h> # include <conio. s) . while(n > 0) { r = n % 10 .A.C */ # include <stdio. scanf("%d".DIGITSUM.14 Programs in C /* Program to find the sum of digits of an integer . printf("Enter a number : ") . } RUN 1 : ~~~~~~~ Enter a number : 12345 The sum of the digits is : 15 . s = s + r .h> void main() { int n. &n) . r. clrscr() . s = 0 . getch() . } printf("\nThe sum of the digits is : %d". n = n / 10 .

i++) if(i % d == 0) printf("%d\t". d) .h> void main() { int i. &d) .h> # include <conio. d .DIVIDE.15 /* Print the numbers that are divisible by a given no. printf("Enter the limit : ") . &n) . scanf("%d". i) . printf("\nEnter the number : ") . printf("\nThe numbers divisible by %d are :\n\n". scanf("%d". clrscr() . i <= n . getch() . .Bhuvaneswaran A. for(i = 1 .C */ # include <stdio.B. n. } RUN 1 : ~~~~~~~ Enter the limit : 15 Enter the number : 3 The numbers divisible by 3 are : 3 6 9 12 15 .

printf("\nThe divisors are :\n\n") . &n) . scanf("%d".16 Programs in C /* To print all the divisors of a given number . i <= n .A. i) .DIVISORS.C */ # include <stdio. i++) if(n % i == 0) printf("%d\t".h> void main() { int i.h> # include <conio. getch() . } RUN 1 : ~~~~~~~ Enter the number : 15 The divisors are : 1 3 5 15 . n . printf("Enter the number : ") . clrscr() . for(i = 1 .

scanf("%ld". } printf("\nThe reversed number is : %ld".h> void main() { long n. printf("Enter a number : ") . while(n > 0) { r = n % 10 . n = n / 10 . s = 0 . &n) . s) .h> # include <conio.17 /* Program for reversing an integer . s = r + s * 10 .B. getch() .Bhuvaneswaran A.REVERSE. } RUN 1 : ~~~~~~~ Enter a number : 2468 The Reversed Numeral is : 8642 . clrscr() .C */ # include <stdio. r.

clrscr() . printf("Enter the limit : ") . &n) .C */ # include <stdio.SUMODDEV.h> void main() { int n. i. osum) . for(i = 1 . esum = 0 . printf("\nThe odd numbers sum is : %d". for(i = 2 . scanf("%d".A. osum = 0.18 Programs in C /* Program to find the sum of odd and even numbers . i = i + 2) osum = osum + i . i <= n . getch() . printf("\n\nThe even numbers sum is : %d". esum) . } RUN 1 : ~~~~~~~ Enter the limit : 10 The odd numbers sum is : 25 The even numbers sum is : 30 . i <= n . i = i + 2) esum = esum + i .h> # include <conio.

c) . scanf("%d".C*/ # include <stdio. i <= n . b = c . clrscr() .h> void main() { int a = -1. &n) . } printf("\n\nThe sum of the fibonacci series is : %d". sum = sum + c . printf("\nThe fibonacci series is : \n\n") .SUMFIBO. i. sum) . getch() .h> # include <conio. } RUN 1 : ~~~~~~~ Enter the limit : 5 The fibonacci series is : 0 1 1 2 3 The sum of the fibonacci series is : 7 . printf("%d \t". i++) { c = a + b . a = b .B. n. printf("Enter the limit : ") . b = 1. c = 0.Bhuvaneswaran A. sum = 0 .19 /* Program to find the sum of fibonacci series . for(i = 1 .

"Wednesday") . 31. 30. strcpy(week[5]. strcpy(week[2]. strcpy(week[4]. 30. &mon. 31. 31}. strcpy(week[1]. &date. char week[7][10] .A. int date. "Friday") .h> void main() { int month[12] = {31.1 . 31. strcpy(week[3]. for(i = 0 . "Tuesday") . printf("Enter a valid date (dd/mm/yyyy) : ") .C */ # include <stdio. week[s]) . s = s + (date + year + (year / 4) . i < mon .20 Programs in C /* Program to find the day of the given date . "Saturday") . i++) s = s + month[i] . getch() . "Sunday") . mon. printf("\nThe day is : %s". clrscr(). r. "Monday") . 30. if( (year / 4 == 0) && (year % 400 == 0) && (year % 100 != 0) ) month[1] = 29 . 31.2) . strcpy(week[0]. year. 30. 31. strcpy(week[6]. } RUN 1 : ~~~~~~~ Enter a valid date (dd/mm/yyyy) : 02/11/1977 The day is : Wednesday . "Thursday") . &year) . i. s = 0 . 28. s = s % 7 . scanf("%d / %d / %d".h> # include <conio.DAY.

C */ # include <stdio.Bhuvaneswaran A.h> # include <conio. if(num % 2 == 0) printf("\nThe given number is even") .21 Condition Checking Programs /* To find whether the given number is even or odd . clrscr() . } RUN 1 : ~~~~~~~ Enter a number : 10 The given number is even RUN 2 : ~~~~~~~ Enter a number : 5 The given number is odd .CHKODDEV. &num) . printf("Enter a number : ") .B. getch() .h> void main() { int num . else printf("\nThe given number is odd") . scanf("%d".

&age) . clrscr() . else printf("\nThe person is not in teen age. scanf("%d". RUN 2 : ~~~~~~~ Enter the age : 21 The person is not in teen age. .C */ # include <stdio.22 Programs in C /* To check whether the person is in teen age or not .CHKTEEN. if(age >= 13 && age <= 19) printf("\nThe person is in teen age. printf("Enter the age : ") .") . getch() .") .h> # include <conio.h> void main() { int age . } RUN 1 : ~~~~~~~ Enter the age : 16 The person is in teen age.A.

C */ # include <stdio.B. clrscr() . . &age) . RUN 2 : ~~~~~~~ Enter the age : 16 The person is not eligible to vote. if(age >= 18) printf("\nThe person is eligible to vote.Bhuvaneswaran A.h> void main() { int age . else printf("\nThe person is not eligible to vote.23 /* Check whether the person is eligible to vote or not . scanf("%d".") . } RUN 1 : ~~~~~~~ Enter the age : 20 The person is eligible to vote.CHKVOTE. getch() .h> # include <conio. printf("Enter the age : ") .") .

clrscr() . printf("Enter a number : ") .A. getch() . } if (s == n) printf("\n%d is a perfect number".h> # include <math.C */ # include <stdio.24 Programs in C /* To find the given no.h> # include <conio. else printf("\n%d is not a perfect number". i++) { if(n % i == 0) s = s + i . n) . s = 0 .CHKPERNO. is perfect no. for(i = 1 . or not . &n) . n.h> void main() { int i. } RUN 1 : ~~~~~~~ Enter a number : 6 6 is a perfect number RUN 2 : ~~~~~~~ Enter a number : 28 28 is a perfect number RUN 3 : ~~~~~~~ Enter a number : 12 12 is not a perfect number . scanf("%d". n) . i < n .

n) .h> # include <conio. float p .B. n) . is perfect square or not .h> void main() { int m. p = sqrt(n) . n . scanf("%d". m = p . &n) . clrscr() .CHKPERSQ.Bhuvaneswaran A.h> # include <math. if (p == m) printf("\n%d is a perfect square". else printf("\n%d is not a perfect square".C */ # include <stdio.25 /* To find the given no. } RUN 1 : ~~~~~~~ Enter a number : 81 81 is a perfect square RUN 2 : ~~~~~~~ Enter a number : 12 12 is not a perfect square . printf("Enter a number : ") . getch() .

else { for(i = 2 .C */ # include <stdio. printf("Enter the Number : ") . break . is prime or not .A. i <= n / 2 . if(n <= 3) flag = 0 . n. } RUN 1 : ~~~~~~~ Enter the Number : 6 The number is not prime RUN 2 : ~~~~~~~ Enter the Number : 11 The number is prime . flag = 0 . else printf("\nThe number is prime") .26 Programs in C /* To check whether the given no. } } if(flag == 1) printf("\nThe number is not prime") . getch() . i++) if(n % i == 0) { flag = 1 . clrscr() .CHKPRIME.h> void main() { int i. &n) . scanf("%d".h> # include <conio.

} RUN 1 : ~~~~~~~ Enter a number : 153 153 is an armstrong number RUN 2 : ~~~~~~~ Enter a number : 123 123 is not an armstrong number . &n) . printf("Enter a number : ") . while(n > 0) { r = n % 10 .h> void main() { int n.B. t) . clrscr() . t) . s = 0. s = s + (r * r * r) . n = n / 10 .CHKARMST. scanf("%d". } if(s == t) printf("\n%d is an armstrong number". getch() .C */ # include <stdio. else printf("\n%d is not an armstrong number".27 /* To find the given number is armstrong or not . t . r.Bhuvaneswaran A. t = n .h> # include <conio.

n .A. clrscr() . i++) { if(a[i] > 0) printf("%d\t". i++) scanf("%d".h> # include <conio. for(i = 0 . i < n . } getch() .h> void main() { int a[20]. &n) . printf("\nThe positive elements are :\n\n") .ARRPOS. printf("\nEnter the elements :\n\n") .28 Programs in C Arrays Programs /* To display only the positive elements of the array . a[i]) . &a[i]) . for(i = 0 . i < n . printf("Enter the limit : ") . scanf("%d". i.C */ # include <stdio. } RUN 1 : ~~~~~~~ Enter the limit : 5 Enter the elements : 10 -20 30 -40 -50 The positive elements are : 10 30 .

n . &n) . i++) { if(a[i] < 0) printf("%d\t". i++) scanf("%d".h> void main() { int a[20]. &a[i]) . i < n . printf("\nThe negative elements are :\n\n") . } RUN 1 : ~~~~~~~ Enter the limit : 5 Enter the elements : 10 -20 30 -40 -50 The negative elements are : -20 -40 -50 . } getch() .C */ # include <stdio. i.Bhuvaneswaran A. scanf("%d".h> # include <conio. printf("\nEnter the elements :\n\n") . printf("Enter the limit : ") . for(i = 0 . for(i = 0 . a[i]) . i < n .ARRNEG.29 /* To display only the negative elements of the array .B. clrscr() .

psum = 0. i++) scanf("%d".30 Programs in C /* To find the sum of +VE & -VE elements in an array . psum) . i++) { if(a[i] > 0) psum = psum + a[i] .h> void main() { int a[20]. for(i = 0 . i < n . &n) .C */ # include <stdio. printf("\nEnter the elements :\n\n") . printf("Enter the limit : ") . } printf("\nSum of positive elements is : %d".A. getch() .h> # include <conio. nsum = 0 . } RUN 1 : ~~~~~~~ Enter the limit : 5 Enter the elements : -10 30 50 -20 40 Sum of positive elements is : 120 Sum of negative elements is : -30 . &a[i]) . printf("\n\nSum of negative elements is : %d". scanf("%d". nsum) . i. clrscr() . for(i = 0 . n.ARPOSNEG. if(a[i] < 0) nsum = nsum + a[i] . i < n .

for(i = 0 . printf("\nThe reversed array is :\n\n") .C */ # include <stdio. printf("Enter the limit : ") . i++) scanf("%d".ARRREV. i. n . i--) printf("%d\t".1 .31 /* Program to read and reverse an array . i >= 0 .h> void main() { int a[20]. &n) . clrscr() . a[i]) . &a[i]) . printf("\nEnter the elements :\n\n") .h> # include <conio. } RUN 1 : ~~~~~~~ Enter the limit : 5 Enter the elements : 20 30 50 10 40 The reversed array is : 40 10 50 30 20 . scanf("%d". i < n . for(i = n .Bhuvaneswaran A.B. getch() .

i++) f = f * i . getch() .FACT. } RUN 1 : ~~~~~~~ Enter a number : 5 Factorial value of 5 is : 120 . f) . printf("Enter a number : ") . scanf("%ld".h> # include <conio. f = 1 . printf("\nFactorial value of %ld is : %ld".h> void main() { long n.A. i. n.C */ # include <stdio. i <= n . for(i = 1 . clrscr() . &n) .32 Programs in C Values Computing Programs /* Program to find the factorial of a given number .

int n.33 /* Program to find NCR value of the given numbers .Bhuvaneswaran A. scanf("%d". &n) . printf("\nThe NCR value is : %ld". ncr . if(i == 0) return 1 . scanf("%d". return x . getch() . clrscr() . printf("\nEnter the value for R : ") .NCR. else { x = i * fact(i . } } RUN 1 : ~~~~~~~ Enter the value for N : 7 Enter the value for R : 5 The NCR value is : 21 RUN 2 : ~~~~~~~ Enter the value for N : 5 Enter the value for R : 7 Calculating NCR value is not possible . r . if(n >= r) { ncr = fact(n) / (fact(n-r) * fact(r)) . } long fact(int i) { long x . &r) . ncr) .1) . void main() { long i.h> long fact(int) .C */ # include <stdio.B. printf("Enter the value for N : ") .h> # include <conio. } else printf("\nCalculating NCR value is not possible") .

&x) . count ++ . printf("Enter the value of x : ") .A.34 Programs in C /* Program to find the value of x^n . scanf("%d". } RUN 1 : ~~~~~~~ Enter the value of x : 2 Enter the value of n : 4 The power of 2^4 is : 16 . n.h> # include <conio. count = 1. while(count <= n) { sum = sum * x . sum) . &n) . clrscr() .h> void main() { int x. printf("\nEnter the value of n : ") .POWER. n. } printf("\nThe power of %d^%d is : %d". x.C */ # include <stdio. scanf("%d". sum = 1 . getch() .

lcm).n1 . printf("\nThe GCD is : %d". } RUN 1 : ~~~~~~~ Enter the two numbers : 10 The GCD is : 2 The LCM is : 40 8 .C */ # include <stdio. while(n1 != n2) { if(n1 > n2) n1 = n1 .h> # include <conio.B. prod.Bhuvaneswaran A. printf("Enter the two numbers : ") . gcd) . if(n2 > n1) n2 = n2 . printf("\n\nThe LCM is : %d". clrscr() . lcm . &n2) . gcd. scanf("%d %d".LCMGCD.h> void main() { int n1.n2 . } gcd = n1 .35 /* Program to find LCM and GCD of the given two numbers . &n1. n2. getch() . prod = n1 * n2 . lcm = prod / gcd .

clrscr() . printf("Enter the values of A. b. imag) . case 3 : printf("\nRoots are real and unequal\n") . } } else printf("\nEquation is linear") . B & C : ") . c.2f . imag) .A. real. } . real = .QUADRAT. if(d > 0) k = 3.r1) . case 2 : printf("\nRoots are real and equal\n") .2f".C */ # include <stdio.2f".sqrt((double) d)) / (2 * a) .d . break .2f".b . n = pow((double) d. printf("\nr1 = r2 = %7. &b. printf("\nr1 = %7.b / (2 * a) .h> # include <math.4 * a * c . break .b + sqrt((double) d)) / (2 * a) . if(a != 0) { d = b * b . scanf("%f %f %f".2f + j%7. r1 = (.h> # include <conio.h> void main() { float a. printf("\nr2 = %7. &c) . if(d < 0) k = 1 . imag. real.5) . d = . real. &a.r2) . r2. getch() . printf("\nr1 = %7. r1. n .j%7. r1) .36 Programs in C /* Program to find the roots of a quadratic equation . int k .b / (2 * a) . printf("\nr2 = %7. if(d == 0) k = 2 . break . r2 = (. (double) 0. switch(k) { case 1 : printf("\nRoots are imaginary\n") .2f". r1 = . d.2f". imag = n / (2 * a) .

45 2.0 1.Bhuvaneswaran A.0 7. B & C : 1.0 7.0 4.45 2.00 2.00 + j -1. B & C : 0.0 Equation is linear RUN 2 : ~~~~~~~ Enter the values of A.0 Roots are real and unequal r1 = r2 = -0.0 RUN 3 : ~~~~~~~ Enter the values of A. B & C : 1.0 1.15 -3. B & C : 2.0 Roots are imaginary r1 = r2 = -1.37 RUN 1 : ~~~~~~~ Enter the values of A.0 -1.B.0 .0 Roots are real and equal r1 = r2 = RUN 4 : ~~~~~~~ Enter the Values of A.00 .j 2.35 7.

j + 1) . i++) { p = a[i][k] / a[k][k] .38 Programs in C /* Simultaneous equation using gauss elimination method . } } x[n-1] = a[n-1][n] / a[n-1][n-1] . &n) . i + 1. } for(k = 0 . i < n .1 . p . n . float a[20][20]. &a[i][j]) . } . double s. for(i = 0 . printf("Enter the number of equations : ") . for(j = k . j++) { s += (a[i][j] * x[j]) . x[i]) . i < n . for(i = 0 . i + 1. j < n . k. scanf("%d". i + 1) . j++) a[i][j] = a[i][j] . j. i < n . } } printf("\nThe result is :\n") .2 . i >= 0 . x[20] . j < n . for(j = i + 1 . i++) printf("\nx[%d] = %. getch() .h> void main() { int i. } printf("b[%d] = ".GAUSS.A.C */ # include <stdio. clrscr() . scanf("%f".h> # include <conio. k++) { for(i = k + 1 . i++) { for(j = 0 . k < n . j++) { printf("a[%d][%d] = ".2f".p * a[k][j] . i--) { s = 0 . x[i] = (a[i][n] . printf("\nEnter the co-efficients of the equations :\n\n") . scanf("%f". for(i = n .s) / a[i][i] . &a[i][n]) . j < n + 1 .

00 .00 x[2] = 1.00 x[3] = 1.B.39 RUN 1 : ~~~~~~~ Enter the number of equations : 3 Enter the co-efficients of the equations : a[1][1] = a[1][2] = a[1][3] = b[1] = 12 a[2][1] = a[2][2] = a[2][3] = b[2] = 13 a[3][1] = a[3][2] = a[3][3] = b[3] = 7 10 1 1 2 10 1 1 1 5 The result is : x[1] = 1.Bhuvaneswaran A.

printf("Enter the centigrade : ") . &c) .40 Programs in C Conversion Programs /* To convert temperature in centigrade to farenheit .2f".8 * c + 32) .h> # include <conio. printf("\nThe farenheit is : %. scanf("%f". f) .00 .A.h> void main() { float c.C */ # include <stdio. f .CTOF. clrscr() . f = (1. getch() . } RUN 1 : ~~~~~~~ Enter the centigrade : 45 The farenheit is : 113.

c = (f . printf("\nThe centigrade is : %.Bhuvaneswaran A.h> # include <conio.00 . clrscr() .32) / 1.B. } RUN 1 : ~~~~~~~ Enter the farenheit : 113 The centigrade is : 45. c) . &f) .FTOC. f .C */ # include <stdio.8 . getch() . printf("Enter the farenheit : ") .2f".h> void main() { float c.41 /* To convert temperature in farenheit to centigrade . scanf("%f".

} getch() . case 5 : printf("\nFIVE") . case 8 : printf("\nEIGHT") .INTTOCH. clrscr() .h> # include <conio. break . break . case 3 : printf("\nTHREE") . scanf("%d". break . break . break . case 2 : printf("\nTWO") . case 10 : printf("\nTEN") .") . &n) . break . break . break .C */ # include <stdio. } . break . switch(n) { case 1 : printf("\nONE") . case 7 : printf("\nSEVEN") . default : printf("\nInvalid Input. printf("Enter a number <1 to 10> : ") . case 9 : printf("\nNINE") .h> void main() { int n . break . case 6 : printf("\nSIX") .42 Programs in C /* To convert the given number (1 to 10) to characters .A. case 4 : printf("\nFOUR") .

B.43 RUN 1 : ~~~~~~~ Enter a number <1 to 10> : 7 SEVEN .Bhuvaneswaran A.

break . i >= 0 . &n) . break . printf("Enter a number : ") . case 2 : printf("TWO ") . c++ . break . . case 5 : printf("FIVE ") . for(i = c . } printf("\n") . break .1 . i--) { switch(a[i]) { case 0 : printf("ZERO ") . case 4 : printf("FOUR ") . n = n / 10 .h> void main() { long n. while(n > 0) { a[c] = n % 10 . clrscr() .C */ # include <stdio.A. break . break . break . case 8 : printf("EIGHT ") . break . i. case 1 : printf("ONE ") . break . case 7 : printf("SEVEN ") . a[10]. scanf("%ld".44 Programs in C /* Program to change an integer to words . case 3 : printf("THREE ") .h> # include <conio.NUMTOWD. c = 0 . case 6 : printf("SIX ") .

B.Bhuvaneswaran A. } RUN 1 : ~~~~~~~ Enter a number : 225589 TWO TWO FIVE FIVE EIGHT NINE .45 case 9 : printf("NINE ") . break . } } getch() .

} RUN 1 : ~~~~~~~ Enter a decimal number : 12 The binary equivalent is : 1100 .A. getch() . scanf("%ld". r. &n) .h> # include <conio. i . b[i]) . for(i = c . } printf("\nThe binary equivalent is : "). printf("Enter a decimal number : ") .C */ # include <stdio. while(n > 0) { r = n % 2 .1 . n. b[c] = r . c++ . i >= 0 . c = 0.46 Programs in C /* To convert a decimal number to a binary number . clrscr() . n = n / 2 .DECTOBIN. i--) printf("%ld".h> void main() { long b[20].

long int n .47 /* To convert a binary number to a decimal number .B. j ++ . sum = sum + i * pow(2. printf("Enter the binary number : ") .h> void main() { int i = 0. getch() . if(i == 0 || i == 1) { while(n != 0) { i = n % 10 . sum = 0 . j = 0. } } } if(sum == 0) printf("\nThe number is not a binary number") . j) . scanf("%ld". clrscr() .h> # include <math.h> # include <conio. sum) .BINTODEC.Bhuvaneswaran A.C */ # include <stdio. if(n != 0) { i = n % 10 . else printf("\nThe equivalent decimal number is : %d". n = n / 10 . &n) . } RUN 1 : ~~~~~~~ Enter the binary number : 1100 The equivalent decimal number is : 12 RUN 2 : ~~~~~~~ Enter the binary number : 15 The number is not a binary number .

for(i = 1 .1)) . val. &n) . clrscr() . sum = sum + t . getch() . t = 1 .h> # include <math. i < n + 1 .14159 / 180 . sum = 1. (double) (2 * i .h> # include <conio.5000 . printf("\nEnter the value for n : ") .000000 is : 0. val. scanf("%d".A. x = x * 3.1)) * x * x / (2 * i * (2 * i . sum) . n .C */ # include <stdio.48 Programs in C Series Calculation Programs /* Program to calculate the cosine series . printf("Enter the value for x : ") . float x.CALCOS. scanf("%f".h> void main() { int i. &x) . } printf("\nCosine value of %f is : %8. } RUN 1 : ~~~~~~~ Enter the value for x : 60 Enter the value for n : 20 Cosine value of 60. i++) { t = t * pow((double) (-1).4f". val = x .

&x) . sum = sum + t . } RUN 1 : ~~~~~~~ Enter the value for x : 30 Enter the value for n : 20 Sine value of 30. float x. printf("Enter the value for x : ") . } printf("\nSine value of %f is : %8.14159 / 180 . getch() . x = x * 3. for(i = 1 . t = x . val. &n) .B. n .1)) * x * x) / (2 * i * (2 * i + 1)) . i < n + 1 .CALSIN. scanf("%f".Bhuvaneswaran A.4f". clrscr() . sum. t . scanf("%d". sum) .49 /* Program to calculate the sine series . sum = x . i++) { t = (t * pow((double) (-1). val = x .h> # include <math.h> # include <conio. (double) (2 * i .000000 is : 0. printf("\nEnter the value for n : ") .h> void main() { int i. val.C */ # include <stdio.5000 .

A. sum = 1. &n) . i < n + 1 . scanf("%f".4f". i++) { exp = i . &x) . float x. for(i = 1 .50 Programs in C /* Program to calculate the exponential series . } RUN 1 : ~~~~~~~ Enter the value for x : 2 Enter the value for n : 20 Exponential Value of 2.C */ # include <stdio. scanf("%d". x.000000 is : 7. exp.3891 . getch() .h> # include <conio.h> void main() { int i. clrscr() . printf("\nEnter the value for n : ") . } printf("\nExponential Value of %f is : %8. sum = sum + t . n. t = 1 .CALEXP. t = t * x / exp . printf("Enter the value for x : ") . sum) .

i++) { for(j = 0 .h> # include <conio. i. &r) . for(i = 0 . c++ .51 Series Generation Programs /* Program to generate Floyd's triangle . printf("Enter the number of rows : ") . } printf("\n\n") . c) .B. j.FLOYDS. } RUN 1 : ~~~~~~~ Enter the number of rows : 5 Floyd's triangle is : 1 2 4 7 11 3 5 8 12 6 9 13 10 14 15 .h> void main() { int r. i < r . j++) { printf("%-6d". clrscr() . c = 1 . j <= i . } getch() .C */ # include <stdio.Bhuvaneswaran A. printf("\nFloyd's triangle is : \n\n") . scanf("%d".

x + 1)) / x . x . } printf("\n\n") .h> # include <conio.A. ++x) { if((x == 0) || (q == 0)) b = 1 . for(x = 0 . else b = (b * (q . printf("Enter the number of rows : ") . scanf("%d". while (q < p) { for(r = 40 . ++q . } getch() .h> void main() { int b.52 Programs in C /* Program to generate Pascal's triangle . } RUN 1 : ~~~~~~~ Enter the number of rows : 5 Pascal's triangle is : 1 1 1 1 1 4 3 6 2 3 4 1 1 1 1 .3 * q .C */ # include <stdio. clrscr() . b = 1 . &p) . q. b) .PASCALS. printf("%6d". --r) printf(" ") . x <= q . r. q = 0 . r > 0 . printf("\nPascal's triangle is : \n\n") . p.

....-") .. sin(r)... i = i + 30) { r = i * 3..58 180 0.... printf("\nAngle \t Sin \t Cos \t Tan \n") ..00 ..87 1........-") ..73 788898.50 -0.00 0. cos(r). } RUN 1 : ~~~~~~~ .0 0.. printf("\n%3d \t %5..73 -0..2f \t %5.... tan(r)). int i ..50 0.. i <= 180 ..........00 -0.GENTRIG */ # include <stdio.h> # include <math.- ........h> # include <conio..87 0.....00 0. char ch .2f \t %5.2f\n".58 1.Bhuvaneswaran A.......h> void main() { float r ........00 -1. i.....00 1.87 0....... for(i = 0 .53 /* Program to generate Trigonometric Table . clrscr() .00 -0..-") . printf("...50 0... } printf(".87 0...14159 / 180 .....50 0.. printf("......00 30 60 90 120 150 0..12 -1......B. getch() .Angle Sin Cos Tan .

printf("Enter the string : ") .h> # include <conio. n) . a) . int k. if(k == n) { printf("\n%s".h> # include <string. clrscr() . perm(a.C */ # include <stdio.h> void main() { int n. void perm(char a[10]. a) . i. } } } . int n) { char t. a) . scanf("%s". int k. i < n . k. a[i] = a[k] . char a[10] . n) . getch() . int n) . return . n = strlen(a) . d[10] .GENPERM. i++) { t = a[i] .A. perm(d. a[k] = t . strcpy(d. k = 0 . int i . k + 1. printf("\nThe permutation is :\n") .54 Programs in C /* Program to generate permutation . } else { for(i = k . } void perm(char a[10].

B.Bhuvaneswaran A.55 RUN 1 : ~~~~~~~ Enter the string : abc The permutation is : abc acb bac bca cab cba .

printf("Enter the size of the magic square : ") . i <= n . } for (i = 1 . if (n % 2 == 0) { printf("\nMagic square is not possible") . j++) { printf("%d\t". c++) { a[i][j] = c . j.C */ # include<stdio. j <= n . } if(i == 1) i = n . i = 1 .GENMAGIC. j = (n + 1) / 2 . if(c % n == 0) { i = i + 1 . else j = j + 1 . c. } printf("\nThe magic square for %d x %d is :\n\n". if(j == n) j = 1. &n) . a[9][9] . } printf("\n\n") . i++) { for (j = 1 . } end : . getch() . c <= n * n .h> void main() { int n. i. scanf("%d". n) .A. goto loop .1 . n. loop : . goto end . else i = i . } . for(c = 1 . a[i][j]) .56 Programs in C /* Program to generate magic square . clrscr() .

B.Bhuvaneswaran A.57 RUN 1 : ~~~~~~~ Enter the size of the magic square : 3 The magic square for 3 x 3 is : 8 3 4 RUN 2 : ~~~~~~~ Enter the size of the magic square : 4 Magic square is not possible 1 5 9 6 7 2 .

i <= n . i) .h> # include <conio.C */ # include <stdio. i = i + 2) printf("%d\t". printf("Enter the limit : ") . } RUN 1 : ~~~~~~~ Enter the limit : 10 The odd numbers are : 1 3 5 7 9 The even numbers are : 2 4 6 8 10 . &n) .GENODDEV. printf("\n\nThe even numbers are :\n\n") . for(i = 2 .A.h> void main() { int n. i <= n . scanf("%d". clrscr() . i) . getch() . for(i = 1 . i .58 Programs in C /* Program to generate odd and even numbers . i = i + 2) printf("%d\t". printf("\nThe odd numbers are :\n\n") .

} RUN 1 : ~~~~~~~ Enter the limit : 7 The fibonacci series is : 0 1 1 2 3 5 8 . printf("Enter the limit : ") .h> void main() { int a = -1. i. printf("%d \t". b = c . a = b . i++) { c = a + b .C*/ # include <stdio. clrscr() . printf("\nThe fibonacci series is : \n\n") . } getch() . n .Bhuvaneswaran A. &n) . scanf("%d". c) . i <= n .h> # include <conio. c = 0.B. b = 1.GENFIBO. for(i = 1 .59 /* Program to generete fibonacci series .

scanf("%d". clrscr() . printf("\nThe prime numbers are :\n\n") . } RUN 1 : ~~~~~~~ Enter the limit : 10 The prime numbers are : 1 2 3 5 7 .60 Programs in C /* Program to generate prime numbers . &n) .A.h> void main() { int i.GENPRIME. loop : . n . j ++) { if (i % j == 0) goto loop . i <= n . j. i) . j <= i / 2 . printf("Enter the limit : ") .h> # include <conio.C */ # include <stdio. } } getch() . } else { for (j = 2. i) . for (i = 1 . i++) { if(i <= 3) { printf("%d\t". } printf("%d\t".

s. s = 0 . i <= n . i++) { a = i . for(i = 0 . a. clrscr() . while(a > 0) { r = a % 10 .h> # include <conio. n . printf("Enter the limit : ") . } getch() .GENARMST. scanf("%d".Bhuvaneswaran A.h> void main() { int i. printf("\nThe armstrong numbers are :\n\n") . } RUN 1 : ~~~~~~~ Enter the limit : 1000 The armstrong numbers are : 0 1 153 370 371 407 . r.B. s = s + (r * r * r) .61 /* Program to generate armstrong numbers . a = a / 10 .C */ # include <stdio. } if(i == s) printf("%d\t". &n) . i) .

C */ # include <stdio. sum) . getch() . i++) for(j = 0 . printf("\nThe sum of the elements in the matrix is : %d". for(i = 0 . row.h> # include <conio. for(i = 0 . i < row . col.MATSUM. &row. &col) . clrscr() . sum = 0 . j < col . i < row . int i. j++) sum = sum + mat[i][j] .A. j++) scanf("%d". printf("Enter the order of the matrix : ") . j. printf("\nEnter the elements of the matrix : \n\n") .62 Programs in C Matrix Manipulation Programs /* To find sum of all the elements of the given matrix . &mat[i][j]) . i++) for(j = 0 .h> void main() { int mat[10][10] . j < col . scanf("%d %d". } RUN 1 : ~~~~~~~ Enter the order of the matrix : 3 Enter the elements of the matrix : 1 4 7 2 5 8 3 6 9 3 The sum of the elements in the matrix is : 45 .

C */ # include <stdio. printf("\nEnter the elements of the matrix : \n\n") . printf("\nThe sum of diagonal elements in the matrix is : %d". scanf("%d". clrscr() .h> void main() { int mat[10][10] . } RUN 1 : ~~~~~~~ Enter size of the square matrix : 3 Enter the elements of the matrix : 1 4 7 2 5 8 3 6 9 The sum of diagonal elements in the matrix is : 15 . j < size . getch() . int i. &mat[i][j]) . i < size .63 /* Find sum of diagonal elements of the given matrix . sum) . printf("Enter size of the square matrix : ") . j. size. i++) sum = sum + mat[i][i] .MATDIAG. i < size . for(i = 0 . sum = 0 .Bhuvaneswaran A. j++) scanf("%d".B. i++) for(j = 0 . &size) .h> # include <conio. for(i = 0 .

int i. : %d". } } printf("\nThe smallest element in the matrix printf("The biggest element in the matrix is getch() . j. &col) . printf("Enter the order of the matrix : ") . is : %d\n\n". small. &mat[i][j]) .64 Programs in C /* Find smallest & biggest elements of the given # include <stdio. big . j++) { if(mat[i][j] < small) small = mat[i][j] . The smallest element in the matrix is : 1 The biggest element in the matrix is : 9 . i++) { for(j = 0 . j++) scanf("%d". big) . } RUN 1 : ~~~~~~~ Enter the order of the matrix : 3 Enter the elements of the matrix : 4 9 1 5 7 2 6 8 3 3 matrix . col. i < row . for(i = 0 .C */ \n\n") . j < col . i++) for(j = 0 . small = mat[0][0] .A.h> void main() { int mat[10][10] . big = mat[0][0] . j < col . scanf("%d %d". &row.h> # include <conio. row.small).MATNUM. i < row . clrscr() . printf("\nEnter the elements of the matrix : for(i = 0 . if(mat[i][j] > big) big = mat[i][j] .

i++) { for(j = 0 .upper). } printf("\nThe lower triangular matrix is : \n\n") . i < size . i++) { for(j = 0 . lower) . j++) { if(j <= i) { printf("%d\t". &size) .C */ .h> void main() { int mat[10][10] . } else printf("\t") . j < size . lower = lower + mat[i][j] . &mat[i][j]) . for(i = 0 . size. j < size . i++) for(j = 0 .65 /* Find the sum of upper & lower traiangular elements # include <stdio. scanf("%d". lower = 0 . . int i. } printf("\n") . upper = upper + mat[i][j] . %d". printf("\nThe upper triangular matrix is : \n\n") . i < size . } else printf("\t") . upper = 0. } printf("\nThe sum of upper triangular elements is : printf("\nThe sum of lower triangular elements is : getch() . j. j++) scanf("%d". } printf("\n") . i < size . printf("Enter size of the square matrix : ") . printf("\nEnter the elements of the matrix : \n\n") for(i = 0 .Bhuvaneswaran A. for(i = 0 . %d\n". mat[i][j]) . mat[i][j]) . } MATUPLOW. clrscr() . j < size .h> # include <conio.B. j++) { if(i <= j) { printf("%d\t".

A.66 Programs in C RUN 1 : ~~~~~~~ Enter size of the square matrix : 3 Enter the elements of the matrix : 1 4 7 2 5 8 3 6 9 The upper triangular matrix is : 1 2 5 3 6 9 The lower triangular matrix is : 1 4 7 5 8 9 The sum of upper triangular elements is : 26 The sum of lower triangular elements is : 34 .

MATUNIT.h> # include <conio. clrscr() . &size) . j < size . for(i = 0 . } } if(flag == 1) printf("\nThe matrix is an unit matrix") .h> void main() { int mat[10][10] . getch() . if(i != j) if(mat[i][j] != 0) flag = 0 .Bhuvaneswaran A. i++) for(j = 0 . int i. scanf("%d".B. j. printf("\nEnter the elements of the matrix : \n\n") . i++) { for(j = 0 . size. flag = 1 . j < size . j++) { if(i == j) if(mat[i][j] != 1) flag = 0 . &mat[i][j]) . i < size .67 /* To find the given matrix is a unit matrix or not . printf("Enter size of the square matrix : ") . j++) scanf("%d". else printf("\nThe matrix is not an unit matrix") .C */ # include <stdio. for(i = 0 . } RUN 1 : ~~~~~~~ Enter size of the square matrix : 3 Enter the elements of the matrix : 1 0 0 0 1 0 0 0 1 The matrix is an unit matrix . i < size .

A.68 Programs in C RUN 2 : ~~~~~~~ Enter size of the square matrix : 3 Enter the elements of the matrix : 1 4 6 2 1 7 3 5 1 The matrix is not an unit matrix .

&mat[i][j]) . j++) { printf("%d \t". j++) scanf("%d". row.B. i++) for(j = 0 . scanf("%d %d". i++) { for(j = 0 . printf("Enter the order of the matrix : ") . } RUN 1 : ~~~~~~~ Enter the order of the matrix : 2 Enter the elements of the matrix : 1 4 2 5 3 6 3 The transpose matrix is : 1 2 3 4 5 6 . int i.C */ # include <stdio.h> void main() { int mat[10][10] . for(i = 0 . printf("\nEnter the elements of the matrix : \n\n") . } printf("\n") . for(i = 0 . j.69 /* Program to transpose the given matrix .MATTRANS.Bhuvaneswaran A. &col) . printf("\nThe transpose matrix is : \n\n") . mat[j][i]) . i < col . j < col . } getch() . &row. col . j < row . i < row .h> # include <conio. clrscr() .

scanf("%d %d". i < row .C */ # include <stdio. j++) matc[i][j] = mata[i][j] + matb[i][j] .h> void main() { int mata[10][10]. for(i = 0 . row. matc[i][j]) . col . for(i = 0 . &mata[i][j]) . i < row .MATADD. printf("Enter the order of the matrix : ") . j++) scanf("%d". printf("\nThe resultant matrix is : \n\n") . printf("\nEnter the elements of second matrix : \n\n") . matb[10][10]. j. int i. j < col . i++) for(j = 0 . i++) { for(j = 0 .A. } getch() . i++) for(j = 0 .70 Programs in C /* Program to add the given two matrices . for(i = 0 . j < col . printf("\nEnter the elements of first matrix : \n\n") . j < col . for(i = 0 . &row. i++) for(j = 0 . j++) { printf("%d \t". j < col . i < row .h> # include <conio. i < row . } printf("\n") . } . clrscr() . matc[10][10] . &matb[i][j]) . &col) . j++) scanf("%d".

71 RUN 1 : ~~~~~~~ Enter the order of the matrix : 3 Enter the elements of first matrix : 1 7 13 3 9 15 5 11 17 3 Enter the elements of second matrix : 2 8 14 4 10 16 6 12 18 The resultant matrix is : 3 15 27 7 19 31 11 23 35 .B.Bhuvaneswaran A.

matc[i][j]) . i < row . j < col . j < col . i++) for(j = 0 . int i.C */ # include <stdio. printf("\nEnter the elements of first matrix : \n\n") . &matb[i][j]) . printf("Enter the order of the matrix : ") . matc[10][10] . j++) { printf("%d \t". i < row . j < col . printf("\nThe resultant matrix is : \n\n") . j++) scanf("%d". i++) for(j = 0 .MATSUB. i < row .h> # include <conio. col .72 Programs in C /* Program to subtract the given two matrices . j. printf("\nEnter the elements of second matrix : \n\n") . clrscr() . } printf("\n") . j < col . i++) { for(j = 0 . i++) for(j = 0 . j++) scanf("%d". } . &col) .matb[i][j] . j++) matc[i][j] = mata[i][j] . for(i = 0 . &mata[i][j]) . matb[10][10]. i < row . row. } getch() . &row.A. for(i = 0 . for(i = 0 .h> void main() { int mata[10][10]. scanf("%d %d". for(i = 0 .

B.Bhuvaneswaran A.73 RUN 1 : ~~~~~~~ Enter the order of the matrix : 3 Enter the elements of first matrix : 5 20 35 10 25 40 15 30 45 3 Enter the elements of second matrix : 2 8 14 4 10 16 6 12 18 The resultant matrix is : 3 12 21 6 15 24 9 18 27 .

h> # include <conio.C */ # include <stdio. j++) { matc[i][j] = 0 . i++) for(j = 0 .74 Programs in C /* Program to multiply the given two matrices . i++) { for(j = 0 . j++) scanf("%d". for(i = 0 . if(col1 == row2) { printf("\nEnter the elements of first matrix : \n\n") . i < row1 . printf("Enter the order of first matrix : ") . matc[10][10] . &col2) . &matb[i][j]) . row1. col2 . j < col2 . &row2.h> void main() { int mata[10][10]. i < row2 . &col1) . for(i = 0 . scanf("%d %d".. printf("\nEnter the elements of second matrix : \n\n") . } . k++) matc[i][j] = matc[i][j] + mata[i][k] * matb[k][j] . } printf("\n") . getch() . i++) { for(j = 0 . &mata[i][j]) . i < row1 . printf("\nEnter the order of second matrix : ") . for(i = 0 . scanf("%d %d". matc[i][j]) . j < col1 . j < col2 .. } } else printf("\nMatrix Multiplication is not possible . j++) scanf("%d". i < row1 . } } printf("\nThe resultant matrix is : \n\n") . j. &row1. for(i = 0 . i++) for(j = 0 .A. col1. j++) { printf("%d \t". clrscr() .MATMUL. j < col2 .") . int i. k. matb[10][10]. row2. for(k = 0 . k < col1 .

. ..Bhuvaneswaran A.B.75 RUN 1 : ~~~~~~~ Enter the order of first matrix : 3 3 3 Enter the order of second matrix : 3 Enter the elements of first matrix : 1 1 1 1 1 1 1 1 1 Enter the elements of second matrix : 1 1 1 1 1 1 1 1 1 The resultant matrix is : 3 3 3 RUN 2 : ~~~~~~~ Enter the order of first matrix : 3 3 2 3 3 3 3 3 3 Enter the order of second matrix : 2 Matrix Multiplication is not possible .

clrscr() .STRASCII. } getch() . printf("ASCII chart for characters : \n\n") . ch <= 122 . ch) . printf("%c \t %3d \t".h> # include <conio.h> void main() { char ch . ch++) { if(ch > 90 && ch < 97) continue .C */ # include <stdio.76 Programs in C String Handling Programs /* Program to print the alphabets with ASCII values . } RUN 1 : ~~~~~~~ ASCII chart for characters : A 69 F 74 K 79 P 84 U 89 Z 100 e 105 j 110 o 115 t 120 y 65 70 75 80 85 90 101 106 111 116 121 B G L Q V a f k p u z 66 71 76 81 86 97 102 107 112 117 122 C H M R W b g l q v 67 72 77 82 87 98 103 108 113 118 D I N S X c h m r w 68 73 78 83 88 99 104 109 114 119 E J O T Y d i n s x . for(ch = 65 . ch.A.

getch() .STRVOWEL. if( c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u' || c == 'A' || c == 'E' || c == 'I' || c == 'O' || c == 'U') printf("\n%c is a vowel".Bhuvaneswaran A.77 /* To check the given character is vowel or not . scanf("%c". c) .h> void main() { char c . &c) . else printf("\n%c is not a vowel".h> # include <conio. clrscr() . } RUN 1 : ~~~~~~~ Enter the character : u u is a vowel RUN 2 : ~~~~~~~ Enter the character : v v is not a vowel . c) .C */ # include <stdio.B. printf("Enter the character : ") .

78 Programs in C /* Program to find the length of the given string . getch() . } RUN 1 : ~~~~~~~ Enter the string : welcome The length of the string is : 7 .A. int i . str[i] != '\0' . printf("Enter the string : ") .h> # include <conio.C */ # include <stdio. gets(str) . printf("\nThe length of the string is : %d".h> void main() { char str[80] . i) . for(i = 0 . clrscr() .STRLEN. i++) .

B./letter/special character . &ch) .h> void main() { char ch .Bhuvaneswaran A. getch() .79 /* To find a character is no. else if(ch >= 48 && ch <= 57) printf("\nEntered character is a number") . else printf("\nEntered character is a special character") . printf("Enter a charatcer : ") . clrscr() . scanf("%c".h> # include <conio. } RUN 1 : ~~~~~~~ Enter a charatcer : b Entered character is a lower case letter RUN 2 : ~~~~~~~ Enter a charatcer : V Entered character is a upper case letter RUN 3 : ~~~~~~~ Enter a charatcer : 2 Entered character is a number RUN 4 : ~~~~~~~ Enter a charatcer : # Entered character is a special character .C */ # include <stdio. else if(ch >= 97 && ch <= 122) printf("\nEntered character is a lower case letter") . if (ch >= 65 && ch <= 90) printf("\nEntered character is a upper case letter") .STRFIND.

clrscr() . int i = 0 . ch . else putchar(' ') . } RUN 1 : ~~~~~~~ Enter the text in uppercase : Anna University The converted text is : anna university .80 Programs in C /* To convert uppercase characters to lowercase . while(str[i] != '\0') { if(str[i] != ' ') printf("%c". str[i] + 32) . } getch() . printf("Enter the text in uppercase : ") .STRCONV. i++ . con[80] . gets(str) .h> # include <conio.C */ # include <stdio.h> void main() { char str[80].A. printf("\nThe converted text is : \n\n") .

special & words . printf("\n\nNumber of words = %d". printf("Enter the text : ") . special) . else if(ch == ' ') ++word . } ++ word .Bhuvaneswaran A. printf("\n\nNumber of special characters = %d". printf("\nThe text contains : ") . else if(ch >= '0' && ch <= '9') ++digit . digit) . while((ch = tolower(text[i++])) != '\0') { if(ch=='a' || ch=='e' || ch=='i' || ch=='o' || ch=='u') ++vowel . printf("\n\nNumber of vowels = %d". ch .STRCOUNT. else if(ch >= 'a' && ch <= 'z') ++cons . else ++special .h> # include <conio. } RUN 1 : ~~~~~~~ Enter the text : Bhuvan was born in 2nd Nov 1977 !!! The text contains : Number of vowels = 6 Number of consonants = 14 Number of digits = 5 Number of special characters = 3 Number of words = 8 .h> void main() { char text[80]. gets(text) . special = 0. getch() . word) . cons = 0. cons) . printf("\n\nNumber of digits = %d".digits. word = 0. int vowel = 0. clrscr() .h> # include <ctype. vowel) .C*/ # include <stdio.81 /* Counting vowels. printf("\n\nNumber of consonants = %d".B.consonants. i = 0. digit = 0.

h> # include <conio. scanf("%s". i++) strcon[i] = str1[i] . j++) strcon[i+j+1] = str2[j] . str1) .STRCAT. str2[j] != '\0' . str1[i] != '\0' . str2) . printf("\nEnter the second string : ") . printf("Enter the first string : ") . } RUN 1 : ~~~~~~~ Enter the first string : Anna Enter the second string : University The concatenation string is : AnnaUniversity . str2[20].82 Programs in C /* Program to concatenate the given two strings . getch() .. for(i = 0 .h> void main() { char str1[20]. strcon[40] . i-. clrscr() . strcon) . for(j = 0 . int i. strcon[i+j+1] = '\0' . printf("\nThe concatenation string is : %s".A. scanf("%s".C */ # include <stdio. j .

str1. str1. printf(". getch() . printf(". strlen(str2)) . gets(str2) .h> # include <string.Bhuvaneswaran A. str2) . str1. str2) . str2[40] . printf("\nEnter the second string : \n\n") . } . printf(". printf("\n\nString 1 = %s & String 2 = %s ". printf("\n\nString 1 = %s & String 2 = %s ".C */ # include <stdio. str2) .Concatenation is : %s ". strlen(str1). strrev(str2)) . printf("\n\nString 1 = %s & String 2 = %s ". str1.STRFUNC.Lowercase is : %s and %s". str1. clrscr() . printf(". strrev(str1). printf("\n\nString 1 = %s & String 2 = %s ". strupr(str1).Uppercase is : %s and %s".h> # include <conio. printf("\n\nString 1 = %s & String 2 = %s ".str2)). gets(str1) . strupr(str2)). strlwr(str1).str2)). printf("Enter the first string : \n\n") . str2) .Reverse is : %s and %s". str2) . str1. printf("\n\nString 1 = %s & String 2 = %s ". str2) . strcpy(str1.h> void main() { char str1[40]. str2) . str1.83 /* Perform string manipulation using string functions . printf("\nString 1 = %s & String 2 = %s ". strlwr(str2)). printf(".String copy is : %s ".B.Length is : %d and %d". printf(". strcat(str1.

84 Programs in C RUN 1 : ~~~~~~~ Enter the first string : Anna Enter the second string : Madras String 1 = Anna & String 2 = Madras .String copy is : sardam String 1= sardam & String 2= sardam .A.Length is : 4 and 6 String 1 = Anna & String 2 = Madras .Lowercase is : anna and madras String 1 = anna & String 2 = madras .Uppercase is : ANNA and MADRAS String 1 = ANNA & String 2 = MADRAS .Reverse is : anna and sardam String 1 = anna & String 2 = sardam .Concatenation is : sardamsardam String 1 = sardamsardam & String 2 = sardam .

} printf("\nThe character %c appears %d times in the text". count) .STROCC.85 /* To count no.Bhuvaneswaran A.B. int i = 0. printf("\nEnter the character to be search : ") . while(str[i] != '\0') { if(ch == str[i]) count++ . &ch) . of occurence of a character in a string . ch . scanf("%c".h> # include <conio. clrscr() .h> void main() { char str[80]. } RUN 1 : ~~~~~~~ Enter the text : Anna University Enter the character to be search : n The character n appears 3 times in the text .C */ # include <stdio. gets(str) . i++ . getch() . printf("Enter the text : \n\n") . count = 0 . ch.

A.86 Programs in C
/* Program to reverse the given string - STRREV.C */ # include <stdio.h> # include <conio.h> void main() { char str[20], rev[20] ; int i, j, l ; clrscr() ; printf("Enter a string : ") ; scanf("%s", str) ; for(l = 0 ; str[l] != '\0' ; l++) ; for(i = l - 1, j = 0 ; i >= 0 ; i--, j++) rev[j] = str[i] ; rev[j] = '\0' ; printf("\nThe given string is : %s\n\n", str) ; printf("The reversed string is : %s", rev) ; getch() ; } RUN 1 : ~~~~~~~ Enter a string : bhuvan The given string is : bhuvan The reversed string is : navuhb

B.Bhuvaneswaran A.87 /* To check the given string is palindrome or not - STRPAL.C */ # include <stdio.h> # include <conio.h> # include <string.h> void main() { char str[20], rev[20] ; int i, j, l ; clrscr() ; printf("Enter a string : ") ; scanf("%s", str) ; for(l = 0 ; str[l] != '\0' ; l++) ; for(i = l - 1, j = 0 ; i >= 0 ; i--, j++) rev[j] = str[i] ; rev[j] = '\0' ; if(stricmp(str, rev) == 0) printf("\nThe given string is a palindrome") ; else printf("\nThe given string is not a palindrome") ; getch() ; } RUN 1 : ~~~~~~~ Enter a string : madam The given string is a palindrome RUN 2 : ~~~~~~~ Enter a string : malayalam The given string is a palindrome RUN 3 : ~~~~~~~ Enter a string : liril The given string is a palindrome RUN 4 : ~~~~~~~ Enter a string : bhuvan The given string is not a palindrome

A.88 Programs in C
/* To sort the given strings in alphabetical order - STRSORT.C */ # include <stdio.h> # include <conio.h> # include <string.h> void main() { char str[10][20], temp[20] ; int n, i, j ; clrscr() ; printf("Enter the number of strings : ") ; scanf("%d", &n) ; printf("\nEnter the strings : \n\n") ; for(i = 0 ; i < n ; i++) scanf("%s", str[i]) ; for(i = 0 ; i < n - 1 ; i++) for(j = 0 ; j < n - 1; j++) if(strcmp(str[j], str[j + 1]) > 0) { strcpy(temp, str[j]) ; strcpy(str[j], str[j + 1]) ; strcpy(str[j + 1], temp) ; } printf("\nThe sorted order of strings are : \n\n") ; for(i = 0 ; i < n ; i++) printf("%s \n", str[i]) ; getch() ; } RUN 1 : ~~~~~~~ Enter the number of strings : 5 Enter the strings : viji udaya priya bhuvan satish The sorted order of strings are : bhuvan priya satish udaya viji

B.Bhuvaneswaran A.89

Structures Programs
/* Program to maintain student details using structures - STSTUD.C */ # include <stdio.h> # include <conio.h> struct stud { int rollno, s1, s2, tot ; char name[10] ; float avg ; } s[10] ; void main() { int i, n ; clrscr() ; printf("Enter the number of students : ") ; scanf("%d", &n) ; for(i = 0 ; i < n ; i++) { printf("\nEnter the roll number : ") ; scanf("%d", &s[i].rollno) ; printf("\nEnter the name : ") ; scanf("%s", s[i].name) ; printf("\nEnter the marks in 2 subjects : ") ; scanf("%d %d", &s[i].s1, &s[i].s2) ; s[i].tot = s[i].s1 + s[i].s2 ; s[i].avg = s[i].tot / 2.0 ; } printf("\nRoll No. Name \t\tSub1\t Sub2\t Total\t Average\n\n") ; for(i = 0 ; i < n ; i++) { printf("%d \t %s \t\t %d \t %d \t %d \t %.2f \n", s[i].rollno,s[i].name,s[i].s1,s[i].s2,s[i].tot,s[i].avg); } getch() ; }

00 85 .90 Programs in C RUN 1 : ~~~~~~~ Enter the number of students : 2 Enter the roll number : 101 Enter the name : Arun Enter the marks in 2 subjects : 75 Enter the roll number : 102 Enter the name : Babu Enter the marks in 2 subjects : 65 Roll No.A. Name 101 102 Arun Babu Sub1 75 65 Sub2 85 75 75 Total 160 140 Average 80.00 70.

&e[i].allow. &e[i]. ded. e[i].ded . e[i].bpay + e[i]. scanf("%d %d %d".ded) . n .empno.ded. &e[i]. } printf("\nEmp. for(i = 0 . i++) { printf("\nEnter the employee number : ") . i++) { printf("%d \t %s \t %d \t %d \t %d \t %d \n".npay = e[i].STEMP. scanf("%d".bpay. printf("Enter the number of employees : ") .npay) . int bpay. e[i]. e[i]. clrscr() . allowances & deductions : ") . for(i = 0 . } e[10] .h> # include <conio.allow.name.Bhuvaneswaran A. No. e[i]. e[i]. e[i]. &n) . scanf("%s". scanf("%d". &e[i]. i < n . i < n .bpay.name) .empno) . e[i]. } getch() . char name[10] . Name \t Bpay \t Allow \t Ded \t Npay \n\n") . printf("\nEnter the basic pay.e[i].91 /* Program to maintain employee details using structures .C */ # include <stdio. npay .B. void main() { int i.h> struct emp { int empno .allow . allow. printf("\nEnter the name : ") . } .

allowances & deductions : 7000 Emp. Name 101 102 Arun Babu Bpay 5000 7000 Allow 1000 1500 Ded 250 750 Npay 5750 7750 1500 750 1000 250 . allowances & deductions : 5000 Enter the employee number : 102 Enter the name : Babu Enter the basic pay.92 Programs in C RUN 1 : ~~~~~~~ Enter the number of employees : 2 Enter the employee number : 101 Enter the name : Arun Enter the basic pay. No.A.

h> # include <conio. printf("Enter a string : ") . } RUN 1 : ~~~~~~~ Enter a string : bhuvan The length of the string is : 6 .Bhuvaneswaran A.C */ # include <stdio.PTRSTRLN.B. } printf("\nThe length of the string is : %d". getch() . str++ . int len = 0 . str) . clrscr() . scanf("%s". while(*str != '\0') { len++ .93 Pointers Programs /* To find the length of the string using pointers . len) .h> void main() { char *str .

h> # include <conio. *str1 = '\0' . scanf("%s". getch() .94 Programs in C /* To copy one string to another using pointers .A. *str2 != '\0' . str1++.C */ # include <stdio. *str2 . int i . for(i = 0. str1 = str1 . str1) .h> void main() { char *str1. } RUN 1 : ~~~~~~~ Enter the string : bhuvan The copied string is : bhuvan . printf("Enter the string : ") .i . clrscr() . str2++) *str1 = *str2 . printf("\nThe copied string is : %s".PTRSTRCP. i++. str2) .

*str2 . scanf("%s". scanf("%s".h> # include <conio. str1++) . int i.95 /* Concatenate the given two strings using pointers . str2) .B.j .C */ # include <stdio. str1 = str1 . j . j++.h> void main() { char *str1.i . *str1 = '\0' . str1) . *str2 != '\0' . *str1 != '\0' . str1) . str1++. printf("\nThe concatenated string is : %s". str2++) *str1 = *str2 . printf("Enter the first string : ") .Bhuvaneswaran A. for(j = 0. clrscr() . printf("\nEnter the second string : ") . for(i = 0. getch() .PTRSTRCT. i++. } RUN 1 : ~~~~~~~ Enter the first string : hello Enter the second string : world The concatenated string is : helloworld .

} if(*str1 == '\0' && *str2 == '\0') printf("\nThe given strings are equal") . clrscr() . str1) .PTRSTRCM. str2) . } RUN 1 : ~~~~~~~ Enter the first string : cse Enter the second string : ece The given strings are not equal RUN 2 : ~~~~~~~ Enter the first string : mech Enter the second string : mech The given strings are equal .h> void main() { char *str1. printf("Enter the first string : ") . str2++ .A. scanf("%s". getch() .h> # include <conio.96 Programs in C /* To compare the given two string using pointers .C */ # include <stdio. scanf("%s". else printf("\nThe given strings are not equal") . printf("\nEnter the second string : ") . *str2 . while(*str1 == *str2 && (*str1 != '\0' || *str2 != '\0')) { str1++ .

fclose(fptr1) . fclose(fptr1) .\n") . printf("Enter the text to be stored in the file. do { c = fgetc(fptr1) ."w") . fptr1 = fopen("COURSES.Bhuvaneswaran A.B. fptr1) . fptr1 = fopen("COURSES.h> void main() { char c . Use ^Z or F6 at the end of the text and press ENTER: Computer Science & Engineering Information Technology Electronics & Communication Engineering ^Z The content of the file is : Computer Science & Engineering Information Technology Electronics & Communication Engineering . FILE *fptr1 .C */ # include <stdio.FILEWRRD. } RUN 1 : ~~~~~~~ Enter the text to be stored in the file.h> # include <conio. } while(c != EOF) . putchar(c) . getch() . printf("Use ^Z or F6 at the end of the text and press ENTER: \n\n") . printf("\nThe content of the file is : \n\n") .97 File Handling Programs /* Program to write and read data from a file . while((c = getc(stdin)) != EOF) fputc(c. "r") .DAT". clrscr() .DAT".

"r") .DAT". fclose(fptr3) . num) . printf("\n\nContents of EVEN file is : ") . "r") .DAT".DAT". fclose(fptr2) . "r") . printf("\nContents of ODD file is : ") . "w") . else putw(num. i++) { scanf("%d". getch() . &n) .FILEODEV.h> void main() { FILE *fptr1. fptr2 = fopen("ODD. fclose(fptr2) . fptr3 = fopen("EVEN. fptr1 = fopen("NUMBERS. int n.A.98 Programs in C /* Read integers and store odd & even no. i. } . in a file .DAT". while((num = getw(fptr3)) != EOF) printf("%d\t".DAT". fptr1 = fopen("NUMBERS. fptr2) . printf("\nEnter the values : ") . fptr2 = fopen("ODD. "w") . &num) . } fclose(fptr1) . num) . while((num = getw(fptr1)) != EOF) { if(num % 2 == 0) putw(num. for(i = 0 .C */ # include <stdio. *fptr3 . fptr3) . i < n . "w") . *fptr2. fptr1) . num . while((num = getw(fptr2)) != EOF) printf("%d\t".h> # include <conio.DAT". } fclose(fptr1) . fptr3 = fopen("EVEN. clrscr() . fclose(fptr3) . printf("Enter number of values : ") . putw(num. scanf("%d".

B.Bhuvaneswaran A.99 RUN 1 : ~~~~~~~ Enter number of values : 6 Enter the values : 11 Contents of ODD file is 22 : 11 33 33 44 44 55 66 55 66 Contents of EVEN file is : 22 .

rollno. n. i++) { fscanf(fptr.DAT". s1 + s2) . &n) . } . scanf("%d %d". fptr = fopen("STUDENT. char name[10] . for(i = 0 .h> # include <conio. rollno."%d %s %d %d \n". "r") . fprintf(fptr.A. "w") . i++) { printf("\nEnter the roll number : ") . printf("%d \t %s \t\t %d \t %d \t %d \n". } fclose(fptr) . fptr = fopen("STUDENT. "%d %s %d %d \n". &s2) . name) . name. s1. Name \t\t Sub1 \t Sub2 \t Total\n\n") . name. printf("\nEnter the name : ") .h> void main() { FILE *fptr . scanf("%d". name. clrscr() . for(i = 0 .C */ # include <stdio. i < n . } fclose(fptr) . int i. s2 . &rollno) . &s1. &s2) . rollno. printf("\nRoll No.FILESTUD. s2. s2) . scanf("%d".100 Programs in C /* Program to maintain student details using files . &s1. printf("\nEnter the marks in 2 subjects : ") . printf("Enter the number of students : ") . s1. &rollno.DAT". i < n . scanf("%s". getch() . s1.

101 RUN 1 : ~~~~~~~ Enter the number of students : 2 Enter the roll number : 101 Enter the name : Udaya Enter the marks in 2 subjects : 75 Enter the roll number : 157 Enter the name : Viji Enter the marks in 2 subjects : 60 Roll No.Bhuvaneswaran A.B. Name 101 157 Udaya Viji Sub1 75 60 Sub2 80 70 70 Total 155 130 80 .

scanf("%d". ded .DAT".&ded).allow.A. No. i++) { fscanf(fptr. float bpay.Name\t\t Bpay\t\t Allow\t\t Ded\t\t Npay\n\n"). &n) . i < n . "w") . bpay + allow . bpay.2f \t %. char name[10] .bpay. fprintf(fptr. name. scanf("%f %f %f". } .2f \t %. printf("\nEnter the name : ") . clrscr() . &empno.FILEEMP. printf("\nEmp."%d%s%f%f%f\n". "r") . fptr = fopen("EMPLOYEE.h> void main() { FILE *fptr . allow. i++) { printf("\nEnter the employee number : ") .DAT".ded). ded. allowances & deductions : ") . printf("%d \t %s \t %.name. scanf("%s".h> # include <conio. &allow.102 Programs in C /* Program to maintain employee details using files .&bpay. printf("\nEnter the basic pay. empno . i < n .ded) . n. fptr = fopen("EMPLOYEE. &ded) . empno. "%d %s %f %f %f \n". getch() .&allow. } fclose(fptr) . &bpay.2f \t %. printf("Enter the number of employees : ") .name. scanf("%d". } fclose(fptr) . int i. for(i = 0 . empno. name) .C */ # include <stdio.2f \n". allow. for(i = 0 . &empno) .

00 1000. No.B.00 450 1000 500 .00 1000 Npay 6500.00 Ded 500. allowances & deductions : 5000 Emp.00 5550. allowances & deductions : 6000 Enter the employee number : 102 Enter the name : Priya Enter the basic pay.00 5000.00 450. Name 101 102 Udaya Priya Bpay 6000.103 RUN 1 : ~~~~~~~ Enter the number of employees : 2 Enter the employee number : 101 Enter the name : Udaya Enter the basic pay.00 Allow 1000.Bhuvaneswaran A.

DAT". fclose(fptr1) . fptr2) .DAT". fptr3) . getch() . fptr3 = fopen("MERGE. clrscr() . while((c = fgetc(fptr1)) != EOF) putchar(c) .104 Programs in C /* Program to merge the contents of two files . printf("\nThe content of the merged file is : \n\n") . while((c = getc(stdin)) != EOF) fputc(c.h> # include <conio. printf("Use ^Z or F6 at the end of the text and press ENTER : \n\n") .h> void main() { char c . while((c = fgetc(fptr1)) != EOF) fputc(c. FILE *fptr1. } . fclose(fptr3) ."w") .\n") ."r") .1. "r") .FILEMER.C */ # include <stdio. fclose(fptr2) . printf("Use ^Z or F6 at the end of the text and press ENTER : \n\n") .A. fptr3 = fopen("MERGE.DAT". printf("Enter the text to be stored in the file ."w") . fptr2 = fopen("SECOND. fptr1 = fopen("FIRST. *fptr2.DAT". while((c = getc(stdin)) != EOF) fputc(c. while((c = fgetc(fptr2)) != EOF) fputc(c. fclose(fptr1) . fclose(fptr3) .2. printf("\nEnter the text to be stored in the file ."w") . fptr1 = fopen("FIRST.DAT". *fptr3 . fptr1) .\n") . fptr3) . fptr2 = fopen("SECOND."r") .DAT". fclose(fptr2) .

B.Bhuvaneswaran A.105 RUN 1 : ~~~~~~~ Enter the text to be stored in the file - 1. Use ^Z or F6 at the end of the text and press ENTER : Computer Practice - I I - Semester ^Z Enter the text to be stored in the file - 2. Use ^Z or F6 at the end of the text and press ENTER : Computer Practice - II II - Semester ^Z The content of the merged file is : Computer Practice - I I - Semester Computer Practice - II II - Semester

A.106 Programs in C
/* Program to encrypt and decrypt a file - ENCDEC.C */ # include <stdio.h> # include <conio.h> void main() { FILE *fptr; char c ; clrscr() ; printf("Enter the text to be stored in the file.\n") ; printf("Use ^Z or F6 at the end of the text and press ENTER : \n\n") ; fptr = fopen("ENCDEC.DAT","w") ; while((c = getchar()) != EOF) fputc(c, fptr) ; fclose(fptr) ; printf("\n\nData output in encrypted form : \n\n") ; fptr = fopen("ENCDEC.DAT", "r") ; while((c = fgetc(fptr)) != EOF) printf("%c", c+1) ; fclose(fptr) ; printf("\n\nData output in decrypted form : \n\n") ; fptr = fopen("ENCDEC.DAT", "r") ; while((c = fgetc(fptr)) != EOF) printf("%c", c) ; fclose(fptr) ; getch() ; } RUN 1 : ~~~~~~~ Enter the text to be stored in the file. Use ^Z or F6 at the end of the text and press ENTER : Computer Practice - II D.D. Publications ^Z

Data output in encrypted form : Dpnqvufs!Qsbdujdf!.!JJ E/E/!Qvcmjdbujpot

Data output in decrypted form : Computer Practice - II D.D. Publications

B.Bhuvaneswaran A.107

Searching Programs
/* Program to search an element using binary search - BINARY.C */ # include <stdio.h> # include <conio.h> void main() { int a[10], f, l, i, j, k, mid, n, t, flag = 0 ; clrscr() ; printf("Enter the limit : ") ; scanf("%d", &n) ; printf("\nEnter the elements :\n\n") ; for(i = 0 ; i < n ; i++) scanf("%d", &a[i]) ; for(i = 0 ; i < n ; i++) for(j = i + 1 ; j < n ; j++) if(a[i] > a[j]) { t = a [i] ; a[i] = a[j] ; a[j] = t ; } printf("\nThe ordered elements are : \n\n") ; for(i = 0 ; i < n ; i++) printf("%d\t", a[i]) ; printf("\n\nEnter the element to be searched : ") ; scanf("%d", &k) ; f = 0 ; l = n - 1 ; while(f <= l) { mid = (f + l) / 2 ; if(a[mid] == k) { flag = 1 ; break ; } else if(a[mid] < k) f = mid + 1 ; else l = mid - 1 ; } if(flag == 1) printf("\nThe element is found at location : %d", mid + 1) ; else printf("\nThe element is not found") ; getch() ; }

108 Programs in C RUN 1 : ~~~~~~~ Enter the limit : 5 Enter the elements : 20 40 30 50 10 The ordered elements are : 10 20 30 40 50 Enter the element to be searched : 30 The element is found at location : 3 RUN 2 : ~~~~~~~ Enter the limit : 5 Enter the elements : 20 40 30 50 10 The ordered elements are : 10 20 30 40 50 Enter the element to be searched : 70 The element is not found .A.

clrscr() . } } if(flag == 1) printf("\nThe element is found at location : %d". printf("Enter the limit : ") . printf("\nEnter the numbers :\n\n") . scanf("%d". &item) . &a[i]) . for(i = 0 . getch() . for(i = 0 . i < n .h> # include <conio. break . i++) { if(item == a[i]) { flag = 1 . i. printf("\nEnter the element to be searched : ") . n. else printf("\nThe element is not found") . } .Bhuvaneswaran A. flag = 0 . i++) scanf("%d".C */ # include <stdio. i < n . item.B.h> void main() { int a[10]. i + 1) . scanf("%d".LINEAR.109 /* Program to search an element using linear search . &n) .

110 Programs in C RUN 1 : ~~~~~~~ Enter the limit : 5 Enter the numbers : 20 40 30 50 10 Enter the element to be searched : 30 The element is found at location : 3 RUN 2 : ~~~~~~~ Enter the limit : 5 Enter the numbers : 20 40 30 50 10 Enter the element to be searched : 70 The element is not found .A.

i++) printf("%d\t".1 . i < n . t. for(i = 0 . i. i++) { for(j = 0 . i++) scanf("%d".1 . printf("\nEnter the numbers :\n\n") . printf("Enter the limit : ") . &a[i]) .B.BUBSORT.h> # include <conio. a[i]) . for(i = 0 .h> void main() { int a[10]. a[j] = a[j + 1] . scanf("%d". n . a[j + 1] = t . i < n . j < n . for(i = 0 . getch() . } } } printf("\nThe sorted elemets are :\n\n") .111 Sorting Programs /* Program to sort the given numbers using bubble sort . j++) { if(a[j] > a[j + 1]) { t = a[j] . clrscr() . &n) . } RUN 1 : ~~~~~~~ Enter the limit : 5 Enter the numbers : 20 40 30 50 10 The sorted elemets are : 10 20 30 40 50 . i < n .Bhuvaneswaran A.C */ # include <stdio. j.

j++) if(left > a[j]) { left = a[j] . i++) { left = a[i] .h> void main() { int i. a[10]. j < n . &n) . i++) printf("%d\t". k. } printf("\nThe sorted elemets are :\n\n") .A. scanf("%d".SELSORT. k = j . printf("\nEnter the elements :\n\n") . t . i < n . for(i = 0 . j. clrscr() . for(i = 0 . a[i]) . i < n . &a[i]) . i < n .112 Programs in C /* To sort the given numbers using selection sort . for(j = i + 1 . } RUN 1 : ~~~~~~~ Enter the limit : 5 Enter the elements : 20 40 30 50 10 The sorted elemets are : 10 20 30 40 50 . a[i] = left . n. getch() .h> # include <conio. printf("Enter the limit : ") . left.C */ # include <stdio. i++) scanf("%d". } a[k] = a[i] . for(i = 0 . k = i .

i < n . getch() .INSSORT.113 /* To sort the given numbers using insertion sort .C */ # include <stdio. } printf("\nThe sorted elemets are :\n\n") .Bhuvaneswaran A. i >= 0 && t < a[i] . j < n .h> void main() { int i. scanf("%d". i++) scanf("%d". i < n . i++) printf("%d\t". a[10]. a[i + 1] = t . } RUN 1 : ~~~~~~~ Enter the limit : 5 Enter the elements : 20 40 30 50 10 The sorted elemets are : 10 20 30 40 50 . for(i = 0 .B. printf("Enter the limit : ") . clrscr() . j. for(j = 1 . printf("\nEnter the elements :\n\n") .1 . for(i = j . n. a[i]) . j++) { t = a[j] . &a[i]) .h> # include <conio. t . for(i = 0 . &n) . i--) a[i + 1] = a[i] .

a[i]) . . if(left < right) { i = left + 1 . printf("\nThe sorted elemets are :\n\n") . t. a[v] = a[j] . a[j] = t . } else break . i < n .1) . getch() . clrscr() . int right) { int i.. scanf("%d". v . printf("\nEnter the elements :\n\n") .h> # include <conio. a[j] = t . } void qsort(int left. v = left . while(a[v] < a[j]) j-.C */ # include <stdio. &a[i]) . n . int) . if(i < j) { t = a[i] . a[i] = MAXINT . for(i = 0 . j. for(i = 0 . i++) scanf("%d". &n) . void qsort(int. j = right . n .A.QSORT.h> # include <values.h> int a[10] . ) { while(a[v] >= a[i]) i++ . } t = a[v] . printf("Enter the limit : ") . a[i] = a[j] . i < n .114 Programs in C /* Program to sort the given numbers using quick sort . i++) printf("%d\t". for( . . void main() { int i. qsort(0.

115 qsort(left.Bhuvaneswaran A.1) .B. qsort(j + 1. right) . } RUN 1 : ~~~~~~~ Enter the limit : 5 Enter the elements : 20 40 30 50 10 The sorted elemets are : 10 20 30 40 50 . } return . j .

t . &n) . printf("\nEnter the elements :\n\n") .SHELSORT.116 Programs in C /* Program to sort the given numbers using shell sort . clrscr() . i = i / 2) for(j = i . n. } RUN 1 : ~~~~~~~ Enter the limit : 5 Enter the elements : 20 40 30 50 10 The sorted elemets are : 10 20 30 40 50 .A.i . j. &a[i]) . i++) printf("%d\t".i) a[k + i] = a[k] .h> # include <conio. i >= 1 . a[i]) . for(i = (n + 1) / 2 . k. k >= 0 && t < a[k] . for(i = 0 .C */ # include <stdio. } printf("\nThe sorted elemets are :\n\n") . getch() . j < n . printf("Enter the limit : ") . k = k . a[k + i] = t . i < n . for(k = j . a[10]. j++) { t = a[j] . scanf("%d".h> void main() { int i. i < n . for(i = 0 . i++) scanf("%d".

1 . a[j + 1] = t . } RUN 1 : ~~~~~~~ Enter the limit : 5 Enter the numbers : 20 30 10 50 40 The numbers in ascending order is : 10 20 30 40 50 The numbers in descending order is : 50 40 30 20 10 . a[i]) .Bhuvaneswaran A.h> # include <conio. n . printf("\n\nThe numbers in descending order is :\n\n") .1 . for(i = 0 . j. j < n . for(i = 0 . i++) for(j = 0 . a[i]) . for(i = 0 . printf("\nEnter the numbers :\n\n") . for(i = n -1 . i. &n) . &a[i]) . i < n . i >= 0 . i++) scanf("%d". i++) printf("%d\t".117 /* To sort the given numbers in ascending & descending order SORTNUM. i < n . i < n . a[j] = a[j + 1] .h> void main() { int a[10]. scanf("%d". } printf("\nThe numbers in ascending order is :\n\n") . i--) printf("%d\t".B. getch() . t. printf("Enter the limit : ") .C */ # include <stdio. clrscr() . j++) if(a[j] > a[j + 1]) { t = a[j] .

break . i . case 3 : display() . scanf("%d".STACK. } . clrscr() .Exit\n") . do { printf("\n[1]. top = -1.Display [4].h> # define SIZE 10 int arr[SIZE]. break . switch(ch) { case 1 : push() . printf("\nEnter your choice [1-4] : ") .C */ # include <stdio. default : printf("\nInvalid option\n") . case 2 : pop() .118 Programs in C Data Structures Programs /* Implentation of stack using arrays . void main() { int ch . void display() . } } while(ch != 4) .h> # include <conio.POP [3]. break .A. &ch) . getch() . void push() . case 4 : break . getch() .PUSH [2]. void pop() .

getch() . scanf("%d".Display [4].PUSH [2]. &arr[top]) . arr[i]) .Bhuvaneswaran A.Exit Enter your choice [1-4] : 1 Enter the element to PUSH : 10 . getch() .119 void push() { if(top == SIZE . } void pop() { if(top == -1) { printf("\nStack is empty (underflow)\n") . } RUN 1 : ~~~~~~~ [1]. return .. top-. i--) printf(" -> %d".1) { printf("\nStack is full (overflow)\n") . return . } void display() { if(top == -1) { printf("\nStack is empty (underflow)\n") . printf("\n") . getch() . arr[top]) . getch() . } printf("\nThe elements in stack are :\n\nTOP") . i >= 0 . getch() . } top++ .B. printf("\nEnter the element to PUSH : ") . return .POP [3]. for(i = top . } printf("\nThe POP element is : %d\n".

PUSH [2].Display [4].Display [4].POP [3].Exit Enter your choice [1-4] : 2 The POP element is : 20 [1].PUSH [2].Exit Enter your choice [1-4] : 2 The POP element is : 10 [1].POP [3].Display [4].POP [3].Display [4].PUSH [2].PUSH [2].POP [3].Exit Enter your choice [1-4] : 1 Enter the element to PUSH : 20 [1].Display [4].POP [3].Display [4].Display [4].Exit Enter your choice [1-4] : 2 Stack is empty (underflow) [1].PUSH [2].A.POP [3].PUSH [2].Exit Enter your choice [1-4] : 3 The elements in stack are : TOP -> 20 -> 10 [1].120 Programs in C [1].POP [3].PUSH [2].Exit Enter your choice [1-4] : 3 The elements in stack are : TOP -> 10 [1].Exit Enter your choice [1-4] : 4 .

h> # include <conio. break . getch() . } void enqueue() { if(rear == SIZE . getch() .QUEUE. void dequeue() . } rear++ . &arr[rear]) .B. printf("\nEnter the element to ENQUEUE : ") . rear = -1. } } while(ch != 4) .ENQUEUE [2]. void enqueue() .h> # define SIZE 10 int arr[SIZE]. getch() . scanf("%d". break .121 /* Implentation of queue using arrays .Bhuvaneswaran A. default : printf("\nInvalid option\n") . switch(ch) { case 1 : enqueue() . case 3 : display() . .C */ # include <stdio.Exit\n") . void main() { int ch . &ch) . return . i . break . case 2 : dequeue() . do { printf("\n[1]. printf("\nEnter your choice [1-4] : ") . void display() . clrscr() .Display [4]. scanf("%d". front = -1. case 4 : break .1) { printf("\nQueue is full (overflow)\n") .DEQUEUE [3].

Exit Enter your choice [1-4] : 1 Enter the element to ENQUEUE : 20 . return ..Exit Enter your choice [1-4] : 1 Enter the element to ENQUEUE : 10 [1].Display [4]. %d". return . if(front == rear) front = rear = -1 ...Display [4].ENQUEUE [2]. getch() .REAR\n") . for(i = front .A. getch() . arr[i]) .ENQUEUE [2]. } printf("\nThe DEQUEUE element is : %d\n". getch() . arr[front]) . } printf("\nThe elements in queue are :\n\nFRONT ->") . } RUN 1 : ~~~~~~~ [1]. i++) printf(" .DEQUEUE [3].. } void dequeue() { if(front == -1) { printf("\nQueue is empty (underflow)\n") . i <= rear . } void display() { if(front == -1) { printf("\nQueue is empty (underflow)\n") . printf(" . getch() .DEQUEUE [3]. <. else front++ .122 Programs in C if(front == -1) front++ .

. 20 . 20 .DEQUEUE [3].Bhuvaneswaran A.REAR [1]..Display [4].Exit Enter your choice [1-4] : 3 The elements in queue are : FRONT -> .Exit Enter your choice [1-4] : 3 The elements in queue are : FRONT -> . <....ENQUEUE [2].Exit Enter your choice [1-4] : 2 The DEQUEUE element is : 20 [1].ENQUEUE [2].DEQUEUE [3].Display [4].ENQUEUE [2]. 10 .Exit Enter your choice [1-4] : 4 .ENQUEUE [2].DEQUEUE [3]..123 [1].ENQUEUE [2].DEQUEUE [3]. <.Display [4]..DEQUEUE [3]..REAR Enter your choice [1-4] : 2 The DEQUEUE element is : 10 [1].Exit Enter your choice [1-4] : 2 Queue is empty (underflow) [1]...Display [4].B.Display [4].

printf("\nCheck whether they bracket EVEN roots. int *s. f0. &b.BISECT.000001 # define F(x) (x) * (x) * (x) . count) . } bim (float *a. bim(&a. f2 = F(x2) . &count) . } else { *count = 0 .C */ # include <stdio. float *root. float *b. F(root)) . } else { printf("\nRoot = %f".124 Programs in C Numerical Methods Programs /* Find the root of a equation using Bisection method . b. printf("\n\nIterations = %d". if(f1 * f2 > 0) { *s = 0 .") . return . int *count) { float x1.h> # define EPS 0.h> # include <math. &s. . count . f1. x2 = *b . &root. &a. printf("\n\nF(root) = %f". f1 = F(x1) . x2.2 * (x) . clrscr() . float a.h> # include <conio. } getch() .5 void main() { int s. &b) . printf("Input starting values : ") . root .A. if(s == 0) { printf("\nStarting points do not bracket any root.") . scanf("%f %f". x0. f2 . x1 = *a . root) .

B. } else { x1 = x0 .0 . *root = x0 . return .094552 F(root) = 0.000006 Iterations = 18 3 .0 . } } } RUN 1 : ~~~~~~~ Input starting values : 2 Root = 2. goto begin . } if(f1 * f0 < 0) { x2 = x0 .125 begin : x0 = (x1 + x2) / 2.Bhuvaneswaran A. f1 = f0 . } if(fabs((x2 . if(f0 == 0) { *s = 1 .x1) / x2) < EPS) { *s = 1 . f0 = F(x0) . *root = (x1 + x2) / 2. } else { *count = *count + 1 . return .

} else { x0 = xn .000001 # define MAXIT 20 # define F(x) (x) * (x) * (x) .fx / fdx . xn.C */ # include <stdio. printf("Input initial value of x : ") .126 Programs in C /* Find the root of a equation using Newton Raphson . &x0) . float x0. scanf("%f". } RUN 1 : ~~~~~~~ Input initial value of x : 0 Root = 2. if(fabs((xn . count) . begin : fx = F(x0) .h> # include <conio. clrscr() .h> # define EPS 0. } else { printf("\nSolution does not coverage in %d iterations". count = count + 1 .094552 Number of iterations = 19 . printf("\n\nNumber of iterations = %d". xn) .NEWTON. fx. fdx = FD(x0) .A.5 # define FD(x) 3 * (x) * (x) . fdx . } } getch() . xn = x0 .h> # include <math.2 void main() { int count .2 * (x) . MAXIT) . if(count < MAXIT) { goto begin . count = 1 .x0) / xn) < EPS) { printf("\nRoot = %f".

printf("\n\nNo. begin : if(fabs(f1 . of iterations = %d". printf("Input two starting points : ") . int *count. &status) . scanf("%f %f". f1.C */ # include <stdio. x2) . } else if(status == 2) { printf("\nNo convergence in %d iterations".B. float *root.E-10) { *status = 1 . f2. count) .127 /* To find the root of a equation using Secant Method . &x1.h> # include <math. status . F(root)) .SECANT. &count. float *x2.h> # define EPS 0. *x1 = *a .2 * (x) . printf("\n\nFunction value at root = %f". clrscr() . printf("\nLast x2 = %f". *count = 1 . x2 . } . f1 = F(*a) .h> # include <conio. sec(&a. &x2. count) . return . b. printf("\nLast x1 = %f".000001 # define MAXIT 50 # define F(x) (x) * (x) * (x) . error . *x2 = *b . int *status) { float x3. if(status == 1) { printf("\nDivision by zero") . &b. root. &root. } sec(float *a. MAXIT) . float *x1. x1. &b) . x1) . root) . &a. } else { printf("\nRoot = %f".Bhuvaneswaran A. } getch() . f2 = F(*b) .5 void main() { float a. float *b. int count.f2) <= 1. printf("\nNo. of iterations = %d".

f2 * (*x2 . } *x2 = x3 .*x2) / x3) . error = fabs((x3 .094552 Function value at root = 0. return . f2 = F(x3) .A.f1) . f1 = f2 . } else { *root = x3 .*x1) / (f2 . of iterations = 6 3 . } } RUN 1 : ~~~~~~~ Input two starting points : 2 Root = 2. if(error > EPS) { if(*count == MAXIT) { *status = 2 . } else { *x1 = *x2 . *status = 3 . return . goto begin .128 Programs in C x3 = *x2 .000001 No. *count = *count + 1 .

Sign up to vote on this title
UsefulNot useful