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 20
Before swapping :
a = 10 b = 20
After swapping :
a = 20 b = 10
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 20
Before swapping :
a = 10 b = 20
After swapping :
a = 20 b = 10
4. A.4 Programs in C
RUN 1 :
~~~~~~~
Enter the first number : 10
Enter the second number : 5
[1] -> Addition
[2] -> Subtraction
[3] -> Multiplication
[4] -> Division
Enter your choice <1...4> : 3
10 * 5 = 50
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. 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. 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 : 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
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 : 5
Enter the limit : 15
The 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. 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 : 5
Enter the limit : 15
The 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. 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 30
30 is the biggest number
RUN 2 :
~~~~~~~
Enter three numbers : 20 30 10
30 is the biggest number
RUN 3 :
~~~~~~~
Enter three numbers : 30 10 20
30 is the biggest number
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 30
The biggest number is : 30
RUN 2 :
~~~~~~~
Enter three numbers : 20 30 10
The biggest number is : 30
RUN 3 :
~~~~~~~
Enter three numbers : 30 10 20
The biggest number is : 30
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 : 5
Average of first 5 numbers is : 3.00
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 : 179
The number of digits is : 3
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 : 12345
The sum of the digits is : 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 : 15
Enter the number : 3
The numbers divisible by 3 are :
3 6 9 12 15
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 : 15
The divisors are :
1 3 5 15
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 : 2468
The Reversed Numeral is : 8642
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 : 10
The odd numbers sum is : 25
The even numbers sum is : 30
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 : 5
The fibonacci series is :
0 1 1 2 3
The sum of the fibonacci series is : 7
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/1977
The day is : Wednesday
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 : 10
The given number is even
RUN 2 :
~~~~~~~
Enter a number : 5
The given number is odd
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 : 16
The person is in teen age.
RUN 2 :
~~~~~~~
Enter the age : 21
The person is not in teen age.
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 : 20
The person is eligible to vote.
RUN 2 :
~~~~~~~
Enter the age : 16
The person is not eligible to vote.
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 : 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
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 : 81
81 is a perfect square
RUN 2 :
~~~~~~~
Enter a number : 12
12 is not a perfect square
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 : 6
The number is not prime
RUN 2 :
~~~~~~~
Enter the Number : 11
The number is prime
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 : 153
153 is an armstrong number
RUN 2 :
~~~~~~~
Enter a number : 123
123 is not an armstrong number
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 : 5
Enter the elements :
10 -20 30 -40 -50
The positive elements are :
10 30
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 : 5
Enter the elements :
10 -20 30 -40 -50
The negative elements are :
-20 -40 -50
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 : 5
Enter the elements :
-10 30 50 -20 40
Sum of positive elements is : 120
Sum of negative elements is : -30
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 : 5
Enter the elements :
20 30 50 10 40
The reversed array is :
40 10 50 30 20
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 : 5
Factorial value of 5 is : 120
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 : 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
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 : 2
Enter the value of n : 4
The power of 2^4 is : 16
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 8
The GCD is : 2
The LCM is : 40
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. B.Bhuvaneswaran A.37
RUN 1 :
~~~~~~~
Enter the values of A, B & C : 0.0 4.0 7.0
Equation is linear
RUN 2 :
~~~~~~~
Enter the values of A, B & C : 1.0 2.0 7.0
Roots are imaginary
r1 = -1.00 + j 2.45
r2 = -1.00 - j 2.45
RUN 3 :
~~~~~~~
Enter the values of A, B & C : 1.0 2.0 1.0
Roots are real and equal
r1 = r2 = -1.00
RUN 4 :
~~~~~~~
Enter the Values of A, B & C : 2.0 7.0 1.0
Roots are real and unequal
r1 = -0.15
r2 = -3.35
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. B.Bhuvaneswaran A.39
RUN 1 :
~~~~~~~
Enter the number of equations : 3
Enter the co-efficients of the equations :
a[1][1] = 10
a[1][2] = 1
a[1][3] = 1
b[1] = 12
a[2][1] = 2
a[2][2] = 10
a[2][3] = 1
b[2] = 13
a[3][1] = 1
a[3][2] = 1
a[3][3] = 5
b[3] = 7
The result is :
x[1] = 1.00
x[2] = 1.00
x[3] = 1.00
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 : 45
The farenheit is : 113.00
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 : 113
The centigrade is : 45.00
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() ;
}
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. B.Bhuvaneswaran A.45
case 9 :
printf("NINE ") ;
break ;
}
}
getch() ;
}
RUN 1 :
~~~~~~~
Enter a number : 225589
TWO TWO FIVE FIVE EIGHT NINE
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 : 12
The binary equivalent is : 1100
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 : 1100
The equivalent decimal number is : 12
RUN 2 :
~~~~~~~
Enter the binary number : 15
The number is not a binary number
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 : 60
Enter the value for n : 20
Cosine value of 60.000000 is : 0.5000
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 : 30
Enter the value for n : 20
Sine value of 30.000000 is : 0.5000
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 : 2
Enter the value for n : 20
Exponential Value of 2.000000 is : 7.3891
51. 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 : 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 : 5
Floyd's triangle is :
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
52. 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 : 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 : 5
Pascal's triangle is :
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
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.00
30 0.50 0.87 0.58
60 0.87 0.50 1.73
90 1.00 0.00 788898.12
120 0.87 -0.50 -1.73
150 0.50 -0.87 -0.58
180 0.00 -1.00 -0.00
- - - - - - - - - - - - - - - - - -
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. B.Bhuvaneswaran A.55
RUN 1 :
~~~~~~~
Enter the string : abc
The permutation is :
abc
acb
bac
bca
cab
cba
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. B.Bhuvaneswaran A.57
RUN 1 :
~~~~~~~
Enter the size of the magic square : 3
The magic square for 3 x 3 is :
8 1 6
3 5 7
4 9 2
RUN 2 :
~~~~~~~
Enter the size of the magic square : 4
Magic square is not possible
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 : 10
The odd numbers are :
1 3 5 7 9
The even numbers are :
2 4 6 8 10
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 : 7
The fibonacci series is :
0 1 1 2 3 5 8
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 : 10
The prime numbers are :
1 2 3 5 7
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 : 1000
The armstrong numbers are :
0 1 153 370 371 407
62. 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 : 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 3
Enter the elements of the matrix :
1 2 3
4 5 6
7 8 9
The sum of the elements in the matrix is : 45
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 : 3
Enter the elements of the matrix :
1 2 3
4 5 6
7 8 9
The sum of diagonal elements in the matrix is : 15
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 3
Enter the elements of the matrix :
4 5 6
9 7 8
1 2 3
The smallest element in the matrix is : 1
The biggest element in the matrix is : 9
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. A.66 Programs in C
RUN 1 :
~~~~~~~
Enter size of the square matrix : 3
Enter the elements of the matrix :
1 2 3
4 5 6
7 8 9
The upper triangular matrix is :
1 2 3
5 6
9
The lower triangular matrix is :
1
4 5
7 8 9
The sum of upper triangular elements is : 26
The sum of lower triangular elements is : 34
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 : 3
Enter the elements of the matrix :
1 0 0
0 1 0
0 0 1
The matrix is an unit matrix
68. A.68 Programs in C
RUN 2 :
~~~~~~~
Enter size of the square matrix : 3
Enter the elements of the matrix :
1 2 3
4 1 5
6 7 1
The matrix is not an unit matrix
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 3
Enter the elements of the matrix :
1 2 3
4 5 6
The transpose matrix is :
1 4
2 5
3 6
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. B.Bhuvaneswaran A.71
RUN 1 :
~~~~~~~
Enter the order of the matrix : 3 3
Enter the elements of first matrix :
1 3 5
7 9 11
13 15 17
Enter the elements of second matrix :
2 4 6
8 10 12
14 16 18
The resultant matrix is :
3 7 11
15 19 23
27 31 35
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. B.Bhuvaneswaran A.73
RUN 1 :
~~~~~~~
Enter the order of the matrix : 3 3
Enter the elements of first matrix :
5 10 15
20 25 30
35 40 45
Enter the elements of second matrix :
2 4 6
8 10 12
14 16 18
The resultant matrix is :
3 6 9
12 15 18
21 24 27
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. B.Bhuvaneswaran A.75
RUN 1 :
~~~~~~~
Enter the order of first matrix : 3 3
Enter the order of second matrix : 3 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
3 3 3
3 3 3
RUN 2 :
~~~~~~~
Enter the order of first matrix : 3 3
Enter the order of second matrix : 2 2
Matrix Multiplication is not possible ...
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 E
69
F 70 G 71 H 72 I 73 J
74
K 75 L 76 M 77 N 78 O
79
P 80 Q 81 R 82 S 83 T
84
U 85 V 86 W 87 X 88 Y
89
Z 90 a 97 b 98 c 99 d
100
e 101 f 102 g 103 h 104 i
105
j 106 k 107 l 108 m 109 n
110
o 111 p 112 q 113 r 114 s
115
t 116 u 117 v 118 w 119 x
120
y 121 z 122
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 : u
u is a vowel
RUN 2 :
~~~~~~~
Enter the character : v
v is not a vowel
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 : welcome
The length of the string is : 7
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 : 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
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 University
The converted text is :
anna university
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 = 6
Number of consonants = 14
Number of digits = 5
Number of special characters = 3
Number of words = 8
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 : Anna
Enter the second string : University
The concatenation string is : AnnaUniversity
84. 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
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 University
Enter the character to be search : n
The character n appears 3 times in the text
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 : bhuvan
The given string is : bhuvan
The reversed string is : navuhb
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 : 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
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 : 5
Enter the strings :
viji
udaya
priya
bhuvan
satish
The sorted order of strings are :
bhuvan
priya
satish
udaya
viji
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. 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 85
Enter the roll number : 102
Enter the name : Babu
Enter the marks in 2 subjects : 65 75
Roll No. Name Sub1 Sub2 Total Average
101 Arun 75 85 160 80.00
102 Babu 65 75 140 70.00
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. 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 1000 250
Enter the employee number : 102
Enter the name : Babu
Enter the basic pay, allowances & deductions : 7000 1500 750
Emp. No. Name Bpay Allow Ded Npay
101 Arun 5000 1000 250 5750
102 Babu 7000 1500 750 7750
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 : bhuvan
The length of the string is : 6
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 : bhuvan
The copied string is : bhuvan
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 : hello
Enter the second string : world
The concatenated string is : helloworld
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 : 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
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 & Engineering
Information Technology
Electronics & Communication Engineering
^Z
The content of the file is :
Computer Science & Engineering
Information Technology
Electronics & Communication Engineering
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. B.Bhuvaneswaran A.99
RUN 1 :
~~~~~~~
Enter number of values : 6
Enter the values : 11 22 33 44 55 66
Contents of ODD file is : 11 33 55
Contents of EVEN file is : 22 44 66
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. 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 80
Enter the roll number : 157
Enter the name : Viji
Enter the marks in 2 subjects : 60 70
Roll No. Name Sub1 Sub2 Total
101 Udaya 75 80 155
157 Viji 60 70 130
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. 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 1000 500
Enter the employee number : 102
Enter the name : Priya
Enter the basic pay, allowances & deductions : 5000 1000 450
Emp. No. Name Bpay Allow Ded Npay
101 Udaya 6000.00 1000.00 500.00 6500.00
102 Priya 5000.00 1000.00 450.00 5550.00
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. 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
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 - 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
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. 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
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. 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
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 : 5
Enter the numbers :
20 40 30 50 10
The sorted elemets are :
10 20 30 40 50
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 : 5
Enter the elements :
20 40 30 50 10
The sorted elemets are :
10 20 30 40 50
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 : 5
Enter the elements :
20 40 30 50 10
The sorted elemets are :
10 20 30 40 50
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. 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
116. 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 :nn") ;
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 :nn") ;
for(i = 0 ; i < n ; i++)
printf("%dt", 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
117. 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 :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 numbers in ascending order is :nn") ;
for(i = 0 ; i < n ; i++)
printf("%dt", a[i]) ;
printf("nnThe numbers in descending order is :nn") ;
for(i = n -1 ; i >= 0 ; i--)
printf("%dt", 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
118. 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].Exitn") ;
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 optionn") ;
getch() ;
}
} while(ch != 4) ;
getch() ;
}
119. 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 : %dn", arr[top]) ;
getch() ;
top-- ;
}
void display()
{
if(top == -1)
{
printf("nStack is empty (underflow)n") ;
getch() ;
return ;
}
printf("nThe elements in stack are :nnTOP") ;
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
120. 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