C Programs
C Programs
1(a)
write a simple program that prints the results of all the operators
available in c(including pre/post increment,bitwise,and/or/not etc)
read required operand values from standard input */
Algorithm
Step1: Start
Step2: Read the values of a ,b
Step3: Perform arithmetic operation
Step4: Print the values of
a+b, a-b, a*b, a/b, a%b
Step5: Perform pre/post increment operations
Step6: Print the value of a++ (post increment).
Step7: Print the value of ++a (pre increment).
Step8: Perform bit wise operation
Step9: Print the value a&b, a|b, ~a, a^b
Step10: Perform left shift and right operation
Step 11: read the value of n(number of bits to be shifted).
Step12: print the value a<<n, a>>n
Step13: stop
Start
Flow chart
Read a and b
values
Perform arithmetic
operation
Perform pre/post
increment operation
Perform bitwise
operation
STOP
Program
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b;
clrscr();
printf("Enter a and b values \n");
scanf("%d%d",&a,&b);
printf("******arthemetic operation**********");
printf("\n sum of a+b=%d",a+b);
printf("\n sub of a-b=%d",a-b);
printf("\n multiplication of a*b=%d",a*b);
printf("\n division of a/b=%d",a/b);
printf("\n modulus of a and b =%d",a%b);
printf("\n***************************************");
printf("\n*********** Pre/Post increment operations****");
printf("\n Value of a is %d",a);
printf("\nValue of postincrement a++ is %d",a++);
printf("\nValue of a is %d ",a);
printf("\nValue of preincrement ++a is %d",++a);
printf("\nvalue of a %d",a);
printf("\n*********************************************");
printf("\n******** BitWise operations****************");
printf("\n value of Bitwise OR %d|%d is %d",a,b,a|b);
printf("\nValue of BitWise AND %d&%d is %d",a,b,a&b);
printf("\n Negation of %d is %d",a,~a);
printf("\n BitWise Right Shift %d>>%d is %d", a,b,a>>b);
printf("\n BitWise LeftShift %d<<%d is %d",a,b,a<<b);
printf("\nBitWise XOR %d^%d is %d",a,b,a^b);
printf("\n*********************************************\n");
getch();
}
Output:
Enter a and b values
62
******** arthemetic operation*******
Sum of a+b=8
Sub of a-b=4
Multiplication of a*b=12divion of a/b=3
Modules of a and b=0
************* pre/post increment operations***********
Value of a is 6
Value of post increment a++ is 6
Value of a is 7
Value of pre increment ++a is 8
Value of a is 8
Value of birwise OR 8|2 is 10
Value of bitwise AND is 8&2 is 0
Negation of 8 is -9
Bitwise right shift 8>>2 is 2
Bitwise leftshift 8<<2 is 32
Bitwise XOR 8^2 is 10
1(b) write a c program which takes two integer opearands and one
operator
from the user performs the operation and then print the result
(consider the opertors +,-,*,/,% and use switch statement)
Algorithm
Step 1 : start
Step 2 : read a,b values and choice(ch)
Step 3 : read choice as
1.addition 2. Substraction 3. Multiplication
4. division 5. modulus
Step 4 : if choice is 1 (ch=1) then print addition of 2 num (a+b) and goto
step 10
Step 5 : if ch=2 then print substraction of 2 num (a-b) and goto step 10
Step 6 : if ch=3 then print multiplication of 2 num (a*b) and goto step 10
Step 7 : if ch=4 then print dividion of 2 num (a/b) and goto step 10
Step 8 : if ch=5 then print modulus of 2 num(a%b) and goto step 10
Step 9 : else if choice entered is invalid then display invalid choice
Step 10 : stop
start
Flow chart
Read a,b
Calculate a-b
if ch=2
Calculate a*b
if ch=3
Calculate a/b
if ch=4
Calculate a%b
if ch=5
Invalid Display
choice
Program result
stop
Program
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,ch;
clrscr();
printf("enter the value of a,b");
scanf("%d%d",&a,&b);
printf("enter your choice:\n 1.add \n 2.sub \n 3.mul \n 4.div \n 5.mod \n
6.exit");
scanf("%d",&ch);
do
{
switch(ch)
{
case 1:printf("\n addition=%d",a+b);
break;
case 2:printf("\n subtraction=%d",a-b);
break;
case 3:printf("\n multiplication=%d",a*b);
break;
case 4:printf("\n division=%d",a/b);
break;
case 5:printf("\n modules=%d",a%b);
break;
case 6:exit(0);
break;
default:printf("invalid choice");
}
}while(ch!=7);
getch();
}
Output:
1) Enter the value for a,b
8
4
Enter your choice:
1.add
2.sub
3.mul
4.div
5.mod
The choice is: 1
Addition= 12
2) Enter the value for a,b
8
4
Enter your choice:
1.add
2.sub
3.mul
4.div
5.mod
The choice is: 2
Subtraction= 4
3) Enter the value for a,b
8
4
Enter your choice:
1.add
2.sub
3.mul
4.div
5.mod
The choice is: 3
Multiplication= 32
4) Enter the value for a,b
8
4
Enter your choice:
1.add
2.sub
3.mul
4.div
5.mod
The choice is: 4
Division= 2
5) Enter the value for a,b
8
4
Enter your choice:
1.add
2.sub
3.mul
4.div
5.mod
The choice is: 5
Modulus= 0
6) Enter the value for a,b
8
4
Enter your choice:
1.add
2.sub
3.mul
4.div
5.mod
The choice is: 6
Invalid choice
1(c) write a c program to find the roots of a quadratic equation
Algorithm
Step 1 : start
Step 2 : read a,b,c values
Step 3 : if a=0 then print this is not a quadratic equation and goto step 9
Step 4 : else calculate d=b*b-4*a*c
Step 5 : if d>0 then print roots are real and unequal and calculate
r1=(-b+sqrt(d))/(2*a)
r2=(-b-sqrt(d))/(2*a)
Step 6 : else if d=0 then print roots are real and equal and calculate
r1=r2=-b/2*a then goto step 8
Step 7 : else print roots are imaginary and goto step 9
Step 8 : display r1,r2
Step 9 : stop
start
Flow chart
Read a,b,c
If a==0
It is not a quadratic
equation Calculate
d=b*b-4ac
If d>0
Roots are real
If d==0
and unequal
Roots are real Roots are
and equal imaginary
Calculate
r1=(-b+sqrt(d))/(2*a)
r2=(-b-sqrt(d))/(2*a)
Calculate r1=r2=-b/2a
Display
r1,r2
stop
Program
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int a,b,c;
float d,r1,r2;
clrscr();
printf("enter the coefficient values for a,b,c: \n");
scanf("%d%d%d",&a,&b,&c);
if(a==0)
{
printf("\n this is not a quadratic equation");
}
else
{
d=b*b-4*a*c;
printf("descriminant is %f" ,d);
if(d>0)
{
printf("\n roots are real and unequal");
r1=(-b+sqrt(d))/(2*a);
r2=(-b-sqrt(d))/(2*a);
printf("\n roots are r1=%f \n r2=%f",r1,r2);
}
else if(d==0)
{
printf("\n roots are real and equal");
r1=r2=-b/(2*a);
printf("\n roots are r1=%f \n r2=%f",r1,r2);
}
else
printf("\n roots are imaginary");
}
getch();
}
Output
Algorithm
Step 1: start
Step 2: read a, b ,c values
Step 3: if(a>b)&&(a>c) then display maximum
value a and goto step 6
Step 4: else if (b>a)&&(b>c)then display maximum value b and goto
step 6
Step 5: else display maximum value c and goto step6
Step 6: if(a<b)&&(a<c) then display minimum value a and goto step 9
Step 7:else if(b<a)&&(b<c) then display minimum value b and goto
step 9
Step 8; else display minimum value c
Step 9: stop
start
Flow chart
F If (a>b&&a>c) T
Print a
If(b>a&&b>c)
Print b
Print c
If(a<b&&a<c)
If(b<a&&b<c) Print a
Print b
Print c
stop
Program
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c;
clrscr();
printf("Enter 3 numbers");
scanf("%d%d%d",&a,&b,&c);
if(a>b && a>c)
printf("Maximum number is a = %d\n",a);
else if(b>a && b>c)
printf("Maximum number is b = %d\n",b);
else
printf("maximum number is c = %d\n",c);
if(a<b && a<c)
printf("Minimum number is a = %d\n",a);
else if(b<a && b<c)
printf("Minimum number is b = %d\n",b);
else
printf("Minimum number is c = %d\n",c);
getch();
Output
Enter 3 numbers
25
654
85
Maximum number is b= 654
Minimum number is a= 25
2(b) write a c program to input marks of five subjects
physics,chemistry,biology,mathematics,and english
calculate percentage and grade according to give conditions:
if percentage>=90%;grade A
if percentage>=80%;grade B
if percentage>=70%;grade C
if percentage>=60%;grade D
if percentage>=40%;grade E
if percentage<40%;grade F
Algorithm
Step 1: start
Step 2: read the values of five subjects physics, chemistry, biology,
mathematics and english
Step 3: calculate
total=physics+ chemistry+ biology+ mathematics+ English and
average(avg)=total/5
Step 4: if (avg>=90), then print ‘A’ grade and goto step 10
Step 5: if (avg >=80), then print ‘B’ grade and goto step 10
Step 6: if (avg>=70) then print ‘C’ grade and goto step 10
Step 7: if (avg >=60), then print ‘D’ grade and goto step 10
Step 8: if (avg >=40), then print ‘E’ grade and goto step 10
Step 9:if (avg<40),then print grade ‘F’
Step 10: stop
Flow chart start
Read values of
five subjects
Calculate Total and Ave=total/5
If
avg>=90
If avg>=80
Print
grade A
If avg>=
Print grade 70
B
Print grade
C
If(avg
>=60
If(avg>
Print grade d =40)
Print grade E
If
avg<40
Grade F
Stop
Program
#include <stdio.h>
#include<conio.h>
void main()
{
int phy, chem, bio, math, eng,total;
float avg;
clrscr();
printf("Enter five subjects marks: ");
scanf("%d%d%d%d%d", &phy, &chem, &bio, &math, &eng);
getch();
}
Output
1) Enter five subject marks:
92
96
97
98
97
Percentage= 96.00
Grade A
2) Enter five subject marks:
81
84
86
87
89
Percentage= 85.00
Grade B
3) Enter five subject marks:
73
77
79
77
75
Percentage= 76.00
Grade C
4) Enter five subject marks:
69
67
65
63
61
Percentage=65.00
Grade D
5) Enter five subject marks:
58
51
57
58
51
Percentage=55.00
Grade E
Algorithm
Step 1: start
Step 2: read the value of n
Step 3: initialize rev=0
Step 4: if n>0 then goto step 5
Step 5: calculate r=n%10
Step 6: calculate rev=rev*10+r
Step 7: calculate n=n/10 and goto step 4
Step 8: display the value of rev
Step 9: stop
Flow chart
start
Read n value
Initialize rev=0
n>0
Calculate r=n%10
Calculate Rev=rev*10+r
Calculate n=n/10
Display
rev
stop
program
#include<stdio.h>
#include<conio.h>
void main()
{
int n,rev=0,r;
clrscr();
printf("enter an integer: ");
scanf("%d",&n);
while(n>0)
{
r=n%10;
rev=rev*10+r;
n=n/10;
}
printf("reversed number is %d",rev);
getch();
}
Output:
Enter an integer: 8154
Reversed number is 4518
3(b(i)) write a c program to calculate the following sum
sum=1+x^2/2!+x^4/4!+.....upto 5 terms
Algorithm
Step 1 : start
Step 2 : read ‘x’ value
Step 3 : initialize s=0
Step 4 : initialize i=0
Step 5 : if i<=10 then f=1
Step 6 : initialize j=1
Step 7 : if j<=i, then f=f*j
Step 8 : else j++ and goto step 7
Step 9 : calculate s=s+(pow(x,i)/f)
Step 10 : else i+2 and goto step 5
Step 11 : print ‘s’ value
Step 12 : stop
Start
Flow chart
Read ‘x’ value
Initialize s=0
i=0 i+2
i<=10
f=1
j=1 j++
j<=i
f=f*j
Calculate
s= s+pow(x,i)/f
Print sum s
stop
Program
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int x,i,j,f;
float s=0;
clrscr();
printf("enter x values ");
scanf("%d",&x);
for(i=0;i<=10;i=i+2)
{
f=1;
for(j=1;j<=i;j++)
{
f=f*j;
}
s=s+(pow(x,i)/f);
// k=k*(1);
}
printf("\n sum of series =%f",s);
getch();
}
Output:
enter x value 1
Sum of series=1.543057
3(b(II)) write a c program to calculate the following sum
sum=x+x^3/3!+x^5/5!+....upto 5 terms
Algorithm
Step 1 : start
Step 2 : read ‘x’ value
Step 3 : initialize s=0
Step 4 : initialize i=1
Step 5 : if i<=10 then f=1
Step 6 : initialize j=1
Step 7 : if j<=i, then f=f*j
Step 8 : else j++ and goto step 7
Step 9: calculate s=s+(pow(x,i)/f)
Step 10 : else i+2 and goto step 5
Step 11 : print ‘s’ value
Step 12 : stop
Start
Flow chart
Read ‘x’ value
Initialize s=0
i=1 i+2
i<=10
f=1
j=1 j++
j<=i
f=f*i
Calculate
s= s+pow(x,i)/f
Print sum s
stop
Program
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int x,i,j,f;
float s=0;
clrscr();
printf("enter x values ");
scanf("%d",&x);
for(i=1;i<=10;i=i+2)
{
f=1;
for(j=1;j<=i;j++)
{
f=f*j;
}
s=s+(pow(x,i)/f);
}
printf("\n sum of series =%f",s);
getch();
}
Output:
Enter x value 1
Sum of series-1.175165
3(c)
write a c program to generate all the prime number
between 1 and n where n is a value supplied by the user */
Algorithm
Step 1 : start
Step 2 : read ‘n’ value
Step 3 : initialize i=1
Step 4 : if i<n then k=0
Step 5 : initialize j=1
Step 6 : if j<=i then if(i%j=0) then k++
Step 7 : j++ goto step 6
Step 8 : else if j<=i is false then i++ goto step 4
Step 9: if k=2 then print ‘i’ value
Step 10 : else i<n is false then goto step11
Step 11 : stop
Flow chart start
i=1 i++
i<n
K=0
j=1 j++
j<=i
if i%j=0
K++
if k=2
Display ‘i’
value
stop
Program
#include<stdio.h>
#include<conio.h>
void main()
{
int n,i,k,j;
clrscr();
printf("enter n values \n");
scanf("%d",&n);
for(i=1;i<n;i++)
{
k=0;
for(j=1;j<=i;j++)
{
if(i%j==0)
k++;
}
if(k==2)
printf("\n %d",i);
}
getch();
}
Output:
Enter n value 7
2
3
5
4(a) write a c program to generate pascal's triangle
Algorithm
Step 1 : start
Step 2 : read n
Step 3 : initialize i=0
Step 4 : if i<n
Step 5 : initialize j=20-i
Step 6: if j>0 is true
Print white space
Decrement by 1
Goto step 5 again
Step 7 : initialize k=0
Step 8 : if k<=i then k<=i is false then i++ goto step 4
Step 9 : if k=0 to i=0 then t=1
Step 10 : else t=t*(i-k+1)/k
Step 11 : print ‘t’ then k++ goto step 8
Step 12 : stop
Start
Initialize i=0
if i<n
Initialize j=20-i
If j>0
j--
Initialize k=0
i++ If k<=i
t=1
Program Display t
k++
stop
program
#include<stdio.h>
#include<conio.h>
void main()
{
int n,i,j,k,t=1;
clrscr();
printf("enter number of lines : \n ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
for(j=20-i;j>0;j--)
{
printf(" ");
}
for(k=0;k<=i;k++)
{
if(k==0||i==0)
t=1;
else
t=t*(i-k+1)/k;
printf("%3d",t);
}
printf("\n\n");
}
getch();
}
Output:
Enter number of lines : 3
1
1 1
1 2 1
4(b) write a c program to generate the first n terms of the fibonacci
sequence
Algorithm
Step 1: start
Step 2 : read ‘n’ value
Step 3: initialize a=0 and b=1
Step 4: print values of a,b
Step 5: initialize i=3
Step 6: if i<=n then c=a+b and print ‘c’ value
Step 7: a=b, b=c then i++ and goto step 6
else goto step 8
Step 8: stop
Flow chart
start
Read n value
Initialize a=0,b=1
Print a and b
values
I=3 i++
I<=n
Calculate c=a+b
print c value
a=b , b=c
stop
program
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c,n,i;
clrscr();
printf("enter the length of series(n):");
scanf("%d",&n);
a=0;
b=1;
printf("\n the fibonacci series upto %d terms is: \n",n);
printf(" \n %d \t \n %d \t ",a,b);
for(i=3;i<=n;i++)
{
c=a+b;
printf(" \n \n %d\t ",c);
a=b;
b=c;
}
getch();
}
Output:
enter the length of series(n):
5
The fibonacci series upto 5 terms is:
0
1
1
2
3
5(a).write a c program to sort array of elements in ascending order.
Program
#include<stdio.h>
#include<conio.h>
#define size 10
void main()
{
int array[size],n,i,j,temp;
clrscr();
printf("enter no of elements: ");
scanf("%d",&n);
printf("\n enter %d elements",n);
for(i=0;i<n;i++)
scanf("%d",&array[i]);
for(i=0;i<n-1;i++)
for(j=0;j<n-i-1;j++)
if(array[j]>array[j+1])
{
temp=array[j];
array[j]=array[j+1];
array[j+1]=temp;
}
printf("\n array elements in sorted order \n");
for(i=0;i<n;i++)
printf("%3d",array[i]);
getch();
}
Output:
Enter no of elements:
5
Enter 5 elements
3
2
5
4
1
Array elements in sorted order
12345
5(b) write a c program to find the transpose of a given matrix.
#include<stdio.h>
#include<conio.h>
void main()
{
int a[10][10],i,j,r,c;
clrscr();
printf("\n enter order of matrix:");
scanf("%d%d",&r,&c);
printf("\n enter elements of matrix :");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
scanf("%d",&a[i][j]);
}
printf("matrix elements are: \n");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
printf("%3d",a[i][j]);
printf("\n");
}
printf("\n transpose of given matrix is \n");
for(i=0;i<c;i++)
{
for(j=0;j<r;j++)
printf("%4d",a[j][i]);
printf("\n");
}
getch();
}
Output
Enter order of matrix
2
2
Enter the elements of matrix
1
2
3
4
Matrix elements are
1 2
3 4
Program
#include<stdio.h>
#include<conio.h>
void main()
{
int a[10][10],b[10][10],c[10][10],i,j,r1,r2,c1,c2;
clrscr();
printf("enter order of matrix A:\n");
scanf("%d%d",&r1,&c1);
printf("enter order of matrix B:\n");
scanf("%d%d",&r2,&c2);
if((r1==r2) && (c1==c2))
{
printf("\n enter element of matrix A:");
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
scanf("%d",&a[i][j]);
}
printf("enter elements of matrix B:");
for(i=0;i<r2;i++)
{
for(j=0;j<c2;j++)
scanf("%d",&b[i][j]);
}
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
c[i][j]=a[i][j]+b[i][j];
}
printf("\n addition of matrices a&b is:\n");
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
printf("%3d",c[i][j]);
printf("\n");
}
}
else
printf("addition is not possible\n");
getch();
}
Output 1
Enter order of matrix A
2 2
Enter order of matrix B
2 2
Enter elements of matrix A
1111
Enter elements of matrix B
1111
Addition of matrix A&b is
22
22
Output 2
Enter order of matrix A
1
2
Enter order of matrix B
2
3
Addition is not possible
/* 6(a) write a c program to find the minimum and maximum elements
in an array */
#include<stdio.h>
#include<conio.h>
void main()
{
int a[10],n,i,max,min;
clrscr();
printf("enter no of elements into array \n ");
scanf("%d",&n);
printf("enter elements into array: \n");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
max=a[0];
min=a[0];
for(i=0;i<n;i++)
{
if(a[i]>max)
max=a[i];
else if(a[i]<min)
min=a[i];
}
printf("\n maximum=%d,minimum=%d",max,min);
getch();
}
Output
Enter no of elements into array
4
Enter elements into array
3
5
6
2
Maximum=6, minimum=2
6(b).
write a c program that uses two dimentional array toperform the
multiplication of two matrices by checking the compatability
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,k,r1,r2,c1,c2,a[10][10],b[10][10],c[10][10];
clrscr();
printf("\n enter the order of matrix A: \n");
scanf("%d%d",&r1,&c1);
printf("\n enter the order of matrix B: \n");
scanf("%d%d",&r2,&c2);
if(c1==r2)
{
printf("enter the elements of matrix A:\n");
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
scanf("%d",&a[i][j]);
}
printf("enter the elements of matrix B: \n");
for(i=0;i<r2;i++)
{
for(j=0;j<c2;j++)
scanf("%d",&b[i][j]);
}
// printf("multiplication is:\n");
for(i=0;i<r1;i++)
{
for(j=0;j<c2;j++)
{
c[i][j]=0;
for(k=0;k<r2;k++)
c[i][j]=c[i][j]+a[i][k]*b[k][j];
}
}
printf("resultant matrix is:\n");
for(i=0;i<r1;i++)
{
for(j=0;j<c2;j++)
printf("%d \t",c[i][j]);
printf("\n");
}
}
else
printf("multiplication is not possible");
getch();
}
Output
Enter the order of matrix A
2 2
Enter the order of matrix B
2 2
7(a) write a c program that reads two integers and call SUM(a,b)
function that takes two integer arguments and returns their sum of two
integers.the SUM(a,b) function should calculate the sum of two integer
values.
#include<stdion.h>
#include<conio.h>
Int sum(int,int);
Void main()
{
Int a,b,c;
Printf(enter two values for a & b”);
Scanf(“%d%d”&a,&b);
C=sum(a,b);
Printf(“sum is c=%d”,c);
}
Int sum(int a, int b)
{
Return(a+b);
}
Output:
Enter two values for a &b
3 4
Sum is c=7
/* 7(b(i))write a c program to find factorial of a given number
using non recursion */
#include<stdio.h>
#include<conio.h>
int fact(int n)
{
int f=1,i;
if((n==0) ||(n==1))
return(1);
else
for(i=1;i<=n;i++)
f=f*i;
return(f);
}
void main()
{
int n;
clrscr();
printf("enter the number:");
scanf("%d",&n);
printf("factorial of %d number is %d",n,fact(n));
getch();
}
Output:
Enter the number:
5
Factorial of 5 number is 120
7(b(ii))write a c program to solve towers of hanoi problem using non
recursion
#include<stdio.h>
#include<conio.h>
void hanoinonrecursion(int num,char ndl1,char ndl2,char ndl3)
{
if(num==1)
{
printf("move top disk from needle %c to needle %c ",ndl1,ndl2);
}
if(num==2)
{
printf("\n move top disk from needle %c to needle %c",ndl1,ndl2);
printf("\n move top disk from needle %c to needle %c",ndl1,ndl3);
printf("\n move top disk from needle %c to needle %c",ndl2,ndl3);
}
if(num==3)
{
printf("\n move top disk from needle %c to needle %c",ndl1,ndl3);
printf("\n move top disk from needle %c to needle %c",ndl1,ndl2);
printf("\n move top disk from needle %c to needle %c",ndl3,ndl2);
printf("\n move top disk from needle %c to needle %c",ndl1,ndl3);
printf("\n move top disk from needle %c to needle %c",ndl2,ndl1);
printf("\n move top disk from needle %c to needle %c",ndl2,ndl3);
printf("\n move top disk from needle %c to needle %c",ndl1,ndl3);
}
}
void main()
{
int no;
clrscr();
printf("enter the number of disks to be transfered");
scanf("%d",&no);
if((no<1)||(no>3))
printf("\n there is nothing to move");
else
printf("\n non recursion");
hanoinonrecursion(no,'A','B','C');
getch();
}
Output
Enter the number of disks to be transferred 1
Non recursion
Move top disk from needle A to needle B
7(c). write a c program to find the GCD(greatest common divisor)of two
given integers
#include<stdio.h>
#include<conio.h>
int gcd(int,int);
void main()
{
int a,b,g;
clrscr();
printf("\n enter 2 numbers:");
scanf("%d%d",&a,&b);
if(a>b)
g=gcd(a,b);
else
g=gcd(b,a);
printf("gcd=%d",g);
getch();
}
int gcd(int m,int n)
{
int r;
r=m%n;
while(r!=0)
{
m=n;
n=r;
r=m%n;
}
return(n);
}
Output
Enter 2 numbers
2
4
Gcd=2
8(a) write a c program to find the frequency of characters occurs in a
string.
Program
#include <stdio.h>
void main()
{
char str[1000], ch;
int i, frequency = 0;
clrscr();
printf("Enter a string: ");
gets(str);
for(i=0;str[i]!='\0';++i)
{
if(ch==str[i])
++frequency;
}
Output
Enter a string: sriindu
Enter a character to fing the frequency: i
Frequency of i=2
8(b) write a c program to perform the following operationsusing
string handling functions
i) toconcatenate two strings.
ii) to compare two strings
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char a[10],b[10],str[10];
int i,l;
clrscr();
printf("\n enter the string a & b :");
scanf("%s%s",a,b);
i=strcmp(a,b);
if(i==0)
printf("\n\n %s & %s strings are equal ",a,b);
else
printf("\n\n %s & %s strings are not equal ",a,b);
strcat(a,b);
printf("\n \n combined string is %s ",a);
getch();
}
Output
Enter the string a &b:sri indu
Sri &indu strings are not equal
Combined string is sriindu
8( c) write a c program that uses string handling function to determine if
the given string is a palindrome or not
#include <stdio.h>
#include <string.h>
void main()
{
char string1[20];
int i, length;
int flag = 0;
clrscr();
printf("Enter a string:");
scanf("%s", string1);
length = strlen(string1);
for(i=0;i<length;i++)
{
if(string1[i]!=string1[length-i-1])
{
flag = 1;
break;
}
}
if (flag)
{
printf("%s is not a palindrome", string1);
}
else
{
printf("%s is a palindrome", string1);
}
getch();
}
Output 1
Enter a string: madam
Madam is a palindrome
Output 2
Enter a string: sri
Sri is not a palindrome
9 create a structure named company which has name, address, phone and
no of employee as member variable. Read name of company, its address,
and phone and no of employee. finally display these members value.
#include<stdio.h>
#include<conio.h>
struct company
{
char name[20];
char addr[30];
char phone[20];
int no_of_emp;
};
void main()
{
struct company c1;
clrscr();
printf("enter the details of employe(name,address,pno,no)\n");
scanf("%s%s%s%d",&c1.name,&c1.addr,&c1.phone,&c1.no_of_emp);
printf("details of employee are \n");
printf("\n employee name =%s",c1.name);
printf("\n employee address=%s",c1.addr);
printf("\n employee pnum=%s",c1.phone);
printf("\n employee number=%d",c1.no_of_emp);
getch();
}
Output:
Enter the details of employee(name,address,pno,no)
Sri
Hyd
9638527410
20
Details of employee are
Sri
Hyd
9638527410
20
//10(a)
/* write a c program that uses functions to perform the following
operations
using structures
a) reading a complex number
b)writing a complex number
c) addition of two complex numbers
d) multiplication of two complex numbers*/
//----------------------------------------------------------------------------
Program
Output
Enter first complex number: 2 3
Enter second complex number: 3 3
Addition= 5.00+(6.00)i
Multiplication=-3.00+(15.00)i
10(b) write a c program to pass address of structure variable to user
defined function and display the contents
#include<stdio.h>
struct student
{
char name[50];
int.roll;
};
void display (struct student *stud);
void main()
{
struct student *stud;
clrscr();
printf("enter the student name:");
scanf("%[^\n]%*c",stud->name);
printf("enter roll number:");
scanf( "%d",&stud->roll);
display(stud);
getch();
}
void display(struct student *stu)
{
printf("\n\n OUTPUT \n name:%s",stu->name);
printf("\n roll :%d",stu->roll);
}
Output:
Enter the student name: sri
Enter roll number: 10
OUTPUT
Name sri
Roll:10
Unit-III
#include<stdio.h>
#include<conio.h>
Void main()
{
Int a[10]={12,13,14,15,17};
Int *ptr;
Clrscr();
Ptr=a;
Printf(“\n initial address is %u,ptr);
Ptr++;
Printf(“\n after increment operation on pointer the address is %u”,ptr);
--ptr;
Printf(“\n after decrement operation on pointer the address is %u”,ptr);
Printf(“\n adding an integer to address %u”,ptr+=4);
Printf(“\n subtracting an integer from address %u”,ptr-=4);
Getch();
}
Output:
Initial address is 65506
After increment operation on pointer the address is65508
After decrement operation on pointer the address is65506
Adding an integer to address 65514
Subtracting an integer from address 65506
11(b) write a c program to find the largest values in the array using
pointer
#include<stdio.h>
#include<conio.h>
void main()
{
long array[100],*maximum,size,i,location=1;
clrscr();
printf("enter the number of elements in array \n");
scanf("%ld",&size);
printf("enter %ld integer \n",size);
for(i=0;i<size;i++)
scanf("%ld",&array[i]);
maximum=array;
*maximum=*array;
for(i=1;i<size;i++)
{
if(*(array+i)>*maximum)
{
*maximum=*(array+i);
location=i+1;
}
}
printf("maximum elements is prsent at location %ld and its value is %ld
\n",location,*maximum);
getch();
}
Output:
Enter the number of elements in array: 3
Enter 3 integers
3
6
9
Maximum element is present at location3 and its value is 9
11(c )write a c program to swap value of two variables using
pointer(note: call by reference)
#include<stdio.h>
#include<conio.h>
void swap(int *,int *);
int main()
{
int a,b;
clrscr();
printf("\n in main enter values of a and b :\n");
scanf("%d%d",&a,&b);
printf("in main() before swaping values are %d %d",a,b);
printf("\n\n address of a =%u and b=%u ",&a,&b);
swap(&a,&b);
printf("\n after swaping in main() values are %d %d",a,b);
getch();
return 0;
}
Output:
Size of structure 12
A=1902
Enter rno and name
101
Indu
Rno=101
Name=indu
12(b) write a c program to demonstrate the reallocate the allocated
memory using realloc()function.
#include<stdio.h>
#include<conio.h>
Void main()
{
Int arr[2],I;
Int *ptr=arr;
Int *ptr_new;
Arr[0]=10;
Arr[1]=20;
Ptr_new=(int*)realloc(ptr,sizeof(int)*3);
*(ptr_new+2)=30;
For(i=0;i<3;i++)
Printf(“%d”,*(ptr_new+i));
Getcgar();
}
Output:
10
20
30
12(c ) write a c program to find the sum of all the elements in one
dimentional array using dynamic memory allocation
#include<stdio.h>
#include<conio.h>
Voia main()
{
Int num,I,*ptr,sum=0;
Printf(“enter number of elements:”);
Scanf(“%d”,&num);
Ptr=(int*)calloc(num,sizeof(int));
If(ptr==NULL)
{
Printf(“error! Memory not allocated”) ;
Exit(0);
}
Printf(“enter elements of array:”);
For(i=0;i<num;++I)
{
Scanf(“%d”,ptr+i);
Sum+=*(ptr+i);
}
Printf(“sum=%d”,sum);
Free(ptr);
}
Output:
Enter number of elements: 3
Enter elements of array;1 2 3
Sum=6
/* 13 (i)
write a c program to find factorial of a given number
using recursion */
#include<stdio.h>
#include<conio.h>
int factorial(int n);
void main()
{
int res,x;
clrscr();
printf("\n enter a number for factorial: ");
scanf("%d",&x);
res=factorial(x);
printf(" \n factorial of %d is %d",x,res);
getch();
}
int factorial(int n)
{
if(n==0)
return 1;
else
return(n*factorial(n-1));
}
Output
Enter a number for factorial:5
Factorial of 5 is 120.
13(ii) write a c program to solve towers of hanoi problem using
recursion
#include<stdio.h>
#include<conio.h>
hanoirecursion(num-1,ndl1,ndl3,ndl2);
printf("move top disk from needle %c to needle %c \n\n",ndl1,ndl2);
hanoirecursion(num-1,ndl3,ndl2,ndl1);
}
void main()
{
int no;
clrscr();
printf("\n\n enter the number of disks to be transferred:");
scanf("%d",&no);
if(no<1)
printf("\n there is nothing to move");
else
hanoirecursion(no,'A','B','C');
getch();
}
Output
Enter the number of disks to be transferred: 1
Move top disk from needle A to needle B
Unit -IV
14(a) write a c program to use the identifier for 3.14 as PI to find the
area of circle.
#include<stdio.h>
#include<conio.h>
#defined pie 3.142
Float r;
Void area():
Void main()
{
Clrscr();
Printf(“enter radius value”);
Scanf(“%f”,&r);
Area();
Getch();
}
Void area()
{
Float a;
A=pie*r*r;
Printf(“area is %f”,a);
}
Output:
Enter radius value
2
Area is 12.568000
14(B) write a c program to use conditional compilation directive
statements as to whether the identifier is defined or not
#include<stdio.h>
#include<conio.h>
#define LINE 1
void main()
{
clrscr();
#ifdef LINE
{
printf("\n line is defined");
}
#else
{
printf("\n line is not defined");
}
#endif
getch();
}
Output:
Line is defined
14(c ) write a c program to identify whether the entered character is a
letter or a digit or an uppercase letter or a lower case using predefined
macros
#include<stdio.h>
#include<conio.h>
#include<ctype.h>
void main()
{
char ch;
clrscr();
printf("Enter a character:");
scanf("%c",&ch);
if(isupper(ch))
{
printf("\n Upper case letter");
}
else if(islower(Ch))
{
printf("\n Lower case letter");
}
else if(isdigit(Ch))
{
printf("\n Digit");
}
getch();
}
Output1:
Enter a Character: H
Upper case letter
Output2:
Enter a Character: g
Lower case letter
Output3:
Enter a Character: 8
Digit
15(a) C Program - Read and Display File's Content */
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void main()
{
FILE *fs;
char ch, *fname;
clrscr();
printf("Enter file name to read and display its content (like file.txt)
: ");
scanf("%s",fname);
fs=fopen(fname, "r");
if(fs==NULL)
{
printf("Error in opening file..!!");
getch();
exit(0);
}
While(1)
{
Ch=fgetc(fs);
If(ch==EOF)
Break ;
Else
Fputch(ch,stdout);
}
printf("\n");
fclose(fs);
getch();
}
Output:
#include <stdio.h>
#include <conio.h>
#include <string.h>
void main()
{
FILE *fs,*ft;
char ch;
int w=0,c=0,l=0;
clrscr();
fs=fopen(“a.txt”,”r”);
if(fs==NULL)
{
printf(“source file can’t be opened”);
exit(0);
}
while((ch=fgetc(fs))!=EOF)
{
if(ch==’\n’)
{
l++;
w++;
}
else if(ch==’\0’|| ch==’ ‘)
{
w++:
c++;
}
else
c++;
}
printf(“\n number of characters are %d”,c);
printf(“\n number of words are %d”,w);
printf(“\n number of lines are %d”,l);
getch();
}
Output:
#include <stdio.h>
#include <stdlib.h>
int main()
{
// Open two files to be merged
FILE *fp1 = fopen("file1.txt", "r");
FILE *fp2 = fopen("file2.txt", "r");
fclose(fp1);
fclose(fp2);
fclose(fp3);
return 0;
}
Output:
Unit-v
#include<stdio.h>
#include<conio.h>
void linear(int[],int,int);
void main()
{
int i,n,key;
int a[50];
clrscr();
printf("\n how many elements you want to insert into an array= \n");
scanf("%d",&n);
printf("\n\n enter the array elements= \n");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("the elements in the array=\n");
for(i=0;i<n;i++)
{
printf("%5d",a[i]);
}
printf("\n which elements you want to search=");
scanf("%d",&key);
linear(a,n,key);
getch();
}
Output
How many elements you want to insert into an array=5
Enter the array elements:
3
2
5
1
4
The elements in the array=
4 2 5 1 4
Which element you want to search=5
Search successful
Element 5 found at location 3
Output 2
How many elements you want to insert into an array=5
Enter the array elements:
3
2
5
1
4
The elements in the array=
32 5 1 4
Which element you want to search=6
Unsuccessfull search 6 not found
16(b) binary search method
#include<stdio.h>
#include<conio.h>
void binary(int [],int,int);
void main()
{
int a[50];
int i,n,key;
clrscr();
printf("\n enter how many elements you want to insert=\n");
scanf("%d",&n);
printf("\n enter the elements in the ascending order= \n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("the array elements are = \n");
for(i=0;i<n;i++)
printf("%5d",a[i]);
printf("\n which elements you want to search=");
scanf("%d",&key);
binary(a,n,key);
getch();
}
high=mid-1;
else
if(key>a[mid])
low=mid+1;
else
if(key==a[mid])
{
printf("\n search successful \n");
printf("\n elements %d found at location %d \n",key,mid+1);
flag=0;
break;
}
}
if(flag)
printf("\n unsuccessful search %d not found",key);
}
Output:
How many elements you want to insert into an array=5
Enter the elements in the ascending order:
1
2
3
4
5
The elements in the array=
1 2 345
Which element you want to search=5
Search successful
Element 5 found at location 5
Output 2
How many elements you want to insert into an array=5
Enter the elements in the ascending order:
1
2
3
4
5
The elements in the array=
12345
Which element you want to search=6
Unsuccessfull search 6 not found
#include<stdio.h>
#include<conio.h>
#include<alloc.h>
Output
#include<stdio.h>
#include<conio.h>
void main()
{
int a[20],i,j,temp,n;
clrscr();
printf("\n enter the size of an array: \n");
scanf("%d",&n);
printf("enter array elementas \n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("\n selection sort: \n\n");
printf("array elements before sorting: \n");
for(i=0;i<n;i++)
printf("%d \t",a[i]);
for(i=0;i<=n-2;i++)
{
for(j=i+1;j<n;j++)
{
if(a[i]>a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
printf("\n array after sorting: \n");
for(i=0;i<n;i++)
printf("%d \t",a[i]);
getch();
}
Output
#include<stdio.h>
#include<conio.h>
void main()
{
int a[20],i,j,k,temp,n;
clrscr();
printf("enter the size of an array: \n");
scanf("%d",&n);
printf("enter the elements in to an array: \n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("\n array before sorting \n");
for(i=0;i<n;i++)
printf("%d \t",a[i]);
printf("\n\n insertion sort \n");
for(i=1;i<n;i++)
{
for(j=0;j<i;j++)
{
if(a[j]>a[i])
{
temp=a[j];
a[j]=a[i];
for(k=i;k>j;k--)
a[k]=a[k-1];
a[k+1]=temp;
}
}
}
printf("\n array after sorting: \n");
for(i=0;i<n;i++)
printf("%d \t",a[i]);
getch();
}
Output
Sample programs
#includd<stdio.h>
#include<conio.h>
#include<process.h>
Void main(int arg,char *arr[])
{
FILE *ft,*fs;
Char ch;
Clrscr();
If(arg!=3)
{
Puts(“\n arguments missing! Press key to exit”);
Getch();
Exit(0);
}
Fs=fopen (arr[1],”r”);
If(fs==NULL)
{
Puts(“\n cannot open source file! Pree key to exit”);
Getch();
Exit(0);
}
Ft=fopen(Arr[2],”w”);
If(ft==NULL)
{
Puts(“\n cannot copy file ! pree key to exit”);
Fclose(fs);
getch();
exit(0);
}
While(1)
{
Ch=getc(fs);
If(ch==EOF)
{
Break;
}
Else
Putc(ch,ft);
}
Printf(“file copied successfully”);
Fclose(fs);\
Fclose(ft);
Output:
F2(save) ->alt+f9 ->file->DOS shell ->
c:\tcc>copy con file1.txt
sri indu
^z(save)
1 file(s) copied
C:\tcc >exit
Ctrl+f9
File-> DOS shell
C:\tcc> src.exe file1.txt file2.txt
File copied successfully
C:/tcc> type file2.txt
Sri indu
#include<stdio.h>
#include<conio.h>
void fibn(int n)
{
int a,b,c,i;
a=0;
b=1;
printf("\n first %d terms of fibonacci series \n",n);
printf(" %d \n %d \n ", a,b);
for(i=0;i<n-2;i++)
{
c=a+b;
printf(" %d \n",c);
a=b;
b=c;
}
}
void main()
{
int x;
clrscr();
printf(" how many fibonacci numbers you want \n " );
scanf("%d" , &x);
fibn(x);
getch();
}
#include<stdio.h>
#include<conio.h>
void fibn(int a, int b, int n)
{
int c;
static int i=2;
if(i<n)
{
c=a+b;
printf(" %d \n",c);
i=i+1;
a=b;
b=c;
fibn(a,b,n);
}
else return;
}
void main()
{
int f1,f2,x;
clrscr();
f1=0;
f2=1;
printf(" how many fibonacci numbers you want \n " );
scanf("%d" , &x);
printf("\n first %d terms of fibonacci series \n",x);
printf(" %d \n %d\n ", f1,f2);
fibn(f1,f2,x);
getch();
}