Download as pdf or txt
Download as pdf or txt
You are on page 1of 56

P1 Write a C program to calculate area and circumference of a circle.

#include<stdio.h>
#include<conio.h>
void main()
{
int r;
float a,c;
clrscr();
printf("\n Enter the radius of circle");
scanf("%d",&r);
a=3.14*r*r;
c=2*3.14*r;
printf("\n AREA=%f",a);
printf("\n CIRCUMFERENCE=%f",c);
getch();
}

P2 Write a C program to find simple interest.

#include<stdio.h>
#include<conio.h>
void main()
{
int p,t;
float r,si;
clrscr();
printf("\n Enter the principal amount");
scanf("%d",&p);
printf("\n Enter the rate of interest");
scanf("%f",&r);
printf("\n Enter the time period");
scanf("%d",&t);
si=(p*r*t)/100;
printf("\n RATE OF INTEREST=%f",si);
getch();
}

P3 Write a C program to find addition of two numbers.

#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c;
clrscr();
printf("\n Enter two value");

Page 1
scanf("%d%d",&a,&b);
c=a+b;
printf("\n SUM=%d",c);
getch();
}

P4 Write a C program to convert Fahrenheit temperature to Celsius temperature.

#include<stdio.h>
#include<conio.h>
void main()
{
float fa,cel;
clrscr();
printf("\n Enter temprature in fahrenhiet");
scanf("%f",&fa);
cel=(fa-32)*5.0/9.0;
printf("\n CELSICUS TEMPRATURE=%f",cel);
getch();
}

P5 Write a C program to find the area of triangle.

#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
float a,b,c,s,area;
clrscr();
printf("\n Enter three sides of triangle");
scanf("%f%f%f",&a,&b,&c);
s=(a+b+c)/2;
area=sqrt(s*(s-a)*(s-b)*(s-c));
printf("\n AREA OF TRIANGLE=%f",area);
getch();
}

P6 Write a C program to find the area and circumference of a rectangle.

#include<stdio.h>
#include<conio.h>
void main()
{
float l,w,a,c;
clrscr();
printf("\n Enter two sides of rectangle");
scanf("%f%f",&l,&w);
a=l*w;
Page 2
c=2*(l+w);
printf("\n AREA OF RECTANGLE=%f",a);
printf("\n CIRCUMFERENCE OF RECTANGLE=%f",c);
getch();
}

P7 Write a C program to swap two variables.

#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,t;
clrscr();
printf("\n Enter two values");
scanf("%d%d",&a,&b);
t=a;
a=b;
b=t;
printf("\n a=%d b=%d",a,b);
getch();
}

P8 Write a C program to swap two variables without using third variable.

#include<stdio.h>
#include<conio.h>
void main()
{
int a,b;
clrscr();
printf("\n Enter two values");
scanf("%d%d",&a,&b);
a=a+b;
b=a-b;
a=a-b;
printf("\n a=%d b=%d",a,b);
getch();
}

P9 Write a C program to calculate the compound interest.

#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
float p,r,n,ci;
clrscr();
printf("\n Enter principal amount");
Page 3
scanf("%f",&p);
printf("\n Enter rate of interest");
scanf("%f",&r);
printf("\n Enter number of years");
scanf("%f",&n);
ci=p*pow((1+r/100),n);
printf("\n COMPOUND INTEREST=%f",ci);
getch();
}

P10 Write a C program to evaluate the following expression

#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int x,n;
float y;
clrscr();
printf("\n Enter thr value of x and n");
scanf("%d%d",&x,&n);
y=pow((x+5),n)-sqrt((x*3)/(2*n));
printf("\n y=%f",y);
getch();
}

P11 Write a C program to find the largest number between the two numbers using
conditional operator.

#include<stdio.h>
#include<conio.h>
void main()
{
int m,n,max;
clrscr();
printf("\n Enter two values");
scanf("%d%d",&m,&n);
max=m>n?m:n;
printf("\n MAXIMUM VALUE=%d",max);
getch();
}

P12 Write a C program to find the smallest number between the two numbers using
conditional operator.

#include<stdio.h>
#include<conio.h>
void main()
Page 4
{
int m,n,min;
clrscr();
printf("\n Enter two values");
scanf("%d%d",&m,&n);
min=m<n?m:n;
printf("\n MINIMUM VALUE=%d",min);
getch();
}

P13 Write a C program to find largest number among three numbers using conditional
operator.

#include<stdio.h>
#include<conio.h>
void main()
{
int m,n,p,max;
clrscr();
printf("\n Enter three values");
scanf("%d%d%d",&m,&n,&p);
max=m>n?(m>p?m:p):(n>p?n:p);
printf("\n MAXIMUM VALUE=%d",max);
getch();
}

P14 Write a C program to find smallest number among three numbers using conditional
operator.

#include<stdio.h>
#include<conio.h>
void main()
{
int m,n,p,min;
clrscr();
printf("\n Enter three values");
scanf("%d%d%d",&m,&n,&p);
min=m<n?(m<p?m:p):(n<p?n:p);
printf("\n MINIMUM VALUE=%d",min);
getch();
}

P15 Write a C program to find the largest number between the two numbers using if
statement.

#include<stdio.h>
#include<conio.h>
void main()
{
Page 5
int a,b,max;
clrscr();
printf("\n Enter two values");
scanf("%d%d",&a,&b);
max=a;
if(max<b)
max=b;
printf("\n MAXIMUM VALUE=%d",max);
getch();
}

P16 Write a C program to find the smallest number between the two numbers using if
statement.

#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,min;
clrscr();
printf("\n Enter two values");
scanf("%d%d",&a,&b);
min=a;
if(min>b)
min=b;
printf("\n MINIMUM VALUE=%d",min);
getch();
}

P17 Write a C program to find largest number among three numbers using if statement.

#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c,max;
clrscr();
printf("\n Enter three values");
scanf("%d%d%d",&a,&b,&c);
max=a;
if(max<b)
max=b;
if(max<c)
max=c;
printf("\n MAXIMUM VALUE=%d",max);
getch();
}
Page 6
P18 Write a C program to find smallest number among three numbers using if statement.

#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c,min;
clrscr();
printf("\n Enter three values");
scanf("%d%d%d",&a,&b,&c);
min=a;
if(min>b)
min=b;
if(min>c)
min=c;
printf("\n MINIMUM VALUE=%d",min);
getch();
}

P19 Write a C program to check whether two numbers are equal or not.

#include<stdio.h>
#include<conio.h>
void main()
{
int x,y;
clrscr();
printf("\n Enter two number");
scanf("%d%d",&x,&y);
if(x==y)
printf("\n given number are equal ");
else
printf("\n given number are not equal");
getch();
}

P20 Write a C program to check whether a number is Even or Odd.

#include<stdio.h>
#include<conio.h>
void main()
{
int a,r;
clrscr();
printf("\n Enter a number");
scanf("%d",&a);
r=a%2;
if(r==0)
Page 7
printf("\n given number is EVEN ");
else
printf("\n given number is ODD");
getch();
}

P21 Write a C program to check whether a year is leap year.

#include<stdio.h>
#include<conio.h>
void main()
{
int year,rem_4,rem_100,rem_400;
clrscr();
printf("\n Enter a year");
scanf("%d",&year);
rem_4=year%4;
rem_100=year%100;
rem_400=year%400;
if((rem_4==0&&rem_100!=0)||(rem_400==0))
printf("\nIt is leap year");
else
printf("\nIt is not leap year");
getch();
}

P22 Write a C program to check that a number is positive or negative or zero.

#include<stdio.h>
#include<conio.h>
void main()
{
int x;
clrscr();
printf("\n Enter a number");
scanf("%d",&x);
if(x==0)
printf("\n given number is ZERO");
else if(x>0)
printf("\n given number is POSITIVE");
else
printf("\n given number is NEGATIVE");
getch();
}

Page 8
P23 Write a C program to print the grade according to the marks secured by students.
Marks Grade
>=90 A
>=80 B
>=70 C
>=60 D
otherwise F
#include<stdio.h>
#include<conio.h>
void main()
{
int marks;
char grade;
clrscr();
printf("\n Enter marks");
scanf("%d",&marks);
if(marks>=90)
grade='A';
else if(marks>=80)
grade='B';
else if(marks>=70)
grade='C';
else if(marks>=60)
grade='D';
else
grade='F';
printf("\nGRADE OF STUDENT=%c",grade);
getch();
}

P24 Write a C program to find the roots of a quadratic equation ax2+bx+c = 0 for all possible
combination of a, b and c.

#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
float a,b,c;
float d,X1,X2,X;
clrscr();
printf("\n Enter the value of constant a,b and c");
scanf("%f%f%f",&a,&b,&c);
d=b*b-4*a*c;
if(d>0)
{
printf("Two real and unequal root");
X1=(-b+sqrt(d))/(2*a);
X2=(-b-sqrt(d))/(2*a);
Page 9
printf("\nX1=%f X2=%f",X1,X2);
}
else if(d==0)
{
printf("Single real root");
X=-b/(2*a);
printf("\n x=%f",X);
}
else
printf("\n Roots are imaginary");
getch();
}
P25 Write a C program to find the value of y using if-else ladder.
1+x n=1
y= 1+x/n n=2
1+xn n=3
1+nx otherwise

#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int x,n,y;
clrscr();
printf("\n Enter the value of x and n");
scanf("%d%d",&x,&n);
if(n==1)
y=1+x;
else if(n==2)
y=1+x/n;
else if(n==3)
y=1+pow(x,n);
else
y=1+n*x;
printf("\ny=%d",y);
getch();
}

P26 Write a C program to implement simple calculator (addition, subtraction, multiply,


divide).

#include<stdio.h>
#include<conio.h>
void main()
{
int x,y;
char operator;
clrscr();
printf("\n Enter two operand value");
Page 10
scanf("%d%d",&x,&y);
printf("\n Enter the operator");
fflush(stdin);
scanf("%c",&operator);
switch(operator)
{
case '+':
printf("\m SUM=%d",x+y);
break;
case '-':
printf("\m DIFFERENCE=%d",x-y);
break;
case '*':
printf("\m PRODUCT=%d",x*y);
break;
case '/':
printf("\m DIVISION=%d",x/y);
break;
default:
printf("\n You have enterd wrong operator");
}
getch();
}

P27 Write a C program to check whether a character entered by the user is a vowel or not.

#include<stdio.h>
#include<conio.h>
void main()
{
char alphabet;
clrscr();
printf("\n Enter the alphabet");
fflush(stdin);
scanf("%c",&alphabet);
switch(alphabet)
{
case 'a':
case 'A':
case 'e':
case 'E':
case 'i':
case 'I':
case 'o':
case 'O':
case 'u':
case 'U':
printf("\n Alphabet is vowel ");
break;
default:
Page 11
printf("\n Alphabet is not vowel");
}
getch();
}

P28 Write a C program to find the value of y using switch.

1+x n=1
y= 1+x/n n=2
1+xn n=3
1+nx otherwise
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int x,n;
clrscr();
printf("\n Enter the value of x and n");
fflush(stdin);
scanf("%d%d",&x,&n);
switch(n)
{
case 1:
printf("\n y=%d",1+x);
break;
case 2:
printf("\n y=%d",1+x/n);
break;
case 3:
printf("\n y=%f",1+pow(x,n));
break;
default:
printf("\n y=%d",1+n*x);
}
getch();
}

P29 Write a C program to find the average of n number.

#include<stdio.h>
#include<conio.h>
void main()
{
int n,c,s,a;
float avg;
clrscr();
printf("\n How many number");
scanf("%d",&n);
Page 12
s=0;
c=1;
while(c<=n)
{
printf("\n Enter the data");
scanf("%d",&a);
s=s+a;
c=c+1;
}
avg=(float)s/n;
printf("\n AVERAGE=%f",avg);
getch();
}

P30 Write a C program to find the sum of digit of a integer number.

#include<stdio.h>
#include<conio.h>
void main()
{
int n,s,r;
clrscr();
printf("\n Enter a number");
scanf("%d",&n);
s=0;
while(n>0)
{
r=n%10;
s=s+r;
n=n/10;
}
printf("\n SUM OF DIGIT=%d",s);
getch();
}

P31 Write a C program to find the reverse of a integer number.

#include<stdio.h>
#include<conio.h>
void main()
{
int n,rn,r;
clrscr();
printf("\n Enter a number");
scanf("%d",&n);
rn=0;
while(n>0)
{
r=n%10;
Page 13
rn=rn*10+r;
n=n/10;
}
printf("\n REVERSE NUMBER=%d",rn);
getch();
}

P32 Write a C program to check whether the given number is an Armstrong number.

#include<stdio.h>
#include<conio.h>
void main()
{
int n,s,r,q;
clrscr();
printf("\n Enter a number");
scanf("%d",&n);
s=0;
q=n;
while(n>0)
{
r=n%10;
s=s+r*r*r;
n=n/10;
}
if(s==q)
printf("\n Number is Armstrong");
else
printf("\n Number is Not Armstrong");
getch();
}

P33 Write a C program to find whether a integer number is palindrome or not.

#include<stdio.h>
#include<conio.h>
void main()
{
int n,rn,r,q;
clrscr();
printf("\n Enter a number");
scanf("%d",&n);
rn=0;
q=n;
while(n>0)
{
r=n%10;
rn=rn*10+r;
n=n/10;
Page 14
}
if(rn==q)
printf("\n Number is Palindrome");
else
printf("\n Number is Not Palindrome");
getch();
}

P34 Write a C program find the factorial of number.

#include<stdio.h>
#include<conio.h>
void main()
{
int n,i;
long int fact;
clrscr();
printf("\n Enter a number");
scanf("%d",&n);
fact=1;
for(i=1;i<=n;i++)
fact=fact*i;
printf("\nFACTORIAL=%ld",fact);
getch();
}

P35 Write a C program check that a number is prime or not.

#include<stdio.h>
#include<conio.h>
void main()
{
int n,r,i;
int flag;
clrscr();
printf("\n Enter a number");
scanf("%d",&n);
flag=0;
for(i=2;i<=n-1;i++)
{
r=n%i;
if(r==0)
{
flag=1;
break;
}
}
if(flag==0)
printf("\nNUMBER IS PRIME");
Page 15
else
printf("\n NUMBER IS NOT PRIME");
getch();
}

P36 Write a C program to input a five digit positive number and then display it as shown
below.(Example number=24689)
24689
4689
689
89
9
#include<stdio.h>
#include<conio.h>
void main()
{
int n,i;
clrscr();
printf("\n Enter a five digit number");
scanf("%d",&n);
for(i=10000;i>0;i=i/10)
{
printf("\n%d",n);
n=n%i;
}
getch();
}

P37 Write a C program to display the pattern of n lines as follows. (Example: n=4)
* * * *
* * * *
* * * *
* * * *
#include<stdio.h>
#include<conio.h>
void main()
{
int n,i,j;
clrscr();
printf("\n How many lines");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
printf("*");
printf("\n");
}
getch();
}
Page 16
P38 Write a C program to display multiplication table up to n.

#include<stdio.h>
#include<conio.h>
void main()
{
int n,i,j,k,l;
clrscr();
printf("\n How many multiplication table");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
for(j=1;j<=10;j++)
printf("\t%d",i*j);
printf("\n");
}
getch();
}

P39 Write a C program to display the pattern of n lines as follows. (Example: n=4)

*
* * *
* * * * *
* * * * * * *

#include<stdio.h>
#include<conio.h>
void main()
{
int n,i,j,k,l;
clrscr();
printf("\n How many lines");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
for(j=i;j<=n-1;j++)
printf(" ");
for(k=1;k<=i;k++)
printf("*");
for(l=1;l<=i-1;l++)
printf("*");
printf("\n");
}
getch();
}

Page 17
P40 Write a C program to display the pattern of n lines as follows. (Example: n=4)

*
* *
* * *
* * * *

#include<stdio.h>
#include<conio.h>
void main()
{
int n,i,j,k,l;
clrscr();
printf("\n How many lines");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
for(j=i;j<=n-1;j++)
printf(" ");
for(k=1;k<=i;k++)
printf("*");
printf("\n");
}
getch();
}

P41 Write a C program to display the pattern of n lines as follows. (Example: n=4)

*
* *
* * *
* * * *

#include<stdio.h>
#include<conio.h>
void main()
{
int n,i,j;
clrscr();
printf("\n How many lines");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
for(j=1;j<=i;j++)
printf("*");
printf("\n");
}
getch();
}
Page 18
P42 Write a C program to display the pattern of n lines as follows. (Example: n=4)

1
1 2
1 2 3
1 2 3 4

#include<stdio.h>
#include<conio.h>
void main()
{
int n,i,j;
int t;
clrscr();
printf("\n How many lines");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
t=1;
for(j=1;j<=i;j++)
printf("%d",t++);
printf("\n");
}
getch();
}

P43 Write a C program to display the pattern of n lines as follows. (Example: n=4)

1
2 3
4 5 6
7 8 9 10

#include<stdio.h>
#include<conio.h>
void main()
{
int n,i,j;
int t;
clrscr();
printf("\n How many lines");
scanf("%d",&n);
t=1;
for(i=1;i<=n;i++)
{
for(j=1;j<=i;j++)
printf("%d",t++);
Page 19
printf("\n");
}
getch();
}

P44 Write a C program to display the pattern of n lines as follows.( Example: n=4)

A
A B
A B C
A B C D

#include<stdio.h>
#include<conio.h>
void main()
{
int n,i,j;
char t;
clrscr();
printf("\n How many lines");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
t='A';
for(j=1;j<=i;j++)
printf("%c",t++);
printf("\n");
}
getch();
}

P45 Write a C program to display the pattern of n lines as follows. ( Example: n=4)

A
B C
D E F
G H I J

#include<stdio.h>
#include<conio.h>
void main()
{
int n,i,j;
char t;
clrscr();
printf("\n How many lines");
scanf("%d",&n);
t='A';
Page 20
for(i=1;i<=n;i++)
{
for(j=1;j<=i;j++)
printf("%c",t++);
printf("\n");
}
getch();
}

P46 Write a C program to display the pattern of n lines as follows.(Example: n=4)

1
2 3 2
3 4 5 4 3
4 5 6 7 6 5 4

#include<stdio.h>
#include<conio.h>
void main()
{
int n,i,j,k,l;
int t;
clrscr();
printf("\n How many lines");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
for(j=i;j<=n-1;j++)
printf(" ");
t=i;
for(k=1;k<=i;k++)
printf("%d",t++);
t=t-2;
for(l=1;l<=i-1;l++)
printf("%d",t--);
printf("\n");
}
getch();
}

P47 Write a C program to display the pattern of n lines as follows.( Example: n=4)

1
1 2 1
1 2 3 2 1
1 2 3 4 3 2 1

Page 21
#include<stdio.h>
#include<conio.h>
void main()
{
int n,i,j,k,l;
int t;
clrscr();
printf("\n How many lines");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
for(j=i;j<=n-1;j++)
printf(" ");
t=1;
for(k=1;k<=i;k++)
printf("%d",t++);
t=t-2;
for(l=1;l<=i-1;l++)
printf("%d",t--);
printf("\n");
}
getch();
}

P48 Write a C program to display the pattern of n lines as follows. (Example: n=4)

* * * * * * *
* * * * *
* * *
*

#include<stdio.h>
#include<conio.h>
void main()
{
int n,i,j,k,l;
clrscr();
printf("\n How many lines");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
for(j=1;j<=i-1;j++)
printf(" ");
for(k=i;k<=n;k++)
printf("*");
for(l=i;l<=n-1;l++)
printf("*");
printf("\n");
}
Page 22
getch();
}

P49 Write a C program to display the pattern of n lines as follows. (Example: n=4)

* * * *
* * *
* *
*

#include<stdio.h>
#include<conio.h>
void main()
{
int n,i,j;
clrscr();
printf("\n How many lines");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
for(j=i;j<=n;j++)
printf("*");
printf("\n");
}
getch();
}

P50 Write a C program to display the pattern of n lines as follows. (Example: n=4)

* * * *
* * *
* *
*
#include<stdio.h>
#include<conio.h>
void main()
{
int n,i,j,k,l;
clrscr();
printf("\n How many lines");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
for(j=1;j<=i-1;j++)
printf(" ");
for(k=i;k<=n;k++)
printf("*");
printf("\n");
Page 23
}
getch();
}

P51 Write a C program to evaluate the series given below.

#include<stdio.h>
#include<conio.h>
void main()
{
float s,x,term;
int n,c,i;
clrscr();
printf("\nEnter value of x and n");
scanf("%f%d",&x,&n);
s=0;
c=1;
term=x;
i=1;
while(c<=n)
{
s=s+term;
term=(term*x*x*(-1))/((i+1)*(i+2));
i=i+2;
c=c+1;
}
printf("\nSUM=%f",s);
getch();
}

P52 Write a C program to evaluate the series given below.

#include<stdio.h>
#include<conio.h>
void main()
{
int s,x,term;
int n,c;
clrscr();
printf("\nEnter value of x and n");
scanf("%d%d",&x,&n);
s=0;
c=1;
term=x*x;
while(c<=n)
Page 24
{
s=s+term;
term=term*x*x*(-1);
c=c+1;
}
printf("\nSUM=%d",s);
getch();
}

P53 Write a C program to evaluate the series given below.

#include<stdio.h>
#include<conio.h>
void main()
{
int s,term,m;
int n,c;
clrscr();
printf("\nEnter value of n");
scanf("%d",&n);
s=0;
c=1;
term=1;
m=-1;
while(c<=n)
{
s=s+term*term*term*m;
term=term+2;
m=m*(-1);
c=c+1;
}
printf("\nSUM=%d",s);
getch();
}

P54 Write a C program to evaluate the series given below.

#include<stdio.h>
#include<conio.h>
void main()
{
int s,term;
int n,c;
clrscr();
Page 25
printf("\nEnter value of n");
scanf("%d",&n);
s=0;
c=1;
term=1;
while(c<=n)
{
s=s+term*term*term*term;
term=term+2;
c=c+1;
}
printf("\nSUM=%d",s);
getch();
}

P55 Write a C program to evaluate the series given below.

#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int s,term;
int n,c;
clrscr();
printf("\nEnter value of n");
scanf("%d",&n);
s=0;
c=1;
term=1;
while(c<=n)
{
s=s+term;
term=term*(c+1);
c=c+1;
}
printf("\nSUM=%d",s);
getch();
}

P56 Write a C program to evaluate the series given below.

#include<stdio.h>
Page 26
#include<conio.h>
#include<math.h>
void main()
{
float s,base,exponent;
int n,c;
clrscr();
printf("\nEnter value of n");
scanf("%d",&n);
s=0;
c=1;
base=2;
exponent=1;
while(c<=n)
{
s=s+pow(base,exponent);
base=base+2;
exponent=exponent+2;
c=c+1;
}
printf("\nSUM=%f",s);
getch();
}
P57 Write a C program to convert a binary number into a decimal number.

#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int n,b,r,s,i;
clrscr();
printf("\nEnter a binary number");
scanf("%d",&n);
s=0;
i=0;
while(n>0)
{
r=n%10;
s=s+r*pow(2,i);
n=n/10;
i=i+1;
}
printf("\nDECIMAL NUMBER=%d",s);
getch();
}

P58 Write a C program to convert a decimal number to a binary number.

#include<stdio.h>
Page 27
#include<conio.h>
#include<math.h>
void main()
{
int a[100],n,i,j;
clrscr();
printf("\nEnter a decimal number");
scanf("%d",&n);
i=0;
while(n!=0)
{
a[i]=n%2;
n=n/2;
i=i+1;
}
printf("Binary number ");
for(j=i-1;j>=0;j--)
printf("%d",a[j]);
getch();
}

P59 Write a C program to find the biggest number out of n numbers.

#include<stdio.h>
#include<conio.h>
void main()
{
int a[100],n,i,max;
clrscr();
printf("\n How many data");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter data value");
scanf("%d",&a[i]);
}
printf("\n content of array");
for(i=0;i<n;i++)
printf("\t%d",a[i]);

max=a[0];
for(i=1;i<n;i++)
{
if(a[i]>max)
max=a[i];
}
printf("\nmax=%d",max);
getch();
}

Page 28
P60 Write a C program to find out perfect number.

#include<stdio.h>
#include<conio.h>
void main()
{
int n,s,r,i;

clrscr();
printf("\n Enter a number");
scanf("%d",&n);
s=0;
for(i=1;i<n;i++)
{
r=n%i;
if(r==0)
s=s+i;
}
if(s==n)
printf("\nNumber is perfect number");
else
printf("\nNumber is not perfect number");
getch();
}

P61 Write a C program to find addition of two number using function.

#include<stdio.h>
#include<conio.h>
int add(int x,int y);
void main()
{
int a,b,result;
clrscr();
printf("\n Enter two number");
scanf("%d%d",&a,&b);
result=add(a,b);
printf("\nADDITION=%d",result);
getch();
}

int add(int x, int y)


{
int t;
t=x+y;
return(t);
}

P62 Write a C program to find area of triangle using function.

Page 29
#include<stdio.h>
#include<conio.h>
#include<math.h>
float area(float x,float y,float z);
void main()
{
float a,b,c,result;
clrscr();
printf("\n Enter three sides of triangle");
scanf("%f%f%f",&a,&b,&c);
result=area(a,b,c);
printf("\nAREA OF TRIANGLE=%f",result);
getch();
}

float area(float x, float y,float z)


{
float s,t;
s=(x+y+z)/2;
t=sqrt(s*(s-x)*(s-y)*(s-z));
return(t);
}

P63 Write a C program to find sum of digit of integer number using function.

#include<stdio.h>
#include<conio.h>
int sumofdigit(int x);
void main()
{
int n,result;
clrscr();
printf("\n Enter a number");
scanf("%d",&n);
result=sumofdigit(n);
printf("\n SUM OF DIGIT=%d",result);
getch();
}

int sumofdigit(int x)
{
int s,r;
s=0;
while(x>0)
{
r=x%10;
s=s+r;
x=x/10;
}
return(s);
Page 30
}

P64 Write a C program to calculate nCr using function.

#include<stdio.h>
#include<conio.h>
long int fact(int x);
void main()
{
int n,r;
int result;
clrscr();
printf("\n Enter the value of n and r");
scanf("%d%d",&n,&r);
result=fact(n)/(fact(r)*fact(n-r));
printf("\n COMBINATION=%d",result);
getch();
}

long int fact(int x)


{
int i;
long int factorial;
factorial=1;
for(i=1;i<=x;i++)
factorial=factorial*i;
return(factorial);
}

P65 Write a C program to find the factorial of number using recursion.

#include<stdio.h>
#include<conio.h>
long int fact(long int n);
void main()
{
long int n;
long int result;
clrscr();
printf("\n Enter a number");
scanf("%ld",&n);
result=fact(n);
printf("\nFACTORIAL=%ld",result);
getch();
}

long int fact(long int n)


{
if(n==0)
return(1);
Page 31
else if(n==1)
return(1);
else
return(n*fact(n-1));
}

P66 Write a C program to compute xn using recursion.

#include<stdio.h>
#include<conio.h>
int power(int x,int n);
void main()
{
int n,x;
int result;
clrscr();
printf("\n Enter base and exponent");
scanf("%d%d",&x,&n);
result=power(x,n);
printf("\nRESULT=%d",result);
getch();
}

int power(int x,int n)


{
if(n==0)
return(1);
else
return(x*power(x,n-1));
}

P67 Write a C program to print the Fibonacci series up to n term using recursion.

#include<stdio.h>
#include<conio.h>
int fib(int n);
void main()
{
int n,i;
clrscr();
printf("\n How many terms of fibonacci series");
scanf("%d",&n);
for(i=1;i<=n;i++)
printf("\t%d",fib(i));
getch();
}

int fib(int n)
Page 32
{
if(n==1)
return(0);
else if(n==2)
return(1);
else
return(fib(n-1)+fib(n-2));
}

P68 Write a C program to find Greatest Common divisor (GCD) using recursion.

#include<stdio.h>
#include<conio.h>
int gcd(int a,int b);
void main()
{
int a,b;
int result;
clrscr();
printf("\n Enter a two number");
scanf("%d%d",&a,&b);
result=gcd(a,b);
printf("\nGreatest comman divisor=%d",result);
getch();
}

int gcd(int a,int b)


{
int r;
r=a%b;
if(r==0)
return(b);
else
return(gcd(b,r));
}

P69 Write a C program to print the content of the array in reverse order.

#include<stdio.h>
#include<conio.h>
void main()
{
int a[100],n,i;
clrscr();
printf("\n How many data");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter data value");
Page 33
scanf("%d",&a[i]);
}
printf("\nSerial order content of array");
for(i=0;i<n;i++)
printf("\t%d",a[i]);
printf("\nReverse order content of array");
for(i=n-1;i>=0;i--)
printf("\t%d",a[i]);
getch();
}

P70 Write a C program to find print the marks that are greater than average marks.

#include<stdio.h>
#include<conio.h>
void main()
{
int a[100],n,i,s=0;
float avg;
clrscr();
printf("\n How many marks");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter marks value");
scanf("%d",&a[i]);
s=s+a[i];
}
avg=(float)s/n;
printf("\n Average marks=%f",avg);
printf("\nMarks greater than average marks are");
for(i=0;i<n;i++)
{
if(a[i]>avg)
printf("\t%d",a[i]);
}
getch();
}

P71 Write a C program for sequential search.

#include<stdio.h>
#include<conio.h>
void main()
{
int a[100],n,i,key,found=0;
clrscr();
printf("\n How many data");
Page 34
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter data value");
scanf("%d",&a[i]);
}
printf("\nEnter the key");
scanf("%d",&key);
printf("\nContent of array");
for(i=0;i<n;i++)
printf("\t%d",a[i]);
for(i=0;i<n;i++)
{
if(a[i]==key)
{
printf("\nkey is present at index %d",i);
found=1;
}
}
if(found==0)
printf("\nkey is not present in the array");
getch();
}

P72 Write a C program for bubble sort.

#include<stdio.h>
#include<conio.h>
void main()
{
int a[100],n,i,j,temp;
clrscr();
printf("\n How many data");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter data value");
scanf("%d",&a[i]);
}
printf("\n Unsorted array is");
for(i=0;i<n;i++)
printf("\t%d",a[i]);
for(i=0;i<n-1;i++)
{
for(j=0;j<n-1-i;j++)
{
if(a[j]>a[j+1])
{
temp=a[j];
Page 35
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
printf("\n Sorted array is");
for(i=0;i<n;i++)
printf("\t%d",a[i]);
getch();
}

P73 Write a C program to find the sum of all the elements of the matrix.

#include<stdio.h>
#include<conio.h>
void main()
{
int mat[10][10],i,j;
int row,col,s=0;
clrscr();
printf("\n Enter row and col of matrix");
scanf("%d%d",&row,&col);

for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
printf("\nEnter matrix data");
scanf("%d",&mat[i][j]);
}
}
printf("\n matrix is as follows\n");
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
printf("\t%d",mat[i][j]);
printf("\n");
}
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
s=s+mat[i][j];
}
printf("\nSum of all element of matrix=%d",s);
getch();
}

P74 Write a C program to find the sum of diagonal element of matrix.

Page 36
#include<stdio.h>
#include<conio.h>
void main()
{
int mat[10][10],i,j;
int row,col,s=0;
clrscr();
printf("\n Enter row and col of matrix");
scanf("%d%d",&row,&col);

for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
printf("\nEnter matrix data");
scanf("%d",&mat[i][j]);
}
}
printf("\n matrix is as follows\n");
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
printf("\t%d",mat[i][j]);
printf("\n");
}
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
if(i==j)
s=s+mat[i][j];
}
}
printf("\nSum of diagonal element of matrix=%d",s);
getch();
}

P75 Write a C program to find the transpose of a matrix.

#include<stdio.h>
#include<conio.h>
void main()
{
int mat[10][10],mat_res[10][10],i,j;
int row,col;
clrscr();
printf("\n Enter row and col of matrix");
scanf("%d%d",&row,&col);

Page 37
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
printf("\nEnter matrix data");
scanf("%d",&mat[i][j]);
}
}
printf("\n matrix is as follows\n");
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
printf("\t%d",mat[i][j]);
printf("\n");
}
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
mat_res[j][i]=mat[i][j];
}
printf("\n Tranpose matrix is as follows\n");
for(i=0;i<col;i++)
{
for(j=0;j<row;j++)
printf("\t%d",mat_res[i][j]);
printf("\n");
}
getch();
}

P76 Write a C program to find the addition or subtraction two of matrix.

#include<stdio.h>
#include<conio.h>
void main()
{
int mat1[10][10],mat2[10][10],mat_res[10][10],i,j;
int row1,col1,row2,col2;
clrscr();
printf("\n Enter row and col of matrix 1");
scanf("%d%d",&row1,&col1);
printf("\n Enter row and col of matrix 2");
scanf("%d%d",&row2,&col2);

for(i=0;i<row1;i++)
{
for(j=0;j<col1;j++)
{
printf("\nEnter matrix 1 data");
Page 38
scanf("%d",&mat1[i][j]);
}
}

for(i=0;i<row2;i++)
{
for(j=0;j<col2;j++)
{
printf("\nEnter matrix 2 data");
scanf("%d",&mat2[i][j]);
}
}

printf("\n matrix 1 is as follows\n");


for(i=0;i<row1;i++)
{
for(j=0;j<col1;j++)
printf("\t%d",mat1[i][j]);
printf("\n");
}

printf("\n matrix 2 is as follows\n");


for(i=0;i<row2;i++)
{
for(j=0;j<col2;j++)
printf("\t%d",mat2[i][j]);
printf("\n");
}

if(row1==row2&&col1==col2)
{
printf("\nmatrix addition is possible");
for(i=0;i<row1;i++)
{
for(j=0;j<col1;j++)
mat_res[i][j]=mat1[i][j]+mat2[i][j];
}
printf("\n Added matrix is as follows\n");
for(i=0;i<row1;i++)
{
for(j=0;j<col1;j++)
printf("\t%d",mat_res[i][j]);
printf("\n");
}
}
else
printf("\nMatrix addition is not possible");
getch();
}

Page 39
P77 Write a C program to find the multiplication of two matrixes.

#include<stdio.h>
#include<conio.h>
void main()
{
int mat1[10][10],mat2[10][10],mat_res[10][10],i,j,k;
int row1,col1,row2,col2;
clrscr();
printf("\n Enter row and col of matrix 1");
scanf("%d%d",&row1,&col1);
printf("\n Enter row and col of matrix 2");
scanf("%d%d",&row2,&col2);

for(i=0;i<row1;i++)
{
for(j=0;j<col1;j++)
{
printf("\nEnter matrix 1 data");
scanf("%d",&mat1[i][j]);
}
}

for(i=0;i<row2;i++)
{
for(j=0;j<col2;j++)
{
printf("\nEnter matrix 2 data");
scanf("%d",&mat2[i][j]);
}
}

printf("\n matrix 1 is as follows\n");


for(i=0;i<row1;i++)
{
for(j=0;j<col1;j++)
printf("\t%d",mat1[i][j]);
printf("\n");
}

printf("\n matrix 2 is as follows\n");


for(i=0;i<row2;i++)
{
for(j=0;j<col2;j++)
printf("\t%d",mat2[i][j]);
printf("\n");
}

if(col1==row2)
Page 40
{
printf("\nmatrix multiplication is possible");
for(i=0;i<row1;i++)
{
for(j=0;j<col2;j++)
{
mat_res[i][j]=0;
for(k=0;k<col1;k++)
{
mat_res[i][j]=mat_res[i][j]+mat1[i][k]*mat2[k][j];
}
}
}
printf("\n Multiplide matrix is as follows\n");
for(i=0;i<row1;i++)
{
for(j=0;j<col2;j++)
printf("\t%d",mat_res[i][j]);
printf("\n");
}
}
else
printf("\nMatrix multiplication is not possible");
getch();
}

P78 Write a C program to find the norm of matrix.

#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int mat[10][10],i,j;
int row,col,s=0;
clrscr();
printf("\n Enter row and col of matrix");
scanf("%d%d",&row,&col);

for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
printf("\nEnter matrix data");
scanf("%d",&mat[i][j]);
}
}
printf("\n matrix is as follows\n");
for(i=0;i<row;i++)
Page 41
{
for(j=0;j<col;j++)
printf("\t%d",mat[i][j]);
printf("\n");
}
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
s=s+mat[i][j]*mat[i][j];
}
printf("\nNorm of matrix=%f",sqrt(s));
getch();
}

P79 Write a C program to check that a matrix symmetric or not.

#include<stdio.h>
#include<conio.h>
void main()
{
int mat[10][10],i,j;
int row,col,flag=0;
clrscr();
printf("\n Enter row and col of matrix");
scanf("%d%d",&row,&col);

for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
printf("\nEnter matrix data");
scanf("%d",&mat[i][j]);
}
}
printf("\n matrix is as follows\n");
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
printf("\t%d",mat[i][j]);
printf("\n");
}
for(i=0;i<row&&flag==0;i++)
{
for(j=0;j<col&&flag==0;j++)
{
if(mat[i][j]==mat[j][i])
continue;
else
flag=1;
}
Page 42
}

if(flag==0)
printf("\nMatrix is symmetric");
else
printf("\nMatrix is not symmetric");
getch();
}

P80 Write a C program to for the addition of two number using pointers.

#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c;
int *p,*q;
clrscr();
printf("\n Enter two value");
scanf("%d%d",&a,&b);
p=&a;
q=&b;
c=*p+*q;
printf("\n SUM=%d",c);
getch();
}

P81 Write a C program to for finding greater number between two number using pointers.

#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c;
int *p,*q;
clrscr();
printf("\n Enter value of a and b");
scanf("%d%d",&a,&b);
p=&a;
q=&b;
if(*p>*q)
printf("\n a is greater than b");
else
printf("\n b is greater than a");
getch();
}

P82 Write a C program to swap two variables using function.

#include<stdio.h>
Page 43
#include<conio.h>
void swap(int *x,int *y);
void main()
{
int a,b;
clrscr();
printf("\n Enter two number");
scanf("%d%d",&a,&b);
printf("\nBefore swapping a=%d b=%d",a,b);
swap(&a,&b);
printf("\nAfter swapping a=%d b=%d",a,b);
getch();
}

void swap(int *x, int *y)


{
int t;
t=*x;
*x=*y;
*y=t;
}

P83 Write a C program to swap two variables without using third variable using function.

#include<stdio.h>
#include<conio.h>
void swap(int *x,int *y);
void main()
{
int a,b;
clrscr();
printf("\n Enter two number");
scanf("%d%d",&a,&b);
printf("\nBefore swapping a=%d b=%d",a,b);
swap(&a,&b);
printf("\nAfter swapping a=%d b=%d",a,b);
getch();
}

void swap(int *x, int *y)


{

*x=*x+*y;
*y=*x-*y;
*x=*x-*y;
}

P84 Write a C program to increase the value of two variables by 10 using function.
Page 44
#include<stdio.h>
#include<conio.h>
void add10(int *x,int *y);
void main()
{
int a,b;
clrscr();
printf("\n Enter two number");
scanf("%d%d",&a,&b);
printf("\nBefore adding a=%d b=%d",a,b);
add10(&a,&b);
printf("\nAfter adding a=%d b=%d",a,b);
getch();
}

void add10(int *x, int *y)


{

*x=*x+10;
*y=*y+10;
}

P85 Write a C program for storing the grade, basic and salary n employee.

#include<stdio.h>
#include<conio.h>

struct employee
{
char grade;
float basic;
int allownce;
};

void main()
{
struct employee a[100];
int n,i;
clrscr();
printf("\n How many employee");
scanf("%d",&n);
printf("\n Enter the employee data");
for(i=0;i<n;i++)
{
printf("\nEnter grade");
scanf("%c",&a[i].grade);
printf("\nEnter basic");
scanf("%f",&a[i].basic);
printf("\nEnter grade");
Page 45
scanf("%d",&a[i].allownce);
}

printf("\nEmployee data are as follows\n");


for(i=0;i<n;i++)
{
printf("\nGrade=%c",a[i].grade);
printf("\nBasic=%f",a[i].basic);
printf("\nAllownce=%d",a[i].allownce);
printf("\n\n\n");
}
getch();
}

P86 Declare a structure of student with detail roll no, student name and total marks. Write a
program in c to read detail of n students and prints the list of students who have secured
75% marks and above.

#include<stdio.h>
#include<conio.h>

struct student
{
int rollno;
char sname[30];
int total;
};

void main()
{
struct student a[100];
int n,i;
clrscr();
printf("\n How many student");
scanf("%d",&n);
printf("\n Enter the student data");
for(i=0;i<n;i++)
{
printf("\nEnter rollno");
scanf("%d",&a[i].rollno);
printf("\nEnter student name");
scanf("%s",a[i].sname);
printf("\nEnter total marks");
scanf("%d",&a[i].total);
}

printf("\nStudent having 75 or above marks are as follows\n");


for(i=0;i<n;i++)
{
if(a[i].total>=75)
Page 46
{
printf("\nRollno=%d",a[i].rollno);
printf("\nStudent name=%s",a[i].sname);
printf("\nTotal marks=%d",a[i].total);
printf("\n\n\n");
}
}
getch();
}

P87 Define a structure called cricket that will describe player name, team name and batting
average. Using cricket, declare an array player with 50 elements and write a program to
read the information about all the 50 players and print team wise list containing names of
players with their batting average.

#include<stdio.h>
#include<conio.h>

struct player
{
char pname[30];
char tname[30];
int avg;
};

void main()
{
struct player a[100];
int n,i;
clrscr();
printf("\n How many player");
scanf("%d",&n);
printf("\n Enter the player data");
for(i=0;i<n;i++)
{
printf("\nEnter player name");
scanf("%s",a[i].pname);
printf("\nEnter team name");
scanf("%s",a[i].tname);
printf("\nEnter batting average");
scanf("%d",&a[i].avg);
}

printf("\nTeam wise player detail are as follows\n");


for(i=0;i<n;i++)
{
printf("\nPlayer name=%s",a[i].pname);
printf("\nTeam name=%s",a[i].tname);
printf("\nBatting average=%d",a[i].avg);
Page 47
printf("\n\n\n");
}
getch();
}

P88 Write a C program to count the occurrence of particular character in a given string.

#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char st[100],ch;
int l,i,c;
clrscr();
printf("\nEnter a string ");
gets(st);
printf("Enter the character to be counted");
scanf("%c",&ch);
l=strlen(st);
c=0;
for(i=0;i<l;i++)
{
if(st[i]==ch)
c=c+1;
}
printf("Character occures %d",c);
getch();
}

P89 Write a C program to count number of vowels present in a sentence.

#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char st[100];
int l,i,c;
clrscr();
printf("\nEnter a string ");
gets(st);
c=0;
l=strlen(st);
for(i=0;i<l;i++)
{
switch(st[i])
{
case 'a':
case 'A':
Page 48
case 'e':
case 'E':
case 'i':
case 'I':
case 'o':
case 'O':
case 'u':
case 'U':
c=c+1;
break;
}
}
printf(" Vowel is present=%d",c);
getch();
}

P90 Write a C program to test whether a given string is a palindrome.

#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char st1[100],st2[100];
int l,i,j;
clrscr();
printf("\nEnter a string ");
gets(st1);
i=strlen(st1)-1;
j=0;
while(i>=0)
{
st2[j]=st1[i];
i--;
j++;
}
st2[j]='\0';
if(strcmp(st1,st2)==0)
printf("String is palindrome");
else
printf("String is not palindrome");
getch();
}

P91 Write a C program to concatenate two strings.

#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
Page 49
{
char st1[100],st2[100],st[100];
int l,i,j;
clrscr();
printf("\nEnter string 1 ");
gets(st1);
printf("\n Enter string 2 ");
gets(st2);
i=0;
j=0;
while(st1[i]!='\0')
{
st[j]=st1[i];
i++;
j++;
}
st[j]=' ';
j++;
i=0;
while(st2[i]!='\0')
{
st[j]=st2[i];
i++;
j++;
}
st2[j]='\0';
printf("Resultant string %s",st);
getch();
}

P92 Write a C program to compare two strings which are given as input through key board
and print alphabetically greater string.

#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char st1[100],st2[100];
clrscr();
printf("\nEnter string 1 ");
gets(st1);
printf("\n Enter string 2 ");
gets(st2);
if(strcmp(st1,st2) > 0)
printf("\n String 1 is greater");
else
printf("\nString 2 is greater");
getch();
}
Page 50
P93 Write a C program to convert a line in the lower case text to upper case text.

#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char st[100];
int l,i;
clrscr();
printf("\nEnter a string ");
gets(st);
l=strlen(st);
for(i=0;i<l;i++)
{
if(st[i]>='a'&&st[i]<='z')
st[i]=st[i]-32;
}
printf("Converted upper case srintg is %s",st);
getch();
}

P94 Write a C program to read a string and rewrite it in the alphabetical order.(STRING will
be written as GINRST)

#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char st[100],temp;
int l,i,j;
clrscr();
printf("\nEnter a string ");
gets(st);
l=strlen(st);
for(i=0;i<l-1;i++)
{
for(j=0;j<l-1;j++)
{
if(st[j]>st[j+1])
{
temp=st[j+1];
st[j+1]=st[j];
st[j]=temp;
}
}
}
Page 51
printf("Alphabeticaly Sorted string=%s",st);
getch();
}

P95 Write a C program to count the number of line, words and character in a given text.

#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char st[100];
int l,i,lns,wds,chs;
clrscr();
printf("\nEnter a string ");
gets(st);
lns=0;
wds=0;
chs=0;
l=strlen(st);
for(i=0;i<l;i++)
{
switch(st[i])
{
case ',':
case '!':
case '\t':
case ' ':
{
wds++;
chs++;
break;
}
case '?':
case '.':
{
wds++;
lns++;
break;
}
default:
chs++;
break;
}
}
printf("\nNumber of character=%d",chs);
printf("\nNumber of words=%d",wds);
printf("\nNumber of lines=%d",lns);
getch();
}
Page 52
P96 Write a C program to create a text file and write some text and close.

#include<Stdio.h>
#include<conio.h>
void main()
{
FILE *fp;
char ch;
clrscr();
fp=fopen("sample.txt","w");
printf("\n Type the txet and press enter key at the end");
while((ch=getchar())!='\n')
putc(ch,fp);
fclose(fp);
}

P97 Write a C program to read the text of a file and also count the number of vowels present
in the file.

#include<Stdio.h>
#include<conio.h>
void main()
{
FILE *fp;
char ch;
int c=0;
clrscr();
fp=fopen("sample.txt","r");
printf("\nThe content of the text file is \n");
while(!feof(fp))
{
ch=getc(fp);
printf("%c",ch);
switch(ch)
{
case 'a':
case 'A':
case 'e':
case 'E':
case 'i':
case 'I':
case 'o':
case 'O':
case 'u':
case 'U':
c++;
break;
}
}
printf("\n number of vovels present=%d",c);
Page 53
fclose(fp);
getch();
}

P98 Write a C program to copy the content one file to another file.

#include<Stdio.h>
#include<conio.h>
void main()
{
FILE *source_fp,*target_fp;
char ch,source_name[20],target_name[20];
clrscr();
printf("\nEnter the source file name");
gets(source_name);
printf("\n Enter the target file name");
gets(target_name);
source_fp=fopen(source_name,"r");
target_fp=fopen(target_name,"w+");
if(source_fp==NULL)
{
printf("\n No content/source file");
exit(0);
}

while(!feof(source_fp))
{
ch=getc(source_fp);
putc(ch,target_fp);
}
printf("\n The content of the target file are");
rewind(target_fp);
while(!feof(target_fp))
{
ch=getc(target_fp);
printf("%c",ch);
}
fclose(source_fp);
fclose(target_fp);
getch();
}

P99 A file called “STUDENT.DAT” contains information such as student roll number, name
and total marks. Write a C program to create a file to store detail of n student on disk
and also display the detail.

#include<Stdio.h>
#include<conio.h>
#include<ctype.h>
Page 54
void main()
{
FILE *fp;
int rollno,total,i,n;
char sname[20];
clrscr();
fp=fopen("STUDENT.DAT","w");
printf("\nHow many student");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\n Enter student rollno,name,total");
scanf("%d %s %d",&rollno,sname,&total);
fprintf(fp,"%d%s%d\n",rollno,sname,total);
}
fclose(fp);
fp=fopen("STUDENT.DAT","r");
while(!feof(fp))
{
fscanf(fp,"%d%s%d\n",&rollno,sname,&total);
printf("\n %d %s %d",rollno,sname,total);
}
getch();
fclose(fp);
}

P100 Write a C program to take n integers from a file and write square of those integer into
another.

#include<Stdio.h>
#include<conio.h>
#include<ctype.h>
void main()
{
FILE *fp1,*fp2;
int n,a,b,c,i;
clrscr();
printf("\n How many integer data");
scanf("%d",&n);
fp1=fopen("data1","w");
for(i=0;i<n;i++)
{
printf("\n Enter integer data");
scanf("%d",&a);
putw(a,fp1);
}
fclose(fp1);
fp1=fopen("data1","r");
fp2=fopen("data2","w");
Page 55
while(!feof(fp1))
{
b=getw(fp1);
putw(b*b,fp2);
}
fclose(fp2);
fp2=fopen("data2","r");
while(!feof(fp2))
{
c=getw(fp2);
printf("\nOutput=%d",c);
}
fclose(fp1);
fclose(fp2);
getch();
}

Page 56

You might also like