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(); }

A..4> : 3 10 * 5 = 50 .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..

c.Bhuvaneswaran A.h> void main() { int r.5 /* Program to print the multiplication table . r = r + 1 .h> # include <conio. printf("\n\n") . y .C */ # include <stdio. printf("%5d". do { c = 1 . printf("The multiplication table is :\n\n") . } while(c <= 10) . do { y = r * c . getch() . } 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 .B. y) . r = 1 . c = c + 1 .MULTABLE. } while(r <= 10) . clrscr() .

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

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

t. printf("\nThe table is :\n\n") . i <= n . &n) . i++) printf("%4d + %4d = %4d\n".A. getch() . scanf("%d". printf("Enter the table : ") .h> # include <conio. i + t) . t. n .ADDITION. i. } 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 . &t) . printf("\nEnter the limit : ") .C */ # include <stdio. for(i = 1 .h> void main() { int i. clrscr() . scanf("%d".8 Programs in C /* Printing addition table of the given number .

t.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 . printf("\nEnter the limit : ") . scanf("%d". i <= n . i++) printf("%4d x %4d = %4d\n".h> void main() { int i. scanf("%d". for(i = 1 .9 /* Program to print the multiplication table . &t) . printf("Enter the table : ") . printf("\nThe table is :\n\n") . i.B.h> # include <conio.MULTIPLY.C */ # include <stdio. i * t) . t. &n) . getch() . n . clrscr() .

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

big) . printf("\nThe biggest number is : %d".C */ # include <stdio.h> void main() { int a. big . &b. c. &a. getch() .h> # include <conio.Bhuvaneswaran A.BIGTER. big = a > b ? (a > c ? a : c) : (b > c ? b : c) . printf("Enter three numbers : ") . } 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. b. scanf("%d %d %d". clrscr() .11 /* Biggest of 3 numbers using ternary operator . &c) .

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

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

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

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

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

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

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

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

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

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

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

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

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

n) .C */ # include <stdio.B. } 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 : ") .Bhuvaneswaran A.25 /* To find the given no.h> # include <conio. clrscr() . else printf("\n%d is not a perfect square". n . n) . &n) . is perfect square or not . getch() . if (p == m) printf("\n%d is a perfect square".CHKPERSQ. m = p . scanf("%d".h> void main() { int m. p = sqrt(n) .h> # include <math. float p .

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

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

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

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

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

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

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

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

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

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

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

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

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

00 x[3] = 1.00 .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.00 x[2] = 1.Bhuvaneswaran A.

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

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

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

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

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

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

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

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

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

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

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

c++ . } 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 . j. } getch() . printf("\nFloyd's triangle is : \n\n") . j++) { printf("%-6d". j <= i . c) .h> # include <conio. i < r .FLOYDS.Bhuvaneswaran A.B.h> void main() { int r.C */ # include <stdio.51 Series Generation Programs /* Program to generate Floyd's triangle . } printf("\n\n") . c = 1 . scanf("%d". printf("Enter the number of rows : ") . i++) { for(j = 0 . clrscr() . i. &r) . for(i = 0 .

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

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

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

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

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

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

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

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

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

&mat[i][j]) . scanf("%d %d". clrscr() . for(i = 0 .A. printf("Enter the order of the matrix : ") .h> void main() { int mat[10][10] . j++) sum = sum + mat[i][j] . row. sum = 0 . &row. printf("\nEnter the elements of the matrix : \n\n") . j. sum) . } 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 . col. j++) scanf("%d". i < row .h> # include <conio. i++) for(j = 0 . getch() . i++) for(j = 0 . j < col .MATSUM. &col) . i < row . printf("\nThe sum of the elements in the matrix is : %d". int i. for(i = 0 .C */ # include <stdio.62 Programs in C Matrix Manipulation Programs /* To find sum of all the elements of the given matrix . j < col .

printf("\nThe sum of diagonal elements in the matrix is : %d".C */ # include <stdio. i < size . i++) sum = sum + mat[i][i] . scanf("%d".h> void main() { int mat[10][10] . getch() . &mat[i][j]) .B. size.h> # include <conio.63 /* Find sum of diagonal elements of the given matrix . clrscr() . j < size . int i.Bhuvaneswaran A. j++) scanf("%d".MATDIAG. &size) . for(i = 0 . sum) . printf("\nEnter the elements of the matrix : \n\n") . } 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 . i++) for(j = 0 . j. printf("Enter size of the square matrix : ") . for(i = 0 . i < size . sum = 0 .

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

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

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 .A.

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

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 .A.

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

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

Bhuvaneswaran A.B.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 .

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

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 .B.Bhuvaneswaran A.

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

B..Bhuvaneswaran A. ..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 .

ch++) { if(ch > 90 && ch < 97) continue .h> # include <conio.A. printf("ASCII chart for characters : \n\n") . clrscr() .STRASCII. ch.h> void main() { char ch . } 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 . ch) .C */ # include <stdio.76 Programs in C String Handling Programs /* Program to print the alphabets with ASCII values . printf("%c \t %3d \t". } getch() . for(ch = 65 . ch <= 122 .

getch() . scanf("%c". &c) . c) .77 /* To check the given character is vowel or not . 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". } RUN 1 : ~~~~~~~ Enter the character : u u is a vowel RUN 2 : ~~~~~~~ Enter the character : v v is not a vowel . printf("Enter the character : ") .h> # include <conio.STRVOWEL.C */ # include <stdio.Bhuvaneswaran A.B. c) . else printf("\n%c is not a vowel".h> void main() { char c . clrscr() .

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

else if(ch >= 48 && ch <= 57) printf("\nEntered character is a number") .STRFIND.h> void main() { char ch . else printf("\nEntered character is a special character") ./letter/special character .Bhuvaneswaran A.79 /* To find a character is no. clrscr() . if (ch >= 65 && ch <= 90) printf("\nEntered character is a upper case letter") . &ch) . } 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 . getch() . scanf("%c". printf("Enter a charatcer : ") . else if(ch >= 97 && ch <= 122) printf("\nEntered character is a lower case letter") .h> # include <conio.C */ # include <stdio.B.

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

vowel) . digit) . getch() . else ++special . printf("\n\nNumber of vowels = %d". word) . clrscr() . else if(ch >= '0' && ch <= '9') ++digit .C*/ # include <stdio. i = 0.h> void main() { char text[80].digits. printf("\n\nNumber of consonants = %d". else if(ch == ' ') ++word .consonants. else if(ch >= 'a' && ch <= 'z') ++cons . } ++ word . gets(text) . printf("\n\nNumber of special characters = %d". printf("\n\nNumber of digits = %d". int vowel = 0. special) .h> # include <ctype. ch .B.h> # include <conio. special = 0. cons) .STRCOUNT. cons = 0. while((ch = tolower(text[i++])) != '\0') { if(ch=='a' || ch=='e' || ch=='i' || ch=='o' || ch=='u') ++vowel . word = 0. } 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 . printf("\n\nNumber of words = %d".Bhuvaneswaran A. printf("\nThe text contains : ") . digit = 0.special & words .81 /* Counting vowels. printf("Enter the text : ") .

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

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

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

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

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() ; }

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.00 85 .00 70. Name 101 102 Arun Babu Sub1 75 65 Sub2 85 75 75 Total 160 140 Average 80.A.

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

allowances & deductions : 5000 Enter the employee number : 102 Enter the name : Babu Enter the basic pay. No. allowances & deductions : 7000 Emp.A.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. Name 101 102 Arun Babu Bpay 5000 7000 Allow 1000 1500 Ded 250 750 Npay 5750 7750 1500 750 1000 250 .

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

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

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

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

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

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

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 .B.Bhuvaneswaran A.

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

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

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

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

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() ; }

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

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.

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

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

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

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

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

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

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

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

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

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

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

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

Exit Enter your choice [1-4] : 2 The DEQUEUE element is : 20 [1].ENQUEUE [2].DEQUEUE [3]..Exit Enter your choice [1-4] : 3 The elements in queue are : FRONT -> ..Display [4]..ENQUEUE [2]..123 [1]..Bhuvaneswaran A.REAR [1]..ENQUEUE [2].B.Display [4].Display [4]. 10 .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]..ENQUEUE [2].DEQUEUE [3]. <... 20 .. <.ENQUEUE [2].Display [4].Exit Enter your choice [1-4] : 4 .DEQUEUE [3].Exit Enter your choice [1-4] : 3 The elements in queue are : FRONT -> .Display [4].DEQUEUE [3]. 20 .DEQUEUE [3].

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

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

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

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

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

Sign up to vote on this title
UsefulNot useful