Upcoming SlideShare
×

# Simple C programs

2,237 views
2,141 views

Published on

Simple C Program..

Published in: Education
1 Like
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

Views
Total views
2,237
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
361
0
Likes
1
Embeds 0
No embeds

No notes for slide

### Simple C programs

1. 1. 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 : nn") ; printf("a = %d t b = %d", a, b) ; a = a + b ; b = a - b ; a = a - b ; printf("nnAfter swapping : nn") ; printf("a = %d t b = %d", a, b) ; getch() ;}RUN 1 :~~~~~~~Enter two numbers : 10 20Before swapping :a = 10 b = 20After swapping :a = 20 b = 10
2. 2. 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 : nn") ; printf("a = %d t b = %d", a, b) ; c = a ; a = b ; b = c ; printf("nnAfter swapping : nn") ; printf("a = %d t b = %d", a, b) ; getch();}RUN 1 :~~~~~~~Enter two numbers : 10 20Before swapping :a = 10 b = 20After swapping :a = 20 b = 10
3. 3. 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("nnEnter your choice <1...4> : ") ; scanf("%d", &ch) ; switch(ch) { case 1 : printf("n%d + %d = %d", n1, n2, n1 + n2) ; break ; case 2 : printf("n%d - %d = %d", n1, n2, n1 - n2) ; break ; case 3 : printf("n%d * %d = %d", n1, n2, n1 * n2); break ; case 4 : printf("n%d / %d = %.2f", n1, n2, (float)n1 / n2); break ; default : printf("nInvalid choice"); break ; } getch();}
4. 4. A.4 Programs in CRUN 1 :~~~~~~~Enter the first number : 10Enter the second number : 5[1] -> Addition[2] -> Subtraction[3] -> Multiplication[4] -> DivisionEnter your choice <1...4> : 310 * 5 = 50
5. 5. 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 :nn") ; do { c = 1 ; do { y = r * c ; printf("%5d", y) ; c = c + 1 ; } while(c <= 10) ; printf("nn") ; 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
6. 6. 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("nnThe no. of hundreds are : %d", b) ; break ; } while(rs >= 50) { c = rs / 50 ; printf("nnThe no. of fifties are : %d", c) ; break ; } while(rs >= 20) { d = rs / 20 ; printf("nnThe no. of twenties are : %d", d) ; break ; } while(rs >= 10) { e = rs / 10 ; printf("nnThe no. of tens are : %d", e) ; break ; } while(rs >= 5) { f = rs / 5 ; printf("nnThe no. of fives are : %d", f) ; break ; }
7. 7. B.Bhuvaneswaran A.7 while(rs >= 2) { g = rs / 2 ; printf("nnThe no. of Twos are : %d", g) ; break ; } while(rs >= 1) { h = rs / 1 ; printf("nnThe no. of ones are : %d", h) ; break ; } getch() ;}RUN 1 :~~~~~~~Enter the amount in Rupees : 179The no. of hundreds are : 1The no. of fifties are : 3The no. of twenties are : 8The no. of tens are : 17The no. of fives are : 35The no. of Twos are : 89The no. of ones are : 179
8. 8. 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 :nn") ; for(i = 1 ; i <= n ; i++) printf("%4d + %4d = %4dn", i, t, i + t) ; getch() ;}RUN 1 :~~~~~~~Enter the table : 5Enter the limit : 15The table is : 1 + 5 = 6 2 + 5 = 7 3 + 5 = 8 4 + 5 = 9 5 + 5 = 10 6 + 5 = 11 7 + 5 = 12 8 + 5 = 13 9 + 5 = 14 10 + 5 = 15 11 + 5 = 16 12 + 5 = 17 13 + 5 = 18 14 + 5 = 19 15 + 5 = 20
9. 9. 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 :nn") ; for(i = 1 ; i <= n ; i++) printf("%4d x %4d = %4dn", i, t, i * t) ; getch() ;}RUN 1 :~~~~~~~Enter the table : 5Enter the limit : 15The table is : 1 x 5 = 5 2 x 5 = 10 3 x 5 = 15 4 x 5 = 20 5 x 5 = 25 6 x 5 = 30 7 x 5 = 35 8 x 5 = 40 9 x 5 = 45 10 x 5 = 50 11 x 5 = 55 12 x 5 = 60 13 x 5 = 65 14 x 5 = 70 15 x 5 = 75
10. 10. 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 20 3030 is the biggest numberRUN 2 :~~~~~~~Enter three numbers : 20 30 1030 is the biggest numberRUN 3 :~~~~~~~Enter three numbers : 30 10 2030 is the biggest number
11. 11. 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 20 30The biggest number is : 30RUN 2 :~~~~~~~Enter three numbers : 20 30 10The biggest number is : 30RUN 3 :~~~~~~~Enter three numbers : 30 10 20The biggest number is : 30
12. 12. 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 : 5Average of first 5 numbers is : 3.00
13. 13. 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 : 179The number of digits is : 3
14. 14. 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 : 12345The sum of the digits is : 15
15. 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 :nn", d) ; for(i = 1 ; i <= n ; i++) if(i % d == 0) printf("%dt", i) ; getch() ;}RUN 1 :~~~~~~~Enter the limit : 15Enter the number : 3The numbers divisible by 3 are :3 6 9 12 15
16. 16. 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 :nn") ; for(i = 1 ; i <= n ; i++) if(n % i == 0) printf("%dt", i) ; getch() ;}RUN 1 :~~~~~~~Enter the number : 15The divisors are :1 3 5 15
17. 17. 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 : 2468The Reversed Numeral is : 8642
18. 18. 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("nnThe even numbers sum is : %d", esum) ; getch() ;}RUN 1 :~~~~~~~Enter the limit : 10The odd numbers sum is : 25The even numbers sum is : 30
19. 19. 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 : nn") ; for(i = 1 ; i <= n ; i++) { c = a + b ; printf("%d t", c) ; sum = sum + c ; a = b ; b = c ; } printf("nnThe sum of the fibonacci series is : %d", sum) ; getch() ;}RUN 1 :~~~~~~~Enter the limit : 5The fibonacci series is :0 1 1 2 3The sum of the fibonacci series is : 7
20. 20. 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/1977The day is : Wednesday
21. 21. 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 : 10The given number is evenRUN 2 :~~~~~~~Enter a number : 5The given number is odd
22. 22. 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 : 16The person is in teen age.RUN 2 :~~~~~~~Enter the age : 21The person is not in teen age.
23. 23. 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 : 20The person is eligible to vote.RUN 2 :~~~~~~~Enter the age : 16The person is not eligible to vote.
24. 24. 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 : 66 is a perfect numberRUN 2 :~~~~~~~Enter a number : 2828 is a perfect numberRUN 3 :~~~~~~~Enter a number : 1212 is not a perfect number
25. 25. 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 : 8181 is a perfect squareRUN 2 :~~~~~~~Enter a number : 1212 is not a perfect square
26. 26. 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 : 6The number is not primeRUN 2 :~~~~~~~Enter the Number : 11The number is prime
27. 27. 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 : 153153 is an armstrong numberRUN 2 :~~~~~~~Enter a number : 123123 is not an armstrong number
28. 28. 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 :nn") ; for(i = 0 ; i < n ; i++) scanf("%d", &a[i]) ; printf("nThe positive elements are :nn") ; for(i = 0 ; i < n ; i++) { if(a[i] > 0) printf("%dt", a[i]) ; } getch() ;}RUN 1 :~~~~~~~Enter the limit : 5Enter the elements :10 -20 30 -40 -50The positive elements are :10 30
29. 29. 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 :nn") ; for(i = 0 ; i < n ; i++) scanf("%d", &a[i]) ; printf("nThe negative elements are :nn") ; for(i = 0 ; i < n ; i++) { if(a[i] < 0) printf("%dt", a[i]) ; } getch() ;}RUN 1 :~~~~~~~Enter the limit : 5Enter the elements :10 -20 30 -40 -50The negative elements are :-20 -40 -50
30. 30. 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 :nn") ; 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("nnSum of negative elements is : %d", nsum) ; getch() ;}RUN 1 :~~~~~~~Enter the limit : 5Enter the elements :-10 30 50 -20 40Sum of positive elements is : 120Sum of negative elements is : -30
31. 31. 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 :nn") ; for(i = 0 ; i < n ; i++) scanf("%d", &a[i]) ; printf("nThe reversed array is :nn") ; for(i = n - 1 ; i >= 0 ; i--) printf("%dt", a[i]) ; getch() ;}RUN 1 :~~~~~~~Enter the limit : 5Enter the elements :20 30 50 10 40The reversed array is :40 10 50 30 20
32. 32. 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 : 5Factorial value of 5 is : 120
33. 33. 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 : 7Enter the value for R : 5The NCR value is : 21RUN 2 :~~~~~~~Enter the value for N : 5Enter the value for R : 7Calculating NCR value is not possible
34. 34. 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 : 2Enter the value of n : 4The power of 2^4 is : 16
35. 35. 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("nnThe LCM is : %d", lcm); getch() ;}RUN 1 :~~~~~~~Enter the two numbers : 10 8The GCD is : 2The LCM is : 40
36. 36. 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 imaginaryn") ; 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 equaln") ; r1 = - b / (2 * a) ; printf("nr1 = r2 = %7.2f", r1) ; break ; case 3 : printf("nRoots are real and unequaln") ; 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() ;}
37. 37. B.Bhuvaneswaran A.37RUN 1 :~~~~~~~Enter the values of A, B & C : 0.0 4.0 7.0Equation is linearRUN 2 :~~~~~~~Enter the values of A, B & C : 1.0 2.0 7.0Roots are imaginaryr1 = -1.00 + j 2.45r2 = -1.00 - j 2.45RUN 3 :~~~~~~~Enter the values of A, B & C : 1.0 2.0 1.0Roots are real and equalr1 = r2 = -1.00RUN 4 :~~~~~~~Enter the Values of A, B & C : 2.0 7.0 1.0Roots are real and unequalr1 = -0.15r2 = -3.35
38. 38. 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 :nn") ; 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() ;}
39. 39. B.Bhuvaneswaran A.39RUN 1 :~~~~~~~Enter the number of equations : 3Enter the co-efficients of the equations :a[1][1] = 10a[1][2] = 1a[1][3] = 1b[1] = 12a[2][1] = 2a[2][2] = 10a[2][3] = 1b[2] = 13a[3][1] = 1a[3][2] = 1a[3][3] = 5b[3] = 7The result is :x[1] = 1.00x[2] = 1.00x[3] = 1.00
40. 40. 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 : 45The farenheit is : 113.00
41. 41. 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 : 113The centigrade is : 45.00
42. 42. 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() ;}
43. 43. B.Bhuvaneswaran A.43RUN 1 :~~~~~~~Enter a number <1 to 10> : 7SEVEN
44. 44. 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 ;
45. 45. B.Bhuvaneswaran A.45 case 9 : printf("NINE ") ; break ; } } getch() ;}RUN 1 :~~~~~~~Enter a number : 225589TWO TWO FIVE FIVE EIGHT NINE
46. 46. 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 : 12The binary equivalent is : 1100
47. 47. 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 : 1100The equivalent decimal number is : 12RUN 2 :~~~~~~~Enter the binary number : 15The number is not a binary number
48. 48. 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 : 60Enter the value for n : 20Cosine value of 60.000000 is : 0.5000
49. 49. 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 : 30Enter the value for n : 20Sine value of 30.000000 is : 0.5000
50. 50. 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 : 2Enter the value for n : 20Exponential Value of 2.000000 is : 7.3891
51. 51. B.Bhuvaneswaran A.51 Series Generation Programs/* Program to generate Floyds 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("nFloyds triangle is : nn") ; for(i = 0 ; i < r ; i++) { for(j = 0 ; j <= i ; j++) { printf("%-6d", c) ; c++ ; } printf("nn") ; } getch() ;}RUN 1 :~~~~~~~Enter the number of rows : 5Floyds triangle is :12 34 5 67 8 9 1011 12 13 14 15
52. 52. A.52 Programs in C/* Program to generate Pascals 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("nPascals triangle is : nn") ; 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("nn") ; ++q ; } getch() ;}RUN 1 :~~~~~~~Enter the number of rows : 5Pascals triangle is : 1 1 1 1 2 1 1 3 3 1 1 4 6 4 1
53. 53. 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.2fn", i, sin(r), cos(r), tan(r)); } printf("- - - - - - - - - - - - - - - - - -") ; getch() ;}RUN 1 :~~~~~~~- - - - - - - - - - - - - - - - - -Angle Sin Cos Tan- - - - - - - - - - - - - - - - - - 0 0.00 1.00 0.0030 0.50 0.87 0.5860 0.87 0.50 1.7390 1.00 0.00 788898.12120 0.87 -0.50 -1.73150 0.50 -0.87 -0.58180 0.00 -1.00 -0.00- - - - - - - - - - - - - - - - - -
54. 54. 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) ; } }}
55. 55. B.Bhuvaneswaran A.55RUN 1 :~~~~~~~Enter the string : abcThe permutation is :abcacbbacbcacabcba
56. 56. 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 :nn", 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("%dt", a[i][j]) ; } printf("nn") ; } end : ; getch() ;}
57. 57. B.Bhuvaneswaran A.57RUN 1 :~~~~~~~Enter the size of the magic square : 3The magic square for 3 x 3 is :8 1 63 5 74 9 2RUN 2 :~~~~~~~Enter the size of the magic square : 4Magic square is not possible
58. 58. 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 :nn") ; for(i = 1 ; i <= n ; i = i + 2) printf("%dt", i) ; printf("nnThe even numbers are :nn") ; for(i = 2 ; i <= n ; i = i + 2) printf("%dt", i) ; getch() ;}RUN 1 :~~~~~~~Enter the limit : 10The odd numbers are :1 3 5 7 9The even numbers are :2 4 6 8 10
59. 59. 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 : nn") ; for(i = 1 ; i <= n ; i++) { c = a + b ; printf("%d t", c) ; a = b ; b = c ; } getch() ;}RUN 1 :~~~~~~~Enter the limit : 7The fibonacci series is :0 1 1 2 3 5 8
60. 60. 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 :nn") ; for (i = 1 ; i <= n ; i++) { if(i <= 3) { printf("%dt", i) ; } else { for (j = 2; j <= i / 2 ; j ++) { if (i % j == 0) goto loop ; } printf("%dt", i) ; loop : ; } } getch() ;}RUN 1 :~~~~~~~Enter the limit : 10The prime numbers are :1 2 3 5 7
61. 61. 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 :nn") ; 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("%dt", i) ; } getch() ;}RUN 1 :~~~~~~~Enter the limit : 1000The armstrong numbers are :0 1 153 370 371 407
62. 62. A.62 Programs in CMatrix 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 : nn") ; 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 3Enter the elements of the matrix :1 2 34 5 67 8 9The sum of the elements in the matrix is : 45
63. 63. 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 : nn") ; 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 : 3Enter the elements of the matrix :1 2 34 5 67 8 9The sum of diagonal elements in the matrix is : 15
64. 64. A.64 Programs in C/* Find smallest & biggest elements of the given matrix - MATNUM.C */# 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 : nn") ; 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 is : %dnn",small); printf("The biggest element in the matrix is : %d", big) ; getch() ;}RUN 1 :~~~~~~~Enter the order of the matrix : 3 3Enter the elements of the matrix :4 5 69 7 81 2 3The smallest element in the matrix is : 1The biggest element in the matrix is : 9
65. 65. B.Bhuvaneswaran A.65/* Find the sum of upper & lower traiangular elements - MATUPLOW.C */# 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 : nn") ; for(i = 0 ; i < size ; i++) for(j = 0 ; j < size ; j++) scanf("%d", &mat[i][j]) ; printf("nThe upper triangular matrix is : nn") ; for(i = 0 ; i < size ; i++) { for(j = 0 ; j < size ; j++) { if(i <= j) { printf("%dt", mat[i][j]) ; upper = upper + mat[i][j] ; } else printf("t") ; } printf("n") ; } printf("nThe lower triangular matrix is : nn") ; for(i = 0 ; i < size ; i++) { for(j = 0 ; j < size ; j++) { if(j <= i) { printf("%dt", mat[i][j]) ; lower = lower + mat[i][j] ; } else printf("t") ; } printf("n") ; } printf("nThe sum of upper triangular elements is : %dn",upper); printf("nThe sum of lower triangular elements is : %d", lower) ; getch() ;}
66. 66. A.66 Programs in CRUN 1 :~~~~~~~Enter size of the square matrix : 3Enter the elements of the matrix :1 2 34 5 67 8 9The upper triangular matrix is :1 2 3 5 6 9The lower triangular matrix is :14 57 8 9The sum of upper triangular elements is : 26The sum of lower triangular elements is : 34
67. 67. 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 : nn") ; 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 : 3Enter the elements of the matrix :1 0 00 1 00 0 1The matrix is an unit matrix
68. 68. A.68 Programs in CRUN 2 :~~~~~~~Enter size of the square matrix : 3Enter the elements of the matrix :1 2 34 1 56 7 1The matrix is not an unit matrix
69. 69. 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 : nn") ; for(i = 0 ; i < row ; i++) for(j = 0 ; j < col ; j++) scanf("%d", &mat[i][j]) ; printf("nThe transpose matrix is : nn") ; 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 3Enter the elements of the matrix :1 2 34 5 6The transpose matrix is :1 42 53 6
70. 70. 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 : nn") ; for(i = 0 ; i < row ; i++) for(j = 0 ; j < col ; j++) scanf("%d", &mata[i][j]) ; printf("nEnter the elements of second matrix : nn") ; 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 : nn") ; for(i = 0 ; i < row ; i++) { for(j = 0 ; j < col ; j++) { printf("%d t", matc[i][j]) ; } printf("n") ; } getch() ;}
71. 71. B.Bhuvaneswaran A.71RUN 1 :~~~~~~~Enter the order of the matrix : 3 3Enter the elements of first matrix :1 3 57 9 1113 15 17Enter the elements of second matrix :2 4 68 10 1214 16 18The resultant matrix is :3 7 1115 19 2327 31 35
72. 72. 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 : nn") ; for(i = 0 ; i < row ; i++) for(j = 0 ; j < col ; j++) scanf("%d", &mata[i][j]) ; printf("nEnter the elements of second matrix : nn") ; 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 : nn") ; for(i = 0 ; i < row ; i++) { for(j = 0 ; j < col ; j++) { printf("%d t", matc[i][j]) ; } printf("n") ; } getch() ;}
73. 73. B.Bhuvaneswaran A.73RUN 1 :~~~~~~~Enter the order of the matrix : 3 3Enter the elements of first matrix :5 10 1520 25 3035 40 45Enter the elements of second matrix :2 4 68 10 1214 16 18The resultant matrix is :3 6 912 15 1821 24 27
74. 74. 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 : nn") ; for(i = 0 ; i < row1 ; i++) for(j = 0 ; j < col1 ; j++) scanf("%d", &mata[i][j]) ; printf("nEnter the elements of second matrix : nn") ; 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 : nn") ; 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() ;}
75. 75. B.Bhuvaneswaran A.75RUN 1 :~~~~~~~Enter the order of first matrix : 3 3Enter the order of second matrix : 3 3Enter the elements of first matrix :1 1 11 1 11 1 1Enter the elements of second matrix :1 1 11 1 11 1 1The resultant matrix is :3 3 33 3 33 3 3RUN 2 :~~~~~~~Enter the order of first matrix : 3 3Enter the order of second matrix : 2 2Matrix Multiplication is not possible ...
76. 76. 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 : nn") ; 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 65 B 66 C 67 D 68 E69F 70 G 71 H 72 I 73 J74K 75 L 76 M 77 N 78 O79P 80 Q 81 R 82 S 83 T84U 85 V 86 W 87 X 88 Y89Z 90 a 97 b 98 c 99 d100e 101 f 102 g 103 h 104 i105j 106 k 107 l 108 m 109 n110o 111 p 112 q 113 r 114 s115t 116 u 117 v 118 w 119 x120y 121 z 122
77. 77. 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 : uu is a vowelRUN 2 :~~~~~~~Enter the character : vv is not a vowel
78. 78. 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 : welcomeThe length of the string is : 7
79. 79. 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 : bEntered character is a lower case letterRUN 2 :~~~~~~~Enter a charatcer : VEntered character is a upper case letterRUN 3 :~~~~~~~Enter a charatcer : 2Entered character is a numberRUN 4 :~~~~~~~Enter a charatcer : #Entered character is a special character
80. 80. 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 : nn") ; while(str[i] != 0) { if(str[i] != ) printf("%c", str[i] + 32) ; else putchar( ) ; i++ ; } getch() ;}RUN 1 :~~~~~~~Enter the text in uppercase : Anna UniversityThe converted text is :anna university
81. 81. 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("nnNumber of vowels = %d", vowel) ; printf("nnNumber of consonants = %d", cons) ; printf("nnNumber of digits = %d", digit) ; printf("nnNumber of special characters = %d", special) ; printf("nnNumber of words = %d", word) ; getch() ;}RUN 1 :~~~~~~~Enter the text : Bhuvan was born in 2nd Nov 1977 !!!The text contains :Number of vowels = 6Number of consonants = 14Number of digits = 5Number of special characters = 3Number of words = 8
82. 82. 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 : AnnaEnter the second string : UniversityThe concatenation string is : AnnaUniversity
83. 83. 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 : nn") ; gets(str1) ; printf("nEnter the second string : nn") ; gets(str2) ; printf("nString 1 = %s & String 2 = %s ", str1, str2) ; printf("- Length is : %d and %d", strlen(str1), strlen(str2)) ; printf("nnString 1 = %s & String 2 = %s ", str1, str2) ; printf("- Uppercase is : %s and %s", strupr(str1), strupr(str2)); printf("nnString 1 = %s & String 2 = %s ", str1, str2) ; printf("- Lowercase is : %s and %s", strlwr(str1), strlwr(str2)); printf("nnString 1 = %s & String 2 = %s ", str1, str2) ; printf("- Reverse is : %s and %s", strrev(str1), strrev(str2)) ; printf("nnString 1 = %s & String 2 = %s ", str1, str2) ; printf("- String copy is : %s ", strcpy(str1,str2)); printf("nnString 1 = %s & String 2 = %s ", str1, str2) ; printf("- Concatenation is : %s ", strcat(str1,str2)); printf("nnString 1 = %s & String 2 = %s ", str1, str2) ; getch() ;}
84. 84. A.84 Programs in CRUN 1 :~~~~~~~Enter the first string :AnnaEnter the second string :MadrasString 1 = Anna & String 2 = Madras - Length is : 4 and 6String 1 = Anna & String 2 = Madras - Uppercase is : ANNA and MADRASString 1 = ANNA & String 2 = MADRAS - Lowercase is : anna and madrasString 1 = anna & String 2 = madras - Reverse is : anna and sardamString 1 = anna & String 2 = sardam - String copy is : sardamString 1= sardam & String 2= sardam - Concatenation is : sardamsardamString 1 = sardamsardam & String 2 = sardam
85. 85. 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 : nn") ; 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 UniversityEnter the character to be search : nThe character n appears 3 times in the text
86. 86. 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 : %snn", str) ; printf("The reversed string is : %s", rev) ; getch() ;}RUN 1 :~~~~~~~Enter a string : bhuvanThe given string is : bhuvanThe reversed string is : navuhb
87. 87. 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 : madamThe given string is a palindromeRUN 2 :~~~~~~~Enter a string : malayalamThe given string is a palindromeRUN 3 :~~~~~~~Enter a string : lirilThe given string is a palindromeRUN 4 :~~~~~~~Enter a string : bhuvanThe given string is not a palindrome
88. 88. 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 : nn") ; 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 : nn") ; for(i = 0 ; i < n ; i++) printf("%s n", str[i]) ; getch() ;}RUN 1 :~~~~~~~Enter the number of strings : 5Enter the strings :vijiudayapriyabhuvansatishThe sorted order of strings are :bhuvanpriyasatishudayaviji
89. 89. 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 ttSub1t Sub2t Totalt Averagenn") ; for(i = 0 ; i < n ; i++) { printf("%d t %s tt %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. 90. A.90 Programs in CRUN 1 :~~~~~~~Enter the number of students : 2Enter the roll number : 101Enter the name : ArunEnter the marks in 2 subjects : 75 85Enter the roll number : 102Enter the name : BabuEnter the marks in 2 subjects : 65 75Roll No. Name Sub1 Sub2 Total Average101 Arun 75 85 160 80.00102 Babu 65 75 140 70.00
91. 91. 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 nn") ; 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() ;}
92. 92. A.92 Programs in CRUN 1 :~~~~~~~Enter the number of employees : 2Enter the employee number : 101Enter the name : ArunEnter the basic pay, allowances & deductions : 5000 1000 250Enter the employee number : 102Enter the name : BabuEnter the basic pay, allowances & deductions : 7000 1500 750Emp. No. Name Bpay Allow Ded Npay101 Arun 5000 1000 250 5750102 Babu 7000 1500 750 7750
93. 93. 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 : bhuvanThe length of the string is : 6
94. 94. 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 : bhuvanThe copied string is : bhuvan
95. 95. 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 : helloEnter the second string : worldThe concatenated string is : helloworld
96. 96. 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 : cseEnter the second string : eceThe given strings are not equalRUN 2 :~~~~~~~Enter the first string : mechEnter the second string : mechThe given strings are equal
97. 97. 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: nn") ; fptr1 = fopen("COURSES.DAT","w") ; while((c = getc(stdin)) != EOF) fputc(c, fptr1) ; fclose(fptr1) ; printf("nThe content of the file is : nn") ; 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 & EngineeringInformation TechnologyElectronics & Communication Engineering^ZThe content of the file is :Computer Science & EngineeringInformation TechnologyElectronics & Communication Engineering
98. 98. 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("%dt", num) ; printf("nnContents of EVEN file is : ") ; while((num = getw(fptr3)) != EOF) printf("%dt", num) ; fclose(fptr2) ; fclose(fptr3) ; getch() ;}
99. 99. B.Bhuvaneswaran A.99RUN 1 :~~~~~~~Enter number of values : 6Enter the values : 11 22 33 44 55 66Contents of ODD file is : 11 33 55Contents of EVEN file is : 22 44 66
100. 100. 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 tt Sub1 t Sub2 t Totalnn") ; for(i = 0 ; i < n ; i++) { fscanf(fptr,"%d %s %d %d n", &rollno, name, &s1, &s2) ; printf("%d t %s tt %d t %d t %d n", rollno, name, s1, s2, s1 + s2) ; } fclose(fptr) ; getch() ;}
101. 101. B.Bhuvaneswaran A.101RUN 1 :~~~~~~~Enter the number of students : 2Enter the roll number : 101Enter the name : UdayaEnter the marks in 2 subjects : 75 80Enter the roll number : 157Enter the name : VijiEnter the marks in 2 subjects : 60 70Roll No. Name Sub1 Sub2 Total101 Udaya 75 80 155157 Viji 60 70 130
102. 102. 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.Namett Bpaytt Allowtt Dedtt Npaynn"); for(i = 0 ; i < n ; i++) { fscanf(fptr,"%d%s%f%f%fn", &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() ;}
103. 103. B.Bhuvaneswaran A.103RUN 1 :~~~~~~~Enter the number of employees : 2Enter the employee number : 101Enter the name : UdayaEnter the basic pay, allowances & deductions : 6000 1000 500Enter the employee number : 102Enter the name : PriyaEnter the basic pay, allowances & deductions : 5000 1000 450Emp. No. Name Bpay Allow Ded Npay101 Udaya 6000.00 1000.00 500.00 6500.00102 Priya 5000.00 1000.00 450.00 5550.00
104. 104. 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 : nn") ; 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 : nn") ; 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 : nn") ; fptr3 = fopen("MERGE.DAT", "r") ; while((c = fgetc(fptr1)) != EOF) putchar(c) ; fclose(fptr3) ; getch() ;}
105. 105. B.Bhuvaneswaran A.105RUN 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 - II - Semester^ZEnter the text to be stored in the file - 2.Use ^Z or F6 at the end of the text and press ENTER :Computer Practice - IIII - Semester^ZThe content of the merged file is :Computer Practice - II - SemesterComputer Practice - IIII - Semester
106. 106. 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 : nn") ; fptr = fopen("ENCDEC.DAT","w") ; while((c = getchar()) != EOF) fputc(c, fptr) ; fclose(fptr) ; printf("nnData output in encrypted form : nn") ; fptr = fopen("ENCDEC.DAT", "r") ; while((c = fgetc(fptr)) != EOF) printf("%c", c+1) ; fclose(fptr) ; printf("nnData output in decrypted form : nn") ; 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 - IID.D. Publications^ZData output in encrypted form :Dpnqvufs!Qsbdujdf!.!JJE/E/!QvcmjdbujpotData output in decrypted form :Computer Practice - IID.D. Publications
107. 107. 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 :nn") ; 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 : nn") ; for(i = 0 ; i < n ; i++) printf("%dt", a[i]) ; printf("nnEnter the element to be searched : ") ; scanf("%d", &k) ; f = 0 ; l = n - 1 ; while(f <= l) { mid = (f + l) / 2 ; if(a[mid] == k) { flag = 1 ; break ; } else if(a[mid] < k) f = mid + 1 ; else l = mid - 1 ; } if(flag == 1) printf("nThe element is found at location : %d", mid + 1) ; else printf("nThe element is not found") ; getch() ;}
108. 108. A.108 Programs in CRUN 1 :~~~~~~~Enter the limit : 5Enter the elements :20 40 30 50 10The ordered elements are :10 20 30 40 50Enter the element to be searched : 30The element is found at location : 3RUN 2 :~~~~~~~Enter the limit : 5Enter the elements :20 40 30 50 10The ordered elements are :10 20 30 40 50Enter the element to be searched : 70The element is not found
109. 109. 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 :nn") ; 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() ;}
110. 110. A.110 Programs in CRUN 1 :~~~~~~~Enter the limit : 5Enter the numbers :20 40 30 50 10Enter the element to be searched : 30The element is found at location : 3RUN 2 :~~~~~~~Enter the limit : 5Enter the numbers :20 40 30 50 10Enter the element to be searched : 70The element is not found
111. 111. 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 :nn") ; 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 :nn") ; for(i = 0 ; i < n ; i++) printf("%dt", a[i]) ; getch() ;}RUN 1 :~~~~~~~Enter the limit : 5Enter the numbers :20 40 30 50 10The sorted elemets are :10 20 30 40 50
112. 112. 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 :nn") ; 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 :nn") ; for(i = 0 ; i < n ; i++) printf("%dt", a[i]) ; getch() ;}RUN 1 :~~~~~~~Enter the limit : 5Enter the elements :20 40 30 50 10The sorted elemets are :10 20 30 40 50
113. 113. 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 :nn") ; 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 :nn") ; for(i = 0 ; i < n ; i++) printf("%dt", a[i]) ; getch() ;}RUN 1 :~~~~~~~Enter the limit : 5Enter the elements :20 40 30 50 10The sorted elemets are :10 20 30 40 50
114. 114. 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 :nn") ; for(i = 0 ; i < n ; i++) scanf("%d", &a[i]) ; a[i] = MAXINT ; qsort(0, n - 1) ; printf("nThe sorted elemets are :nn") ; for(i = 0 ; i < n ; i++) printf("%dt", 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 ;
115. 115. B.Bhuvaneswaran A.115 qsort(left, j - 1) ; qsort(j + 1, right) ; } return ;}RUN 1 :~~~~~~~Enter the limit : 5Enter the elements :20 40 30 50 10The sorted elemets are :10 20 30 40 50