Professional Documents
Culture Documents
C Progms
C Progms
C Progms
RUN 1 :
~~~~~~~
Before swapping :
a = 10 b = 20
After swapping :
a = 20 b = 10
A.2 Programs in C
/* Swapping two numbers (using three variables) - SWAPTHRE.C */
# include <stdio.h>
# include <conio.h>
void main()
{
int a, b, c ;
clrscr() ;
printf("Enter two numbers : ") ;
scanf("%d %d", &a, &b) ;
printf("\nBefore swapping : \n\n") ;
printf("a = %d \t b = %d", a, b) ;
c = a ;
a = b ;
b = c ;
printf("\n\nAfter swapping : \n\n") ;
printf("a = %d \t b = %d", a, b) ;
getch();
}
RUN 1 :
~~~~~~~
Before swapping :
a = 10 b = 20
After swapping :
a = 20 b = 10
B.Bhuvaneswaran A.3
10 * 5 = 50
B.Bhuvaneswaran A.5
RUN 1 :
~~~~~~~
1 2 3 4 5 6 7 8 9 10
2 4 6 8 10 12 14 16 18 20
3 6 9 12 15 18 21 24 27 30
4 8 12 16 20 24 28 32 36 40
5 10 15 20 25 30 35 40 45 50
6 12 18 24 30 36 42 48 54 60
7 14 21 28 35 42 49 56 63 70
8 16 24 32 40 48 56 64 72 80
9 18 27 36 45 54 63 72 81 90
10 20 30 40 50 60 70 80 90 100
A.6 Programs in C
/* Finding the number of 500, 100, 50, 20, 10, 5, 2, 1 - RUPEE.C */
# include <stdio.h>
# include <conio.h>
void main()
{
int rs, a, b, c, d, e, f, g, h ;
clrscr() ;
printf("Enter the amount in Rupees : ") ;
scanf("%d", &rs) ;
while(rs >= 500)
{
a = rs / 500 ;
printf("\nThe no. of five hundreds are : %d", a) ;
break ;
}
while(rs >= 100)
{
b = rs / 100 ;
printf("\n\nThe no. of hundreds are : %d", b) ;
break ;
}
while(rs >= 50)
{
c = rs / 50 ;
printf("\n\nThe no. of fifties are : %d", c) ;
break ;
}
while(rs >= 20)
{
d = rs / 20 ;
printf("\n\nThe no. of twenties are : %d", d) ;
break ;
}
while(rs >= 10)
{
e = rs / 10 ;
printf("\n\nThe no. of tens are : %d", e) ;
break ;
}
while(rs >= 5)
{
f = rs / 5 ;
printf("\n\nThe no. of fives are : %d", f) ;
break ;
}
B.Bhuvaneswaran A.7
while(rs >= 2)
{
g = rs / 2 ;
printf("\n\nThe no. of Twos are : %d", g) ;
break ;
}
while(rs >= 1)
{
h = rs / 1 ;
printf("\n\nThe no. of ones are : %d", h) ;
break ;
}
getch() ;
}
RUN 1 :
~~~~~~~
RUN 1 :
~~~~~~~
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
B.Bhuvaneswaran A.9
RUN 1 :
~~~~~~~
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
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 :
~~~~~~~
RUN 2 :
~~~~~~~
RUN 3 :
~~~~~~~
RUN 1 :
~~~~~~~
RUN 2 :
~~~~~~~
RUN 3 :
~~~~~~~
RUN 1 :
~~~~~~~
RUN 1 :
~~~~~~~
RUN 1 :
~~~~~~~
RUN 1 :
~~~~~~~
3 6 9 12 15
A.16 Programs in C
/* To print all the divisors of a given number - DIVISORS.C */
# include <stdio.h>
# include <conio.h>
void main()
{
int i, n ;
clrscr() ;
printf("Enter the number : ") ;
scanf("%d", &n) ;
printf("\nThe divisors are :\n\n") ;
for(i = 1 ; i <= n ; i++)
if(n % i == 0)
printf("%d\t", i) ;
getch() ;
}
RUN 1 :
~~~~~~~
1 3 5 15
B.Bhuvaneswaran A.17
RUN 1 :
~~~~~~~
RUN 1 :
~~~~~~~
RUN 1 :
~~~~~~~
0 1 1 2 3
RUN 1 :
~~~~~~~
RUN 1 :
~~~~~~~
Enter a number : 10
RUN 2 :
~~~~~~~
Enter a number : 5
RUN 1 :
~~~~~~~
RUN 2 :
~~~~~~~
RUN 1 :
~~~~~~~
RUN 2 :
~~~~~~~
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
RUN 1 :
~~~~~~~
Enter a number : 81
81 is a perfect square
RUN 2 :
~~~~~~~
Enter a number : 12
RUN 1 :
~~~~~~~
RUN 2 :
~~~~~~~
RUN 1 :
~~~~~~~
RUN 2 :
~~~~~~~
Arrays Programs
/* To display only the positive elements of the array - ARRPOS.C */
# include <stdio.h>
# include <conio.h>
void main()
{
int a[20], i, n ;
clrscr() ;
printf("Enter the limit : ") ;
scanf("%d", &n) ;
printf("\nEnter the elements :\n\n") ;
for(i = 0 ; i < n ; i++)
scanf("%d", &a[i]) ;
printf("\nThe positive elements are :\n\n") ;
for(i = 0 ; i < n ; i++)
{
if(a[i] > 0)
printf("%d\t", a[i]) ;
}
getch() ;
}
RUN 1 :
~~~~~~~
10 30
B.Bhuvaneswaran A.29
RUN 1 :
~~~~~~~
RUN 1 :
~~~~~~~
-10 30 50 -20 40
RUN 1 :
~~~~~~~
20 30 50 10 40
40 10 50 30 20
A.32 Programs in C
RUN 1 :
~~~~~~~
Enter a number : 5
RUN 1 :
~~~~~~~
Enter the value for N : 7
RUN 2 :
~~~~~~~
Enter the value for N : 5
RUN 1 :
~~~~~~~
/* Program to find LCM and GCD of the given two numbers - LCMGCD.C */
# include <stdio.h>
# include <conio.h>
void main()
{
int n1, n2, prod, gcd, lcm ;
clrscr() ;
printf("Enter the two numbers : ") ;
scanf("%d %d", &n1, &n2) ;
prod = n1 * n2 ;
while(n1 != n2)
{
if(n1 > n2)
n1 = n1 - n2 ;
if(n2 > n1)
n2 = n2 - n1 ;
}
gcd = n1 ;
lcm = prod / gcd ;
printf("\nThe GCD is : %d", gcd) ;
printf("\n\nThe LCM is : %d", lcm);
getch() ;
}
RUN 1 :
~~~~~~~
The GCD is : 2
The LCM is : 40
A.36 Programs in C
/* Program to find the roots of a quadratic equation - QUADRAT.C */
# include <stdio.h>
# include <conio.h>
# include <math.h>
void main()
{
float a, b, c, d, real, imag, r1, r2, n ;
int k ;
clrscr() ;
printf("Enter the values of A, B & C : ") ;
scanf("%f %f %f", &a, &b, &c) ;
if(a != 0)
{
d = b * b - 4 * a * c ;
if(d < 0)
k = 1 ;
if(d == 0)
k = 2 ;
if(d > 0)
k = 3;
switch(k)
{
case 1 :
printf("\nRoots are imaginary\n") ;
real = - b / (2 * a) ;
d = - d ;
n = pow((double) d, (double) 0.5) ;
imag = n / (2 * a) ;
printf("\nr1 = %7.2f + j%7.2f", real, imag) ;
printf("\nr2 = %7.2f - j%7.2f", real, imag) ;
break ;
case 2 :
printf("\nRoots are real and equal\n") ;
r1 = - b / (2 * a) ;
printf("\nr1 = r2 = %7.2f", r1) ;
break ;
case 3 :
printf("\nRoots are real and unequal\n") ;
r1 = (- b + sqrt((double) d)) / (2 * a) ;
r2 = (- b - sqrt((double) d)) / (2 * a) ;
printf("\nr1 = %7.2f",r1) ;
printf("\nr2 = %7.2f",r2) ;
break ;
}
}
else
printf("\nEquation is linear") ;
getch() ;
}
B.Bhuvaneswaran A.37
RUN 1 :
~~~~~~~
Equation is linear
RUN 2 :
~~~~~~~
r1 = -1.00 + j 2.45
r2 = -1.00 - j 2.45
RUN 3 :
~~~~~~~
r1 = r2 = -1.00
RUN 4 :
~~~~~~~
r1 = -0.15
r2 = -3.35
A.38 Programs in C
/* Simultaneous equation using gauss elimination method - GAUSS.C */
# include <stdio.h>
# include <conio.h>
void main()
{
int i, j, k, n ;
float a[20][20], x[20] ;
double s, p ;
clrscr() ;
printf("Enter the number of equations : ") ;
scanf("%d", &n) ;
printf("\nEnter the co-efficients of the equations :\n\n") ;
for(i = 0 ; i < n ; i++)
{
for(j = 0 ; j < n ; j++)
{
printf("a[%d][%d] = ", i + 1, j + 1) ;
scanf("%f", &a[i][j]) ;
}
printf("b[%d] = ", i + 1) ;
scanf("%f", &a[i][n]) ;
}
for(k = 0 ; k < n - 1 ; k++)
{
for(i = k + 1 ; i < n ; i++)
{
p = a[i][k] / a[k][k] ;
for(j = k ; j < n + 1 ; j++)
a[i][j] = a[i][j] - p * a[k][j] ;
}
}
x[n-1] = a[n-1][n] / a[n-1][n-1] ;
for(i = n - 2 ; i >= 0 ; i--)
{
s = 0 ;
for(j = i + 1 ; j < n ; j++)
{
s += (a[i][j] * x[j]) ;
x[i] = (a[i][n] - s) / a[i][i] ;
}
}
printf("\nThe result is :\n") ;
for(i = 0 ; i < n ; i++)
printf("\nx[%d] = %.2f", i + 1, x[i]) ;
getch() ;
}
B.Bhuvaneswaran A.39
RUN 1 :
~~~~~~~
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
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 :
~~~~~~~
RUN 1 :
~~~~~~~
RUN 1 :
~~~~~~~
SEVEN
A.44 Programs in C
/* Program to change an integer to words - NUMTOWD.C */
# include <stdio.h>
# include <conio.h>
void main()
{
long n, a[10], i, c = 0 ;
clrscr() ;
printf("Enter a number : ") ;
scanf("%ld", &n) ;
while(n > 0)
{
a[c] = n % 10 ;
n = n / 10 ;
c++ ;
}
printf("\n") ;
for(i = c - 1 ; i >= 0 ; i--)
{
switch(a[i])
{
case 0 :
printf("ZERO ") ;
break ;
case 1 :
printf("ONE ") ;
break ;
case 2 :
printf("TWO ") ;
break ;
case 3 :
printf("THREE ") ;
break ;
case 4 :
printf("FOUR ") ;
break ;
case 5 :
printf("FIVE ") ;
break ;
case 6 :
printf("SIX ") ;
break ;
case 7 :
printf("SEVEN ") ;
break ;
case 8 :
printf("EIGHT ") ;
break ;
B.Bhuvaneswaran A.45
case 9 :
printf("NINE ") ;
break ;
}
}
getch() ;
}
RUN 1 :
~~~~~~~
RUN 1 :
~~~~~~~
RUN 1 :
~~~~~~~
RUN 2 :
~~~~~~~
RUN 1 :
~~~~~~~
RUN 1 :
~~~~~~~
RUN 1 :
~~~~~~~
RUN 1 :
~~~~~~~
Floyd's triangle is :
2 3
4 5 6
7 8 9 10
11 12 13 14 15
A.52 Programs in C
/* Program to generate Pascal's triangle - PASCALS.C */
# include <stdio.h>
# include <conio.h>
void main()
{
int b, p, q, r, x ;
clrscr() ;
b = 1 ;
q = 0 ;
printf("Enter the number of rows : ") ;
scanf("%d", &p) ;
printf("\nPascal's triangle is : \n\n") ;
while (q < p)
{
for(r = 40 - 3 * q ; r > 0 ; --r)
printf(" ") ;
for(x = 0 ; x <= q ; ++x)
{
if((x == 0) || (q == 0))
b = 1 ;
else
b = (b * (q - x + 1)) / x ;
printf("%6d", b) ;
}
printf("\n\n") ;
++q ;
}
getch() ;
}
RUN 1 :
~~~~~~~
Pascal's triangle is :
1 1
1 2 1
1 3 3 1
1 4 6 4 1
B.Bhuvaneswaran A.53
RUN 1 :
~~~~~~~
- - - - - - - - - - - - - - - - - -
Angle Sin Cos Tan
- - - - - - - - - - - - - - - - - -
0 0.00 1.00 0.00
RUN 1 :
~~~~~~~
The permutation is :
abc
acb
bac
bca
cab
cba
A.56 Programs in C
/* Program to generate magic square - GENMAGIC.C */
# include<stdio.h>
void main()
{
int n, i, j, c, a[9][9] ;
clrscr() ;
printf("Enter the size of the magic square : ") ;
scanf("%d", &n) ;
if (n % 2 == 0)
{
printf("\nMagic square is not possible") ;
goto end ;
}
printf("\nThe magic square for %d x %d is :\n\n", n, n) ;
j = (n + 1) / 2 ;
i = 1 ;
for(c = 1 ; c <= n * n ; c++)
{
a[i][j] = c ;
if(c % n == 0)
{
i = i + 1 ;
goto loop ;
}
if(i == 1)
i = n ;
else
i = i - 1 ;
if(j == n)
j = 1;
else
j = j + 1 ;
loop : ;
}
for (i = 1 ; i <= n ; i++)
{
for (j = 1 ; j <= n ; j++)
{
printf("%d\t", a[i][j]) ;
}
printf("\n\n") ;
}
end : ;
getch() ;
}
B.Bhuvaneswaran A.57
RUN 1 :
~~~~~~~
8 1 6
3 5 7
4 9 2
RUN 2 :
~~~~~~~
RUN 1 :
~~~~~~~
1 3 5 7 9
2 4 6 8 10
B.Bhuvaneswaran A.59
RUN 1 :
~~~~~~~
0 1 1 2 3 5 8
A.60 Programs in C
/* Program to generate prime numbers - GENPRIME.C */
# include <stdio.h>
# include <conio.h>
void main()
{
int i, j, n ;
clrscr() ;
printf("Enter the limit : ") ;
scanf("%d", &n) ;
printf("\nThe prime numbers are :\n\n") ;
for (i = 1 ; i <= n ; i++)
{
if(i <= 3)
{
printf("%d\t", i) ;
}
else
{
for (j = 2; j <= i / 2 ; j ++)
{
if (i % j == 0)
goto loop ;
}
printf("%d\t", i) ;
loop : ;
}
}
getch() ;
}
RUN 1 :
~~~~~~~
1 2 3 5 7
B.Bhuvaneswaran A.61
RUN 1 :
~~~~~~~
RUN 1 :
~~~~~~~
1 2 3
4 5 6
7 8 9
RUN 1 :
~~~~~~~
1 2 3
4 5 6
7 8 9
RUN 1 :
~~~~~~~
4 5 6
9 7 8
1 2 3
1 2 3
4 5 6
7 8 9
1 2 3
5 6
9
1
4 5
7 8 9
RUN 1 :
~~~~~~~
1 0 0
0 1 0
0 0 1
1 2 3
4 1 5
6 7 1
RUN 1 :
~~~~~~~
1 2 3
4 5 6
1 4
2 5
3 6
A.70 Programs in C
/* Program to add the given two matrices - MATADD.C */
# include <stdio.h>
# include <conio.h>
void main()
{
int mata[10][10], matb[10][10], matc[10][10] ;
int i, j, row, col ;
clrscr() ;
printf("Enter the order of the matrix : ") ;
scanf("%d %d", &row, &col) ;
printf("\nEnter the elements of first matrix : \n\n") ;
for(i = 0 ; i < row ; i++)
for(j = 0 ; j < col ; j++)
scanf("%d", &mata[i][j]) ;
printf("\nEnter the elements of second matrix : \n\n") ;
for(i = 0 ; i < row ; i++)
for(j = 0 ; j < col ; j++)
scanf("%d", &matb[i][j]) ;
for(i = 0 ; i < row ; i++)
for(j = 0 ; j < col ; j++)
matc[i][j] = mata[i][j] + matb[i][j] ;
printf("\nThe resultant matrix is : \n\n") ;
for(i = 0 ; i < row ; i++)
{
for(j = 0 ; j < col ; j++)
{
printf("%d \t", matc[i][j]) ;
}
printf("\n") ;
}
getch() ;
}
B.Bhuvaneswaran A.71
RUN 1 :
~~~~~~~
1 3 5
7 9 11
13 15 17
2 4 6
8 10 12
14 16 18
3 7 11
15 19 23
27 31 35
A.72 Programs in C
/* Program to subtract the given two matrices - MATSUB.C */
# include <stdio.h>
# include <conio.h>
void main()
{
int mata[10][10], matb[10][10], matc[10][10] ;
int i, j, row, col ;
clrscr() ;
printf("Enter the order of the matrix : ") ;
scanf("%d %d", &row, &col) ;
printf("\nEnter the elements of first matrix : \n\n") ;
for(i = 0 ; i < row ; i++)
for(j = 0 ; j < col ; j++)
scanf("%d", &mata[i][j]) ;
printf("\nEnter the elements of second matrix : \n\n") ;
for(i = 0 ; i < row ; i++)
for(j = 0 ; j < col ; j++)
scanf("%d", &matb[i][j]) ;
for(i = 0 ; i < row ; i++)
for(j = 0 ; j < col ; j++)
matc[i][j] = mata[i][j] - matb[i][j] ;
printf("\nThe resultant matrix is : \n\n") ;
for(i = 0 ; i < row ; i++)
{
for(j = 0 ; j < col ; j++)
{
printf("%d \t", matc[i][j]) ;
}
printf("\n") ;
}
getch() ;
}
B.Bhuvaneswaran A.73
RUN 1 :
~~~~~~~
5 10 15
20 25 30
35 40 45
2 4 6
8 10 12
14 16 18
3 6 9
12 15 18
21 24 27
A.74 Programs in C
/* Program to multiply the given two matrices - MATMUL.C */
# include <stdio.h>
# include <conio.h>
void main()
{
int mata[10][10], matb[10][10], matc[10][10] ;
int i, j, k, row1, col1, row2, col2 ;
clrscr() ;
printf("Enter the order of first matrix : ") ;
scanf("%d %d", &row1, &col1) ;
printf("\nEnter the order of second matrix : ") ;
scanf("%d %d", &row2, &col2) ;
if(col1 == row2)
{
printf("\nEnter the elements of first matrix : \n\n") ;
for(i = 0 ; i < row1 ; i++)
for(j = 0 ; j < col1 ; j++)
scanf("%d", &mata[i][j]) ;
printf("\nEnter the elements of second matrix : \n\n") ;
for(i = 0 ; i < row2 ; i++)
for(j = 0 ; j < col2 ; j++)
scanf("%d", &matb[i][j]) ;
for(i = 0 ; i < row1 ; i++)
{
for(j = 0 ; j < col2 ; j++)
{
matc[i][j] = 0 ;
for(k = 0 ; k < col1 ; k++)
matc[i][j] = matc[i][j] + mata[i][k] * matb[k][j] ;
}
}
printf("\nThe resultant matrix is : \n\n") ;
for(i = 0 ; i < row1 ; i++)
{
for(j = 0 ; j < col2 ; j++)
{
printf("%d \t", matc[i][j]) ;
}
printf("\n") ;
}
}
else
printf("\nMatrix Multiplication is not possible ...") ;
getch() ;
}
B.Bhuvaneswaran A.75
RUN 1 :
~~~~~~~
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1
3 3 3
3 3 3
3 3 3
RUN 2 :
~~~~~~~
RUN 1 :
~~~~~~~
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
B.Bhuvaneswaran A.77
RUN 1 :
~~~~~~~
u is a vowel
RUN 2 :
~~~~~~~
v is not a vowel
A.78 Programs in C
/* Program to find the length of the given string - STRLEN.C */
# include <stdio.h>
# include <conio.h>
void main()
{
char str[80] ;
int i ;
clrscr() ;
printf("Enter the string : ") ;
gets(str) ;
for(i = 0 ; str[i] != '\0' ; i++) ;
printf("\nThe length of the string is : %d", i) ;
getch() ;
}
RUN 1 :
~~~~~~~
RUN 1 :
~~~~~~~
Enter a charatcer : b
RUN 2 :
~~~~~~~
Enter a charatcer : V
RUN 3 :
~~~~~~~
Enter a charatcer : 2
RUN 4 :
~~~~~~~
Enter a charatcer : #
RUN 1 :
~~~~~~~
anna university
B.Bhuvaneswaran A.81
RUN 1 :
~~~~~~~
Enter the text : Bhuvan was born in 2nd Nov 1977 !!!
Number of vowels = 6
Number of consonants = 14
Number of digits = 5
Number of words = 8
A.82 Programs in C
/* Program to concatenate the given two strings - STRCAT.C */
# include <stdio.h>
# include <conio.h>
void main()
{
char str1[20], str2[20], strcon[40] ;
int i, j ;
clrscr() ;
printf("Enter the first string : ") ;
scanf("%s", str1) ;
printf("\nEnter the second string : ") ;
scanf("%s", str2) ;
for(i = 0 ; str1[i] != '\0' ; i++)
strcon[i] = str1[i] ;
i-- ;
for(j = 0 ; str2[j] != '\0' ; j++)
strcon[i+j+1] = str2[j] ;
strcon[i+j+1] = '\0' ;
printf("\nThe concatenation string is : %s", strcon) ;
getch() ;
}
RUN 1 :
~~~~~~~
Anna
Madras
RUN 1 :
~~~~~~~
Anna University
RUN 1 :
~~~~~~~
RUN 1 :
~~~~~~~
RUN 2 :
~~~~~~~
RUN 3 :
~~~~~~~
RUN 4 :
~~~~~~~
RUN 1 :
~~~~~~~
viji
udaya
priya
bhuvan
satish
bhuvan
priya
satish
udaya
viji
B.Bhuvaneswaran A.89
Structures Programs
/* Program to maintain student details using structures - STSTUD.C */
# include <stdio.h>
# include <conio.h>
struct stud
{
int rollno, s1, s2, tot ;
char name[10] ;
float avg ;
} s[10] ;
void main()
{
int i, n ;
clrscr() ;
printf("Enter the number of students : ") ;
scanf("%d", &n) ;
for(i = 0 ; i < n ; i++)
{
printf("\nEnter the roll number : ") ;
scanf("%d", &s[i].rollno) ;
printf("\nEnter the name : ") ;
scanf("%s", s[i].name) ;
printf("\nEnter the marks in 2 subjects : ") ;
scanf("%d %d", &s[i].s1, &s[i].s2) ;
s[i].tot = s[i].s1 + s[i].s2 ;
s[i].avg = s[i].tot / 2.0 ;
}
printf("\nRoll No. Name \t\tSub1\t Sub2\t Total\t Average\n\n") ;
for(i = 0 ; i < n ; i++)
{
printf("%d \t %s \t\t %d \t %d \t %d \t %.2f \n",
s[i].rollno,s[i].name,s[i].s1,s[i].s2,s[i].tot,s[i].avg);
}
getch() ;
}
A.90 Programs in C
RUN 1 :
~~~~~~~
Enter the basic pay, allowances & deductions : 5000 1000 250
Enter the basic pay, allowances & deductions : 7000 1500 750
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 :
~~~~~~~
RUN 1 :
~~~~~~~
RUN 1 :
~~~~~~~
RUN 1 :
~~~~~~~
Enter the first string : cse
RUN 2 :
~~~~~~~
RUN 1 :
~~~~~~~
RUN 1 :
~~~~~~~
RUN 1 :
~~~~~~~
RUN 1 :
~~~~~~~
Enter the basic pay, allowances & deductions : 6000 1000 500
Enter the basic pay, allowances & deductions : 5000 1000 450
RUN 1 :
~~~~~~~
Computer Practice - I
I - Semester
^Z
Computer Practice - II
II - Semester
^Z
Computer Practice - I
I - Semester
Computer Practice - II
II - Semester
A.106 Programs in C
/* Program to encrypt and decrypt a file - ENCDEC.C */
# include <stdio.h>
# include <conio.h>
void main()
{
FILE *fptr;
char c ;
clrscr() ;
printf("Enter the text to be stored in the file.\n") ;
printf("Use ^Z or F6 at the end of the text and press
ENTER : \n\n") ;
fptr = fopen("ENCDEC.DAT","w") ;
while((c = getchar()) != EOF)
fputc(c, fptr) ;
fclose(fptr) ;
printf("\n\nData output in encrypted form : \n\n") ;
fptr = fopen("ENCDEC.DAT", "r") ;
while((c = fgetc(fptr)) != EOF)
printf("%c", c+1) ;
fclose(fptr) ;
printf("\n\nData output in decrypted form : \n\n") ;
fptr = fopen("ENCDEC.DAT", "r") ;
while((c = fgetc(fptr)) != EOF)
printf("%c", c) ;
fclose(fptr) ;
getch() ;
}
RUN 1 :
~~~~~~~
Computer Practice - II
D.D. Publications
^Z
Dpnqvufs!Qsbdujdf!.!JJ
E/E/!Qvcmjdbujpot
Computer Practice - II
D.D. Publications
B.Bhuvaneswaran A.107
Searching Programs
/* Program to search an element using binary search - BINARY.C */
# include <stdio.h>
# include <conio.h>
void main()
{
int a[10], f, l, i, j, k, mid, n, t, flag = 0 ;
clrscr() ;
printf("Enter the limit : ") ;
scanf("%d", &n) ;
printf("\nEnter the elements :\n\n") ;
for(i = 0 ; i < n ; i++)
scanf("%d", &a[i]) ;
for(i = 0 ; i < n ; i++)
for(j = i + 1 ; j < n ; j++)
if(a[i] > a[j])
{
t = a [i] ;
a[i] = a[j] ;
a[j] = t ;
}
printf("\nThe ordered elements are : \n\n") ;
for(i = 0 ; i < n ; i++)
printf("%d\t", a[i]) ;
printf("\n\nEnter the element to be searched : ") ;
scanf("%d", &k) ;
f = 0 ;
l = n - 1 ;
while(f <= l)
{
mid = (f + l) / 2 ;
if(a[mid] == k)
{
flag = 1 ;
break ;
}
else if(a[mid] < k)
f = mid + 1 ;
else
l = mid - 1 ;
}
if(flag == 1)
printf("\nThe element is found at location : %d", mid + 1) ;
else
printf("\nThe element is not found") ;
getch() ;
}
A.108 Programs in C
RUN 1 :
~~~~~~~
20 40 30 50 10
10 20 30 40 50
RUN 2 :
~~~~~~~
20 40 30 50 10
10 20 30 40 50
20 40 30 50 10
RUN 2 :
~~~~~~~
20 40 30 50 10
Sorting Programs
/* Program to sort the given numbers using bubble sort - BUBSORT.C */
# include <stdio.h>
# include <conio.h>
void main()
{
int a[10], t, i, j, n ;
clrscr() ;
printf("Enter the limit : ") ;
scanf("%d", &n) ;
printf("\nEnter the numbers :\n\n") ;
for(i = 0 ; i < n ; i++)
scanf("%d", &a[i]) ;
for(i = 0 ; i < n - 1 ; i++)
{
for(j = 0 ; j < n - 1 ; j++)
{
if(a[j] > a[j + 1])
{
t = a[j] ;
a[j] = a[j + 1] ;
a[j + 1] = t ;
}
}
}
printf("\nThe sorted elemets are :\n\n") ;
for(i = 0 ; i < n ; i++)
printf("%d\t", a[i]) ;
getch() ;
}
RUN 1 :
~~~~~~~
20 40 30 50 10
10 20 30 40 50
A.112 Programs in C
/* To sort the given numbers using selection sort - SELSORT.C */
# include <stdio.h>
# include <conio.h>
void main()
{
int i, j, k, left, n, a[10], t ;
clrscr() ;
printf("Enter the limit : ") ;
scanf("%d", &n) ;
printf("\nEnter the elements :\n\n") ;
for(i = 0 ; i < n ; i++)
scanf("%d", &a[i]) ;
for(i = 0 ; i < n ; i++)
{
left = a[i] ;
k = i ;
for(j = i + 1 ; j < n ; j++)
if(left > a[j])
{
left = a[j] ;
k = j ;
}
a[k] = a[i] ;
a[i] = left ;
}
printf("\nThe sorted elemets are :\n\n") ;
for(i = 0 ; i < n ; i++)
printf("%d\t", a[i]) ;
getch() ;
}
RUN 1 :
~~~~~~~
20 40 30 50 10
10 20 30 40 50
B.Bhuvaneswaran A.113
RUN 1 :
~~~~~~~
20 40 30 50 10
10 20 30 40 50
A.114 Programs in C
/* Program to sort the given numbers using quick sort - QSORT.C */
# include <stdio.h>
# include <conio.h>
# include <values.h>
int a[10] ;
void main()
{
int i, n ;
void qsort(int, int) ;
clrscr() ;
printf("Enter the limit : ") ;
scanf("%d", &n) ;
printf("\nEnter the elements :\n\n") ;
for(i = 0 ; i < n ; i++)
scanf("%d", &a[i]) ;
a[i] = MAXINT ;
qsort(0, n - 1) ;
printf("\nThe sorted elemets are :\n\n") ;
for(i = 0 ; i < n ; i++)
printf("%d\t", a[i]) ;
getch() ;
}
qsort(left, j - 1) ;
qsort(j + 1, right) ;
}
return ;
}
RUN 1 :
~~~~~~~
20 40 30 50 10
10 20 30 40 50
A.116 Programs in C
/* Program to sort the given numbers using shell sort - SHELSORT.C */
# include <stdio.h>
# include <conio.h>
void main()
{
int i, j, k, n, a[10], t ;
clrscr() ;
printf("Enter the limit : ") ;
scanf("%d", &n) ;
printf("\nEnter the elements :\n\n") ;
for(i = 0 ; i < n ; i++)
scanf("%d", &a[i]) ;
for(i = (n + 1) / 2 ; i >= 1 ; i = i / 2)
for(j = i ; j < n ; j++)
{
t = a[j] ;
for(k = j - i ; k >= 0 && t < a[k] ; k = k - i)
a[k + i] = a[k] ;
a[k + i] = t ;
}
printf("\nThe sorted elemets are :\n\n") ;
for(i = 0 ; i < n ; i++)
printf("%d\t", a[i]) ;
getch() ;
}
RUN 1 :
~~~~~~~
20 40 30 50 10
10 20 30 40 50
B.Bhuvaneswaran A.117
RUN 1 :
~~~~~~~
20 30 10 50 40
10 20 30 40 50
50 40 30 20 10
A.118 Programs in C
void push()
{
if(top == SIZE - 1)
{
printf("\nStack is full (overflow)\n") ;
getch() ;
return ;
}
top++ ;
printf("\nEnter the element to PUSH : ") ;
scanf("%d", &arr[top]) ;
}
void pop()
{
if(top == -1)
{
printf("\nStack is empty (underflow)\n") ;
getch() ;
return ;
}
printf("\nThe POP element is : %d\n", arr[top]) ;
getch() ;
top-- ;
}
void display()
{
if(top == -1)
{
printf("\nStack is empty (underflow)\n") ;
getch() ;
return ;
}
printf("\nThe elements in stack are :\n\nTOP") ;
for(i = top ; i >= 0 ; i--)
printf(" -> %d", arr[i]) ;
printf("\n") ;
getch() ;
}
RUN 1 :
~~~~~~~
TOP -> 10
void enqueue()
{
if(rear == SIZE - 1)
{
printf("\nQueue is full (overflow)\n") ;
getch() ;
return ;
}
rear++ ;
printf("\nEnter the element to ENQUEUE : ") ;
scanf("%d", &arr[rear]) ;
A.122 Programs in C
if(front == -1)
front++ ;
}
void dequeue()
{
if(front == -1)
{
printf("\nQueue is empty (underflow)\n") ;
getch() ;
return ;
}
printf("\nThe DEQUEUE element is : %d\n", arr[front]) ;
getch() ;
if(front == rear)
front = rear = -1 ;
else
front++ ;
}
void display()
{
if(front == -1)
{
printf("\nQueue is empty (underflow)\n") ;
getch() ;
return ;
}
printf("\nThe elements in queue are :\n\nFRONT ->") ;
for(i = front ; i <= rear ; i++)
printf(" ... %d", arr[i]) ;
printf(" ... <- REAR\n") ;
getch() ;
}
RUN 1 :
~~~~~~~
bim (float *a, float *b, float *root, int *s, int *count)
{
float x1, x2, x0, f0, f1, f2 ;
x1 = *a ;
x2 = *b ;
f1 = F(x1) ;
f2 = F(x2) ;
if(f1 * f2 > 0)
{
*s = 0 ;
return ;
}
else
{
*count = 0 ;
B.Bhuvaneswaran A.125
begin :
x0 = (x1 + x2) / 2.0 ;
f0 = F(x0) ;
if(f0 == 0)
{
*s = 1 ;
*root = x0 ;
return ;
}
if(f1 * f0 < 0)
{
x2 = x0 ;
}
else
{
x1 = x0 ;
f1 = f0 ;
}
if(fabs((x2 - x1) / x2) < EPS)
{
*s = 1 ;
*root = (x1 + x2) / 2.0 ;
return ;
}
else
{
*count = *count + 1 ;
goto begin ;
}
}
}
RUN 1 :
~~~~~~~
Root = 2.094552
F(root) = 0.000006
Iterations = 18
A.126 Programs in C
/* Find the root of a equation using Newton Raphson - NEWTON.C */
# include <stdio.h>
# include <conio.h>
# include <math.h>
# define EPS 0.000001
# define MAXIT 20
# define F(x) (x) * (x) * (x) - 2 * (x) - 5
# define FD(x) 3 * (x) * (x) - 2
void main()
{
int count ;
float x0, xn, fx, fdx ;
clrscr() ;
printf("Input initial value of x : ") ;
scanf("%f", &x0) ;
count = 1 ;
begin :
fx = F(x0) ;
fdx = FD(x0) ;
xn = x0 - fx / fdx ;
if(fabs((xn - x0) / xn) < EPS)
{
printf("\nRoot = %f", xn) ;
printf("\n\nNumber of iterations = %d", count) ;
}
else
{
x0 = xn ;
count = count + 1 ;
if(count < MAXIT)
{
goto begin ;
}
else
{
printf("\nSolution does not coverage in %d iterations",
MAXIT) ;
}
}
getch() ;
}
RUN 1 :
~~~~~~~
Root = 2.094552
Number of iterations = 19
B.Bhuvaneswaran A.127
sec(float *a, float *b, float *x1, float *x2, float *root,
int *count, int *status)
{
float x3, f1, f2, error ;
*x1 = *a ;
*x2 = *b ;
f1 = F(*a) ;
f2 = F(*b) ;
*count = 1 ;
begin :
if(fabs(f1 - f2) <= 1.E-10)
{
*status = 1 ;
return ;
}
A.128 Programs in C
x3 = *x2 - f2 * (*x2 - *x1) / (f2 - f1) ;
error = fabs((x3 - *x2) / x3) ;
if(error > EPS)
{
if(*count == MAXIT)
{
*status = 2 ;
return ;
}
else
{
*x1 = *x2 ;
}
*x2 = x3 ;
f1 = f2 ;
f2 = F(x3) ;
*count = *count + 1 ;
goto begin ;
}
else
{
*root = x3 ;
*status = 3 ;
return ;
}
}
RUN 1 :
~~~~~~~
Root = 2.094552
No. of iterations = 6