You are on page 1of 128

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

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

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

B.Bhuvaneswaran A.7 while(rs >= 2) { g = rs / 2 ; printf("\n\nThe no. of Twos are : %d", g) ; break ; } while(rs >= 1) { h = rs / 1 ; printf("\n\nThe no. of ones are : %d", h) ; break ; } getch() ; } RUN 1 : ~~~~~~~ Enter the amount in Rupees : 179

The no. of hundreds are : 1 The no. of fifties are : 3 The no. of twenties are : 8 The no. of tens are : 17 The no. of fives are : 35 The no. of Twos are : 89 The no. of ones are : 179

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

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

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

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

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

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

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

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

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

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

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

The sum of the fibonacci series is : 7

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

B.Bhuvaneswaran A.21

Condition Checking Programs


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

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

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

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

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

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

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

A.28 Programs in C

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

The positive elements are : 10 30

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

The negative elements are : -20 -40 -50

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

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

The reversed array is : 40 10 50 30 20

A.32 Programs in C

Values Computing Programs


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

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

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

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

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

B.Bhuvaneswaran A.37 RUN 1 : ~~~~~~~ Enter the values of A, B & C : 0.0 Equation is linear RUN 2 : ~~~~~~~ Enter the values of A, B & C : 1.0 Roots are imaginary r1 = r2 = -1.00 + j -1.00 - j 2.45 2.45 2.0 7.0 4.0 7.0

RUN 3 : ~~~~~~~ Enter the values of A, B & C : 1.0 Roots are real and equal r1 = r2 = RUN 4 : ~~~~~~~ Enter the Values of A, B & C : 2.0 Roots are real and unequal r1 = r2 = -0.15 -3.35 7.0 1.0 -1.00 2.0 1.0

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

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

A.40 Programs in C

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

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

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

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

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

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

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

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

A.48 Programs in C

Series Calculation Programs


/* Program to calculate the cosine series - CALCOS.C */ # include <stdio.h> # include <conio.h> # include <math.h> void main() { int i, n ; float x, val, sum = 1, t = 1 ; clrscr() ; printf("Enter the value for x : ") ; scanf("%f", &x) ; printf("\nEnter the value for n : ") ; scanf("%d", &n) ; val = x ; x = x * 3.14159 / 180 ; for(i = 1 ; i < n + 1 ; i++) { t = t * pow((double) (-1), (double) (2 * i - 1)) * x * x / (2 * i * (2 * i - 1)) ; sum = sum + t ; } printf("\nCosine value of %f is : %8.4f", val, sum) ; getch() ; } RUN 1 : ~~~~~~~ Enter the value for x : 60 Enter the value for n : 20 Cosine value of 60.000000 is : 0.5000

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

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

B.Bhuvaneswaran A.51

Series Generation Programs


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

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

B.Bhuvaneswaran A.53 /* Program to generate Trigonometric Table - GENTRIG */ # include <stdio.h> # include <conio.h> # include <math.h> void main() { float r ; int i ; char ch ; clrscr() ; printf("- - - - - - - - - - - - - - - - - -") ; printf("\nAngle \t Sin \t Cos \t Tan \n") ; printf("- - - - - - - - - - - - - - - - - -") ; for(i = 0 ; i <= 180 ; i = i + 30) { r = i * 3.14159 / 180 ; printf("\n%3d \t %5.2f \t %5.2f \t %5.2f\n", i, sin(r), cos(r), tan(r)); } printf("- - - - - - - - - - - - - - - - - -") ; getch() ; } RUN 1 : ~~~~~~~ - - - - - - - - - - - - - - - - - Angle Sin Cos Tan - - - - - - - - - - - - - - - - - 0 0.00 1.00 0.00 30 60 90 120 150 0.50 0.87 1.00 0.87 0.50 0.87 0.50 0.00 -0.50 -0.87 0.58 1.73 788898.12 -1.73 -0.58

180 0.00 -1.00 -0.00 - - - - - - - - - - - - - - - - - -

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

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

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

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

A.58 Programs in C
/* Program to generate odd and even numbers - GENODDEV.C */ # include <stdio.h> # include <conio.h> void main() { int n, i ; clrscr() ; printf("Enter the limit : ") ; scanf("%d", &n) ; printf("\nThe odd numbers are :\n\n") ; for(i = 1 ; i <= n ; i = i + 2) printf("%d\t", i) ; printf("\n\nThe even numbers are :\n\n") ; for(i = 2 ; i <= n ; i = i + 2) printf("%d\t", i) ; 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

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

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

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

A.62 Programs in C

Matrix Manipulation Programs


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

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

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

\n\n") ;

is : %d\n\n",small); : %d", big) ;

The smallest element in the matrix is : 1 The biggest element in the matrix is : 9

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

%d\n",upper); %d", lower) ;

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

The sum of upper triangular elements is : 26 The sum of lower triangular elements is : 34

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

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

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

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

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

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

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

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

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

A.76 Programs in C

String Handling Programs


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

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

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

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

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

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

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

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

A.84 Programs in C
RUN 1 : ~~~~~~~ Enter the first string : Anna Enter the second string : Madras String 1 = Anna & String 2 = Madras - Length is : 4 and 6 String 1 = Anna & String 2 = Madras - Uppercase is : ANNA and MADRAS String 1 = ANNA & String 2 = MADRAS - Lowercase is : anna and madras 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 - Concatenation is : sardamsardam String 1 = sardamsardam & String 2 = sardam

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

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

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

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

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

B.Bhuvaneswaran A.93

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

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

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

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

B.Bhuvaneswaran A.97

File Handling Programs


/* Program to write and read data from a file - FILEWRRD.C */ # include <stdio.h> # include <conio.h> void main() { char c ; FILE *fptr1 ; 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") ; fptr1 = fopen("COURSES.DAT","w") ; while((c = getc(stdin)) != EOF) fputc(c, fptr1) ; fclose(fptr1) ; printf("\nThe content of the file is : \n\n") ; fptr1 = fopen("COURSES.DAT", "r") ; do { c = fgetc(fptr1) ; putchar(c) ; } while(c != EOF) ; fclose(fptr1) ; 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 Science & Engineering Information Technology Electronics & Communication Engineering ^Z The content of the file is : Computer Science & Engineering Information Technology Electronics & Communication Engineering

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

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

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

B.Bhuvaneswaran A.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. Name 101 157 Udaya Viji Sub1 75 60 Sub2 80 70 70 Total 155 130 80

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

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

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

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

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

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

B.Bhuvaneswaran A.111

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

The sorted elemets are : 10 20 30 40 50

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

The sorted elemets are : 10 20 30 40 50

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

The sorted elemets are : 10 20 30 40 50

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

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

The sorted elemets are : 10 20 30 40 50

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

The sorted elemets are : 10 20 30 40 50

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

Data Structures Programs


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

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

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

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

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

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

A.124 Programs in C

Numerical Methods Programs


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

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

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

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

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

You might also like