You are on page 1of 126

170080111047

SET 1: SIMPLE ‘C’ PROGRAMS.

1
170080111047

01: WRITE A PROGRAM TO PRINT “HELLO WORLD”.

ALGORITHM:
Step 1: start
Step 2: print Hello world
Step 3: stop

PROGRAM:
#include<stdio.h>
void main()
{
printf(“Hello world”);
}

OUTPUT:
Hello world

2
170080111047

02: WRITE A PROGRAM TO READ ONE NUMBER AND DISPLAY IT.

ALGORITHM:
Step 1: start
Step 2: read n
Step 3: enter a number n
Step 4: print n
Step 5: stop

PROGRAM:
#include<stdio.h>
void main()
{
int n;
printf(“Enter a number”);
scanf(“%d”,&n);
printf(“%d”,n);
}

OUTPUT:
Enter a number :5
5

3
170080111047

03: WRITE A PROGRAM THAT WILL OBTAIN THE LENGTH AND WIDTH
OF A RECTANGLE FORM THE USER AND CALCULATE ITS AREA,
PERIMETER AND DIAGONAL.

ALGORITHM:
Step 1: start
Step 2: read l,b,area,perimeter,diagonal
Step 3: enter length
Step 4: enter breadth
Step 5: area=l*b
Step 6: perimeter=2(l+b)
Step 7: diagonal=sqrt((l*l)+(b*b))
Step 8: print area
Step 9: print perimeter
Step 10: print diagonal
Step 11: stop

PROGRAM:
#include<stdio.h>
void main()
{
int l, b, area, perimeter, diagonal;
printf(“enter length:”);
scanf(“%d”,&l);
printf(“enter breadth:”);
scanf(“%d”,&b);
area=l*b;
perimeter=2(l+b);
diagonal=sqrt((l*l)+(b*b));
printf(“area=%d”,area);
printf(“perimeter=%d”,perimeter);
printf(“diagonal=%d”,diagonal);
}

OUTPUT:
Enter one side:4
Enter second side:3
Area=12
Perimeter=14
Diagonal=5

4
170080111047

04: WRITE A PROGRAM TO CONVERT KILOMETER TO METER,


CENTIMETER AND INCHES.

ALGORITHM:
Step 1: start
Step 2: Enter the distance in km
Step 3: m=1000*km
Step 4: cm=100*m
Step 5: inches=39370*km
Step 6: print distance in m,cm and inches
Step 7: stop

PROGRAM:
#include<stdio.h>
void main()
{
float km,m,cm,inches;
printf(“Enter the distance in km:”);
scanf(“%f”,&km);
m=km*1000;
cm=m*100;
inches=km*39370;
printf(“distance in m=%f”,m);
printf(“distance in cm=%f”,cm);
printf(“distance in inches=%f”,inches);
}

OUTPUT:
Enter distance in km=1
D=1000m
D=100000cm
D=39370inches

5
170080111047

05: WRITE A PROGRAM TO COMPUTE AREA OF TRIANGLE.

ALGORITHM:
Step 1: Start
Step 2: float a,b,c,area,s;
Step 3: Enter a,b,c
Step 4: s=(a+b+c)/2;
Step 5: area=sqrt(s*(s-a)*(s-b)*(s-c));
Step 6: print area
Step 7: End

PROGRAM:
#include<stdio.h>
#include<math.h>
void main()
{
float a,b,c,area,s;
printf("Enter the values of sides a,b,c:\n");
scanf("%f%f%f",&a,&b,&c);
s=(a+b+c)/2;
area=sqrt(s*(s-a)*(s-b)*(s-c));
printf("The area of the triangle is %f\n",area);
}

OUTPUT:
Enter the values of sides a,b,c:
7 5 4
The area of the triangle is 9.797959

6
170080111047

06: WRITE A PROGRAM TO CONVERT TEMPERATURE IN CELSIUS TO


FAHRENHEIT.

ALGORITHM:
Step 1: Start
Step 2: float F,C;
Step 3: C=200;
Step 4: F=(((9*C)/5)+32);
Step 5: print temperature in fahrenheit
Step 6: End

PROGRAM:
#include<stdio.h>
void main()
{
float F,C;
C=200;
F=(((9*C)/5)+32);
printf("Celsius = %f to Fahrenheit = %f\n",C,F);
}

OUTPUT:
Celsius = 200.000000 to Fahrenheit = 392.000000

7
170080111047

07: WRITE A PROGRAM TO CONVERT TEMPERATURE IN FAHRENHEIT


TO CELSIUS.

ALGORITHM:
Step 1: Start
Step 2: float F,C;
Step 3: F=300;
Step 4: C=((F-32)*5)/9;
Step 5: print temperature in celsius
Step 6: End

PROGRAM:
#include<stdio.h>
void main()
{
float F,C;
F=300;
C=((F-32)*5)/9;
printf("Fahrenheit = %f to Celsius = %f\n",F,C);
}

OUTPUT:
Fahrenheit = 300.000000 to Celsius = 148.888885

8
170080111047

08: WRITE A PROGRAM TO FIND SUM OF DIGITS OF 3 DIGIT


INTEGER CONSTANT.

ALGORITHM:
Step 1: start
Step 2: read a,b,c
Step 3: Enter three integers
Step 4: sum=a+b+c
Step 5: print sum
Step 6: stop

PROGRAM:
#include<stdio.h>
void main()
{
int a,b,c;
printf(“Enter three numbers:”);
scanf(“%d%d%d”.&a,&b,&c);
sum=a+b+c;
printf(“addition=%d”,sum);
}

OUTPUT:
Enter three numbers:
1,2,3
Sum=6

9
170080111047

09: WRITE A PROGRAM TO REVERSE A DIGIT.

ALGORITHM:
Step 1: start
Step 2: read x,y,z
Step 3: print x,y,z before reversing
Step 4: temp=x
Step 5: x=y
Step 6: y=z
Step 7: z=temp
Step 8: print x,y,z after reversing
Step 9: stop
PROGRAM:
#include<stdio.h>
void main()
{
int x,y,z,temp;
printf(“enter the values of x,y,z ”);
scanf(“%d %d %d”,&x,&y,&z);
printf(“before reversing x=%d,y=%d,z=%d \”,x,y,z);
temp=x;
x=y;
y=z;
z=temp;
printf(“after reversing x=%d,y=%d,z=%d \”,x,y,z);
}

OUTPUT:
Enter values of x,y,z
123
before reversing x=1,y=2,z=3
after reversing x=3,y=2,z=1

10
170080111047

10: WRITE A PROGRAM TO INTERCHANGE THE VALUES OF TWO


VARIABLE.

ALGORITHM:
step 1: start
step 2: enter the values of x & y
step 3: before interchange print x and y
step 4: temp=x;
step 5: x=y;
step 6: y=temp;
step 7: after interchange print x and y
step 8: end

PROGRAM:
#include<stdio.h>
void main()
{
printf(“enter the values of x&y \n”);
scanf(“%d %d”,&x,&y);
printf(“before interchange \n x=%d \n x=%d \n y=%d\n”,x,y);
temp=x;
x=y;
y=temp;
printf(“after interchange \n x=%d \n y=%d\n”,x,y);
}

OUTPUT:
Enter the value of x &y
1
2
Before interchange
x=1
y=2
After interchange
x=2
y=1

11
170080111047

11: WRITE A PROGRAM TO ASSIGN VALUE OF ONE VARIABLE USING


POST AND PRE INCREMENT AND PRINT THE RESULT.

ALGORITHM:
Step 1: start
Step 2: enter value of x:
Step 3: y=++x;
Step 4: z=x++;
Step 5: print y
Step 6: print z
Step 7: end

PROGRAM:
#include<stdio.h>
void main()
{
int x,y,z;
printf(“enter the values of x: \n”);
scanf(“%d”,&x);
y=++x;
z=x++;
printf(“the values assigned using preincrement is %d \n”,y);
printf(“the values assigned using postincrement is%d \n”,z);
}

OUTPUT:
enter value of x:
1
The value assigned using preincrement is 2
The value assigned using postincrement is 2

12
170080111047

12: WRITE A PROGRAM TO READ THE PRICE OF ITEM IN DECIMAL


FORM. SEPARATE RUPEES AND PAISA FROM THE GIVEN VALUE.

ALGORITHM:
Step 1: start
Step 2: let p, r, total;
Step 3: enter amount
Step 4: p=(float)(total-r)*100;
Step 5: print r
Step 6: print p
Step 7: end

PROGRAM:
#include<stdio.h>
void main()
{
float total;
int p,r;
printf(“enter amount : “);
scanf(“%f”,&total);
r=total;
p=(float)(total-r)*100;
printf(“rupee = %d\n”,r);
printf(“paisa= %d\n”,p);
}

OUTPUT:
Enter amount 50.30
Rupees=50
Paisa=30

13
170080111047

13: WRITE A PROGRAM TO CONVERT DAYS INTO MONTHS AND DAYS.

ALGORITHM:
Step 1: Start
Step 2: Read number of days
Step 3: Enter number of days
Step 4: Calculate number of days
Step 5: Stop

PROGRAM:
#include<stdio.h>
void main()
{
printf (“Enter days\n”);
Scanf(“%d”,&days);
Months=days/30;
Days=days%30;
Printf(“Months=%d Days =%d,months,days);
}

OUTPUT:
Enter days
265
Months=8 Days=25

14
170080111047

14. WRITE A PROGRAM FOR SIMPLE CALCULATOR.

ALGORITHM:
Step 1: start
Step 2: read integers a,b
Step 3: enter two numbers
Step 4: sum of two numbers
Step 5: difference of two numbers
Step 6: multiplication of two numbers
Step 7: division of two numbers
Step 8: modulus of two numbers
Step 9: stop

PROGRAM:
#include<stdio.h>
void main()
{
int a,b,sum,sub,mul,mod;
float div;
printf("enter two numbers \n");
scanf("%d %d",&a,&b);
sum=a+b;
sub=a-b;
mul=a*b;
div=a/b;
mod=a%b;
printf("sum of two numbers is %d \n",sum);
printf("difference is %d \n",sub);
printf("multiplication is %d \n",mul);
printf("division is %f \n",div);
printf("modulus is %d \n", mod);
}

OUTPUT:
Enter numbers:3 6
sum of two numbers is 9
difference is 3
multiplication is 18
division is 2
modulus is 0

15
170080111047

SET 2: ‘C’ PROGRAMS USING IF...ELSE, IF…ELSE LADDER,


SWITCH……

16
170080111047

01: WRITE A PROGRAM TO READ MARKS FROM KEYBOARD AND


DISPLAY EQUIVALENT GRADE.

ALGORITHM:
Step 1: start
Step 2: read marks
Step 3: if mks>=0&&mks<=34 else goto step 5
Step 4: print fail
Step 5: if mks>=35&&mks<=59 else goto step 7
Step 6: print second class
Step 7: if mks>=60&&mks<=79 else goto step 9
Step 8: print first class
Step 9: print distinction

PROGRAM:
#include<stdio.h>
void main()
{
int mks;
printf("enter your marks:");
scanf("%d",&mks);
if(mks>=0&&mks<=34)
{printf("Fail");}
else if(mks>=35&&mks<=59)
{printf("second class");}
else if(mks>=60&&mks<=79)
{printf("first class");}
else
{printf("distinction");}
}

OUTPUT:
Enter marks:65
First class

17
170080111047

02: WRITE A PROGRAM FOR THE SOLUTION OF QUADRATIC


EQUATION.

ALGORITHM:
Step 1: start
Step 2: read integers a,b,c,r1,r2,r3
Step 3: enter the value of a,b,c
Step 4: r=b**-4*a*c
Step 5: if(r3==0)
Step 6: then printr1=r2=2*a otherwise goto step 7
Step 7: if(r3<0)
Step 8: then print imaginary roots otherwise goto step 9
Step 9: if r1=(-b+sqrt(r3))/2*a and r2=(-b-sqrt(r3))/(2*a)
Step 10: print i
Step 11: stop

PROGRAM:
#include<stdio.h>
#include<math.h>
void main()
{
float a,b,c,r1,r2,r3;
printf("Enter a:");
scanf("%f",&a);
printf("Enter b:");
scanf("%f",&b);
printf("Enter c:");
scanf("%f",&c);
r3=b*b-4*a*c;
if(r3==0)
{
printf("r1=r2=%f",-b/(2*a));
}
else if(r3<0)
{
printf("Imaginart Roots\n");
}
else
{
r1=(-b+sqrt(r3))/(2*a);

18
170080111047

r2=(-b-sqrt(r3))/(2*a);
printf("r1= %f\n r2=%f\n",r1,r2);
}
}

OUTPUT:
Enter a:2
Enter b:6
Enter c:2

19
170080111047

03: WRITE A PROGRAM TO MAKE SIMPLE CALCULATOR.

ALGORITHM:
Step 1: start
Step 2: read a,b
Step 3: sum=a+b print sum
Step 4: diff=a-b print diff
Step 5: mul=a*b print product
Step 6: div=a/b print div

PROGRAM:
#include<stdio.h>
void main()
{
char op;
float num1,num2,res;
printf("Enter first number:");
scanf("%f",&num1);
printf("Enter second number:");
scanf("%f",&num2);
printf("Operation (+,-,*,/) :");
scanf(" %c",&op);
if(op=='+')res=num1+num2;
else if(op=='-')res=num1-num2;
else if(op=='*')res=num1*num2;
else if(op=='/')res=num1/num2;
else {printf("Invalid Choice"); return;}
printf("Result: %f",res);
}

OUTPUT:
Enter first number:2
Enter second number:5
Operation (+,-,*,/) :
Result: 10

20
170080111047

04: WRITE A PROGRAM TO FIND MAXIMUM AND MINIMUM USING


TERNARY OPERATOR.

ALGORITHM:
Step 1: start
Step 2: read a,b,c
Step 3: min=a<b?((a<c)?a:c):((b<c)?b:c)
Step 4: max=a>b?((a>c)?a:c):((b>c)?b:c)
Step 5: print min,max
Step 6: stop

PROGRAM:
#include<stdio.h>
void main()
{
int a,b,c,min,max;
printf("\n enter the value of A:");
scanf("%d",&a);
printf("\n enter the value of B:");
scanf("%d",&b);
printf("\n enter the value of C:");
scanf("%d",&c);
min=a<b?((a<c)?a:c):((b<c)?b:c);
max=a>b?((a>c)?a:c):((b>c)?b:c);
printf("Minimum=%d\nMaximum=%d",min,max);
}

OUTPUT:
enter the value of A:5
enter the value of B:3
enter the value of C:7
Minimum=3
Maximum=7

21
170080111047

05: WRITE A PROGRAM TO CHECK THE GIVEN YEAR IS LEAP YEAR


OR NOT.

ALGORITHM:
Step 1: start
Step 2: input the year
Step 3: if year%4==0 and year%100!=0 or if year%400==0 else goto
Step 4: print is a leap year
Step 5: print is not a leap year
step 6: stop

PROGRAM:
#include<stdio.h>
void main()
{
int year;
printf("Enter a year:");
scanf("%d",&year);
if((year%4==0&&year%100!=0) || (year%400==0))
{
printf("%d is a leap year",year);
}
else
{
printf("%d is not a leap year",year);
}
}

OUTPUT:
Enter a year:1996
1996 is a leap year

22
170080111047

06: WRITE A PROGRAM TO CONVERT THE GIVEN CASE.

ALGORITHM:
Step 1: start
step 2: read ch
Step 3: if(ch>='A' && ch<='Z') else goto step 5
Step 4: ch=ch+32
Step 5: ch=ch-32
Step 6: print
Step 7: stop

PROGRAM:
#include<stdio.h>
void main()
{
char ch;
printf("Enter any character:");
scanf("%c",&ch);
if(ch>='A' && ch<='Z')
ch=ch+32;
else if(ch>='a' && ch<='z')
ch=ch-32;
printf("Convert case of the character: %c\n",ch);
}

OUTPUT:
Enter any character:k
Convert case of the character: K

23
170080111047

07: WRITE A PROGRAM TO FIND OUT NET SALARY OF AN EMPLOYEE.

ALGORITHM:
Step 1: read basic salary
Step 2: if( bs>=10000) else goto step 5
Step 3: salary=bs+(0.20*bs)+(0.15*bs)
Step 4: Print salary
Step 5: else if bs>=5000&&bs<10000 else goto step 6
Step 6: salary=bs+(0.50*bs)+(0.10*bs);
Step 7: Print salary
Step 8: salary= bs+(0.10*bs)+(0.05*bs)
Step 9: Print salary
Step 10: stop

PROGRAM:
#include<stdio.h>
void main()
{
int bs;
float hra,da,salary;
printf("enter basic salary \n");
scanf("%d",&bs);
if(bs>=10000)
{
salary=bs+(0.20*bs)+(0.15*bs);
printf("salary is %f \n",salary);
}
else if(bs>=5000&&bs<10000)
{
salary=bs+(0.50*bs)+(0.10*bs);
printf("salary is %f \n",salary);
}
else
{
salary= bs+(0.10*bs)+(0.05*bs);
printf("salary is %f \n",salary);
}
}

OUTPUT:

24
170080111047

enter basic salary 10000


salary is 13500.

25
170080111047

08: WRITE A PROGRAM TO READ CUSTOM CODES AND CALLS MADE


AND PRINT THE BILL FOR EACH CUSTOMER

ALGORITHM:
Step 1: Input number of calls of customer1 and store in Cus1.
Step 2: Input number of calls of customer2 and store in Cus2.
Step 3: Check Cus1<=100 then store Bill1=250 otherwise compute
(250+(1.25*Cus1)) & store in Bill11.
Step 4: Check Cus2<=100 then store Bill2=250 otherwise compute
(250+(1.25*Cus2)) & store in Bill12.
Step 5: Display Bill1.
Step 6: Display Bill2.

PROGRAM:
#include<stdio.h>
void main()
{
floatCus1,Cus2,Bil
l1,Bill2;
printf("Enter Numbers of Call of Customer 1:--
\n");
scanf("%f",&Cus1);
printf("Enter Numbers of Call of Customer 2:--
\n");
scanf("%f",&Cus2);
Cus1<=100?Bill1=250:Bill1=(250+Cus1*1.25);
Cus2<=100?Bill2=250:Bill2=(250+Cus1*1.25);
printf("Mobile Bill of Customer 1:-- %f\n",Bill1);
printf("Mobile Bill of Customer 2:- %f\n”,bill2);
}

OUTPUT:
Enter Numbers of Call of Customer 1:--
100
Enter Numbers of Call of
Customer 2:-Mobile Bill of
Customer 1:--
250.000000
Mobile Bill of Customer 2:--
375.000000

26
170080111047

SET 3: ‘C’ PROGRAMS USING FOR, WHILE, DO WHILE

27
170080111047

01: WRITE A PROGRAM TO PRINT FIRST N NATURAL NUMBERS AND


CALCULATE ITS AVERAGE.

ALGORITHM:
Step 1: start
Step 2: int i=1,n sum=0,avg=0
Step 3: if i<n is true then go to next step else go to step 8
Step 4: print value of i
Step 5: sum=sum+i
Step 6: go to step 3
Step 7: go to step 3
Step 8: avg=sum/n
Step 9: print values of sum and avg
Step 10: stop

PROGRAM:
#include<stdio.h>
void main()
{
int i=1,n,sum=0;
float avg;
printf("enter a number:");
scanf("%d",&n);
while(i<=n)
{
printf("%d \n",i);
sum=sum+i;
i++;
}
avg=sum/n;
printf("sum is %d \n",sum);
printf("average is %f \n",avg);
}

OUTPUT:
Enter a number till which you want to print:5
1
2

28
170080111047

3
4
5
Sum is 15
Avgerage is 3.

29
170080111047

02: WRITE A PROGRAM TO PRINT SQUARE AND CUBE OF 1 ST N


NATURAL NUMBER & CALCULATE THEIR SUM & AVG.

ALGORITHM:
Step 1: Start
Step 2: read n
Step 3: int square,cube
Step 4: for loop i=0,i<=n,i++
Step 5: square=i*i
Step 6: cube=i*i*i
Step 7: print square and cube
Step 8: int sum=square+sum
Step 9: int avg=sum/i

PROGRAM:
#include<stdio.h>
Void main()
{
int I,n,sq,cube,sum,avg:
printf(“enter n:”);
scanf(“%d”,&n);
for(i=0;i<n;i++)
{
Sq=i*i;
Cube=i*i*i;
Sum=sum+sq;
Avg=sum/I;
}
Printf(“sum is %d \n”,sum);
Printf(“avg is %d”,avg);
}

OUTPUT:
Enter n 3
Sum 14
Avg 4

30
170080111047

03: WRITE A PROGRAM TO PRINT NUMBERS FROM –N TO N.

ALGORITHM:
Step 1: let i,n;
Step 2: Enter n
Step 3: for(i=-n;i<=n;i++)
Step 4: print i
Step 5: End

PROGRAM:
#include<stdio.h>
void main()
{
int i,n;
printf("Enter the value of n:");
scanf("%d",&n);
for(i=-n;i<=n;i++)
{
printf("%d\n",i);
}
}

OUTPUT:
Enter the value of n:2
-2
-1
0
1
2

31
170080111047

04: WRITE A PROGRAM TO PRINT ODD AND EVEN NUMBERS AND


PRINT ITS SUM AND AVERAGE.

ALGORITHM:
Step 1: let i,num,oddsum=0,evensum=0,avg;
Step 2: enter n
Step 3: for(i=1;i<=num;i++)
Step 4: if(i%2==0)
Step 5: evensum=evensum+i;
Step 6: else
Step 7: odds um=oddsum+i;
Step 8: avg=(evensum+oddsum)/num;
Step 9: print oddsum
Step 10: print evensum
Step 11: print avg
Step 12: End

PROGRAM:
#include<stdio.h>
void main()
{
int i,num,oddsum=0,evensum=0;
float avg;
printf("Enter the value of number:\n");
scanf("%d",&num);
for(i=1;i<=num;i++)
{
if(i%2==0)
{
evensum=evensum+i;
}
else
{
oddsum=oddsum+i;
}
}
avg=(evensum+oddsum)/num;11
printf("The sum of odd numbers is %d\n",oddsum);
printf("The sum of even numbers is %d\n",evensum);
printf("The average is %f\n",avg);

32
170080111047

OUTPUT:
Enter the value of number:
10
The sum of odd numbers is 25
The sum of even numbers is 30
The average is 5.000000

33
170080111047

05: WRITE A PROGRAM TO PRINT NUMBERS BETWEEN X AND Y.

ALGORITHM:
Step 1: let i,x=1,y=10,sum=0,avg;
Step 2: for(i=1;i<=y;i++)
Step 3: print i
Step 4: sum=sum+i
Step 5: print sum
Step 6: avg=sum/10
Step 7: print avg
Step 8: End

PROGRAM:
#include<stdio.h>
void main()
{
int i,x=1,y=10,sum=0;
float avg;
for(i=1;i<=y;i++)
{
printf("%d\n",i);
sum=sum+i;
}
printf("Sum=%d\n",sum);
avg=sum/10;
printf("Average is %f\n",avg);
}

OUTPUT:
1
2
3
4
5
6
7
8
9

34
170080111047

10
Sum=55
Average is 5.00000

35
170080111047

06: WRITE A PROGRAM TO PRINT ALL NUMBERS BETWEEN X AND Y


AND PRINT SUM AND AVERAGE.

ALGORITHM:
Step 1: i,x=1,y=10,sum=0,avg;
Step 2: printf("even numbers:\n");
Step 3: for(i=1;i<=y;i++)
Step 4: if(i%2==0)
Step 5: print i
Step 6: printf("odd numbers:\n");
Step 7: for(i=1;i<=y;i++)
Step 8: if(i%2!=0)
Step 9: print i
Step 10: sum=sum+i;
Step 11: print sum
Step 12: avg=sum/10;
Step 13: print avg
Step 14: End

PROGRAM:
#include<stdio.h>
void main()
{
int i,x=1,y=10,sum=0;
float avg;
printf("Even numbers:\n");
for(i=1;i<=y;i++)
{
if(i%2==0)
{
Printf("%d\n",i);
}
}
printf("Odd numbers:\n");
for(i=1;i<=y;i++)
{
if(i%2!=0)
{
printf("%d\n",i);
}

36
170080111047

}
sum=sum+i;
printf("Sum=%d\n",sum);
avg=sum/10;
printf("Average is %f\n",avg);
}

OUTPUT:
Even numbers:
2
4
6
8
10
Odd numbers:
1
3
5
7
9
Sum=11
Average is 1.000000

37
170080111047

07: WRITE A PROGRAM TO PRINT EVERY NUMBER <100 AFTER


EVERY 2 NUMBERS.

ALGORITHM:
step 1: start
step 2: sum=0,i=2,n=0
step 3: float avg
step 4: print every third no. begining from 2 until<100
step 5: while(i<100)
step 6: sum=sum+i
step 7: n++
step 8: i=i+3
step 9: print sum
step 10: sum=sum/n
step 11: print avg
step 12: stop

PROGRAM:
#include<stdio.h>
void main()
{
int sum=0,i=2,n=0;
float avg;
printf("every third no. begining from 2 until<100 are \n");
while(i<100)
{
printf("%d",i);
sum=sum+i;
n++;
i=i+3;
}
printf("sum is %d",sum);
avg=sum/n;
printf("avg is %f",avg);
}

OUTPUT:
Every third no. beginning from 2 until<100 are
258111417202326293235384144475053565962656871747780838689929598
sum is 1650avg is 50.000000

38
170080111047

08: WRITE A PROGRAM TO PRINT ALL NUMBERS DIVISIBLE BY 5.

ALGORITHM:
step 1: start
step 2: int n=0;sum=0,avg=0
step 3: for loop i=0;i<100;i++
step 4: if i%5==0
step 5: n++
step 6: print i
step 7: sum=sum+i
step 8: avg=sum/n
step 5: stop

PROGRAM:
#include<stdio.h>
void main()
{
int sum=0,i=0;int n;
int avg;
printf("numbers exactly divisible by 5 until no<100 are");
for(i=0;i<100;i++)
{
if(i%5==0)
{
printf("%d \n",i);
sum=sum+i;
n++;
}
}
avg=sum/n;
printf("sum and avg is %d and %d respectively \n",sum,avg);
}

OUTPUT:
Numbers exactly divisible by 5 until no<100 are
05101520253035404550556065707580859095sum and avg is 950 and 47
respectively

39
170080111047

09: WRITE A PROGRAM TO PRINT SERIES.


-15,-10,-5,0,5,10,15

ALGORITHM:
step 1: start
step 2: int i=-15
step 3: print the series
step 4: for(i,i<=15;i=i+5)
step 5: print i
step 6: stop

PROGRAM:
#include<stdio.h>
void main()
{
int i=-15;
printf("the series is");
for(i;i<=15;i=i+5)
{
printf("%d",i);
}
}

OUTPUT:
the series is-15-10-5051015

40
170080111047

10: WRITE A PROGRAM TO PRINT VALUE OF FOLLOWING SERIES -


1,x,-x^2,……

ALGORITHM:
Step 1: start
Step 2: int i
Step 3: float x,a,b,c
Step 4: printnthe value of x
Step 5: print the series
Step 6: print -1
Step 7: for(i=1;1<20;i++)
Step 8: a=(pow(x,i));
Step 9: b=(pow(-1,(i+1)));
Step 10: c=a*b;
Step 11: print the value of c
Step 12: stop

PROGRAM:
#include<stdio.h>
#include<math.h>
void main()
{
int i:
float x,a,b,c;
printf(“enter the value of x”);
scanf(“%f”,&x);
printf(“series is”);
printf(“-1”);
for(“i=1;1<20;i++)
{
a=(pow(x,i));
b=(pow(-1,(i+1)));
c=a*b;
printf(“,%f”,c);
}
}

OUTPUT:
Enter the value of x : 3
-1,3,-9,27

41
170080111047

11: WRITE A PROGRAM TO PRINT MULTIPLICATION OF


TABLE(X*1=X)

ALGORITHM:
Step 1: declare header file
step 2: declare i,num
step 3: intialize the loop for(i=1;i<=10;i++)
step 4: print num*i
step 5: stop

PROGRAM:
#include<stdio.h>
void main()
{
int i,n;
printf("Enter num: \n ");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
for(j=1;j<=10;j++)
{
pro=i*j;
printf("%d * %d = %d\n",i,j,pro);
}
Printf(“\n”);
}
}

OUTPUT:
Enter num to print table: 2
1*1=1
1*2=2
1*3=3
1*4=4
1*5=5
1*6=6
1*7=7
1*8=8
1*9=9
1*10=10

42
170080111047

2 * 1 = 2
2 * 2 = 4
2 * 3 = 6
2 * 4 = 8
2 * 5 = 10
2 * 6 = 12
2 * 7 = 14
2 * 8 = 16
2 * 9 = 18
2 * 10 = 20

12: WRITE A PROGRAM TO CALCULATE THE POWER OF NUMBER(X^N)


WITHOUT USING FUNCTION.

ALGORITHM:
step 1: start
step 2: declare n=0,i,x=1,k
step 3: enter the value of x and n
step 4: k=x
step 5: using for loop(i=1;i<n;i++)
step 6: k=k*x
step 7: print ans
step 8: stop

PROGRAM:
#include<stdio.h>
void main()
{
int n=0,i,x=1;
int k;
printf("enter x:");
scanf("%d",&x);
printf("enter n:");
scanf("%d",&n);
k=x;
for(i=1;i<n;i++)
{

43
170080111047

k=k*x;
}
printf("ans is=%d \n",k);
}

OUTPUT:
enter x :2
enter n :3
ans is=8

44
170080111047

13: WRITE A PROGRAM TO PRINT FACTORIAL OF GIVEN NUMBER.

ALGORITHM:
Step 1: start
step 2: declare header files and varibles
step 3: print enter n
step 4: using for loop i=1,i<=n,i++
step 5: fact=fact*1
step 6: print factorial
step 7: stop

PROGRAM:
#include<stdio.h>
void main()
{
int n;
long ans;
printf("enter a num :");
scanf("%d",&n);
for(i=0;i<n;i++)
{
ans= ans*i;
}
printf("factorial=%ld\n",ans);
}

OUTPUT:
enter a num :5
factorial=24

45
170080111047

14: WRITE A PROGRAM TO PRINT ALL ALPHABETS IN LOWER CASE


AND UPPERCASE.

ALGORITHM:
Step 1: start
step 2: declare header files and varible
step 3: intialize first for loop(i=A;i<=Z;i++)
step 4: intialize second for loop inside the first loop
step 5: for(i=a;i<=z;i++)
step 6: stop

PROGRAM:
#include<stdio.h>
void main()
{
char i;
printf("Capital (upper) case characters:\n");
for(i='A';i<='Z';i++)
printf("%c ",i);
printf("\nLower case characters:\n");
for(i='a';i<='z';i++)
printf("%c ",i);
}

OUTPUT:
Capital (upper) case chatacter:
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
Lower case character:
a b c d e f g h i j k l m n o p q r s t u v w x y z

46
170080111047

15: WRITE A PROGRAM TO PRINT FIRST N NATURAL NUMBER OF


FIBONACCI SERIES.

ALGORITHM:
Step 1: declare the header file and variable
Step 2: initialize fib1=o,fib2=1,fib3num,count=0
Step 3: enter the value of n
Step 4: initialize the loop while(count<num)
Step 5: fib3=fib1+fib2
Step 6: count++
Step 7: print fib3

PROGRAM:
#include<stdio.h>
void main()
{
int fib1=0,fib2=1,fib3,num,count=0;
printf("enter the value of number");
scanf("%d",&num);
printf("first %d FIBONACCI numbers are...\n",num);
printf("%d\n",fib1);
printf("%d\n",fib2);
count=2;
while(count<num)
{
fib3=fib1+fib2;
count++;
printf("%d\n",fib3);
fib1=fib2;
fib2=fib3;
}
}

OUTPUT:
enter the value of number5
0
1
1
2
3

47
170080111047

16: WRITE A PROGRAM TO CHECK IF A GIVEN NUMBER IS A PRIME


NUMBER OR NOT.

ALGORITHM:
Step 1: start
Step 2: define the header files and variables
Step 3: enter the value of n
Step 4: initialize loop for(i=2;i<n;i++)
Step 5: initialize the loop inside the first one
Step 6: print the value of n
Step 7: stop

PROGRAM:
#include<stdio.h>
void main()
{
int n,i,flag=0;
printf("enter any number \n");
scanf("%d",&n);
for(i=2;i<n;i++)
{
if(n%i==0)
{
flag=1;
break;
}
}
if(flag==0)
{
printf("\n%d is a prime number",n);
}
else
{
printf("\n%d is not a prime number",n);
}
}

OUTPUT:
enter any number
5
5 is a prime number

48
170080111047

17: WRITE A PROGRAM TO PRINT ALL THE CHARACTERS


BETWEEN X AND Y.

ALGORITHM:
Step 1: start
Step 2: declare header file and initialize variable x,y,i
Step 3: use loop for(i=x;i<=y;i++)
Step 4: print the value of i in %d
Step 5: stop

PROGRAM:
#include<stdio.h>
void main()
{
int x,y,i;
printf("enter x and y");
scanf("%d%d",&x,&y);
for(i=x;i<=y;i++)
{
printf("%d",i);
printf("\t");
}
}

OUTPUT:
enter x and y2 5
2 3 4 5

49
170080111047

18: WRITE A PROGRAM TO PRINT 1-X+X^2/2!-X^3/3!+…..X^N/N!

ALGORITHM:
Step 1: start
Step 2: declre the header files and variables
Step 3: enter the value of x and n
Step 4: initialize the loop for (x=0.1;x<=1.0;x++)
Step 5: sum+=pow(-1,i)*(pow(x,i)/fact)
Step 6: print x and sum
Step 7: stop

PROGRAM:
#include<stdio.h>
#include<math.h>
void main()
{
float x,sum,fact;
int i,k;
for(x=0.1;x<=1.0;x++0.1)
{
sum=0;
for(i=0;i<10;++i)
{
fact=1;
for(k=1;k<=i;++k)
{
fact*=k;
}
sum+=pow(-1,i)*(pow(x,i)/fact);
}
printf("x=%f\tsum=%f\n",x,sum);
}
}

OUTPUT:
x=0.100000 sum=0.904837
x=0.200000 sum=0.818731
x=0.300000 sum=0.740818
x=0.400000 sum=0.670320

50
170080111047

SET 4:‘C’ PROGRAMS FOR PATTERN GENERATION

51
170080111047

01: PATTERN
1
12
123
1234
12345

ALGORITHM:
Step 1: start
Step 2: let i,j,n=5;
Step 3: for(i=1;i<=n;++i)
Step 4: for(j=1;j<=i;++j)
Step 5: print j
Step 6: end

PROGRAM:
#include<stdio.h>
void main()
{
int i,j,n=5;
for(i=1;i<=n;++i)
{
for(j=1;j<=i;++j)
{
printf(“%d”,j);
}
printf(“\n”);
}
}

52
170080111047

02: PATTERN
1
121
12321
1234321
123454321

ALGORITHM:
Step 1: start
Step 2: let i,j,n,k;
Step 3: enter the number
Step 4: for(i=1;i<=n;i++)
Step 5: for(k=n;k>i;k--)
Step 6: print space
Step 7: for(j=1;j<=i;j++)
Step 8: print j
Step 9: if(i>1)
Step 10: for(j=i;j>1;j--)
Step 11: print j-1
Step 12: End

PROGRAM:
#include<stdio.h>
void main()
{
int i,j,n,k;
printf("enter the number :");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
for(k=n;k>i;k--) printf(" ");
for(j=1;j<=i;j++)
{
printf("%d",j);
}
if(i>1)
{
for(j=i;j>1;j--)
{
printf("%d",j-1);

53
170080111047

}
}
printf("\n");
}
}

54
170080111047

03: PATTERN
GHIJ
DEF
BC
A

ALGORITHM:
Step 1: start
Step 2: Read int i, j
Step 3: Read char ch='G'
Step 4: for(i=4;i>0;i--)
Step 5: for(j=1;j<=i;j++)
Step 6: print ch++
Step 7: if(i>=3)
Step 8: ch=ch-(i+3);
Step 9: else
Step 10: ch=ch-i;
Step 11: End

PROGRAM:
#include<stdio.h>
void main()
{
int i,j;
char ch='G':
for(i=4;i>0;i--)
{
for(j=1;j<=i;j++)
{
printf("%c ",ch++);
}
if(i>=3)
{
ch=ch-(i+3);
}
else
{
ch=ch-i;
}
printf("\n");

55
170080111047

56
170080111047

04: PATTERN
AAAAA
BBBB
CCC
DD
E

ALGORITHM:
Step 1: start
Step 2: let i,j;
Step 3: for (i=1;i<=5;i++)
Step 4: for (j=5;j>=i;j--)
Step 5: print 'A'-1 + i
Step 6: End

PROGRAM:
#include <stdio.h>
int main()
{
int i, j;
for (i=1;i<=5;i++)
{
for (j=5;j>=i;j--)
{
printf("%c",'A'-1 + i);
}
printf("\n");
}
return 0;
}

57
170080111047

05: PATTERN
A
A B
A B C
A B C D

ALGORITHM:
Step 1: start
Step 2: Read int i, j, n
Step 3: Read char c
Step 4: print Enter n
Step 5: for loop i=1,i<=n,i++
Step 6: {c=’A’ for loop j=1,j<=i;j++
Step 7: print c}

PROGRAM:
#include<stdio.h>
void main()
{
int i,j,n;
char c;
printf("Enter n:");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
c='A';
for(j=1;j<=i;j++)
{
printf("%c",c);
}
printf("\n");
}
}

58
170080111047

06: PATTERN
*
* *
* * *
* * * *

ALGORITHM:
Step 1: start
Step 2: Define the Header files
Step 3: Define the variables required for given pattern.
Step 4: Make the loop for the variable ‘i’ define its condition.
Step 5: Make loop for variable ‘k’ for spaceing purpose
Step 6: Formulate the same conditions for loop variable ‘j’
Step 7: Now print the required output using ‘printf’ function.
Step 8: Check the output.

PROGRAM:
#include<stdio.h>
void main()
{
int i,j,n;
printf("Enter n:");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
for(j=1;j<=i;j++)
{
printf("* ");
}
printf("\n");
}
}

59
170080111047

07: PATTERN
1
01
101
1010

ALGORITHM:
Step 1: start
Step 2: Define the Header files
Step 3: Make the loop for the variable ‘i’ define its condition.
Step 4: if i%2!=0 then k=1, else k=0
Step 5: Similarly formulate the same conditions for loop for
variable ‘j’
Step 6: now print the required output using ‘printf’ function.
Step 7: Check the output.

PROGRAM:
#include<stdio.h>
void main()
{
int i,j,n;
printf("Enter n:");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
for(j=i;j>=1;j++)
{
if(j%2==0)
{
printf("0");
}
else
{
printf("1");
}
}
printf("\n");
}
}

60
170080111047

08: PATTERN
*
**
***
****

ALGORITHM:
Step 1: start
Step 2: Define the Header files
Step 3: Define the variables required for given pattern.
Step 4: Make the loop for the variable ‘i’ define its condition.
Step 5: Formulate the same conditions for loop fpr variable
‘j’, define its conditions and print the output for formulated
loop.
Step 6: Now print the required output using ‘printf’ function.
Step 7: Check the output.

PROGRAM:
#include<stdio.h>
void main()
{
int i,j,n;
printf("Enter n:");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
for(j=1;j<=i;j++)
{
printf("*");
}
printf("\n");
}
}

61
170080111047

09: PATTERN
1
21
321
4321
54321

ALGORITHM:
Step 1: Store 5 to i,j,n;
Step 2: for(i=1;i<=n;++i)
Step 3: for(j=i;j>=1;--j)
Step 4: Display J
Step 5: End

PROGRAM:
#include<stdio.h>
void main()
{
int i,j,n=5;
for(i=1;i<=n;++i)
{
for(j=i;j>=1;--j)
{
printf("%d",j);
}
printf("\n");}
}

62
170080111047

10: PATTERN
5
45
345
2345
12345

ALGORITHM:
Step 1: Store 5 to i,j,n;
Step 2: for(i=n;i>=1;++ i)
Step 3: for(j=i;j>=1;++j)
Step 4: Display i
Step 5: End

PROGRAM:
#include<stdio.h>
void main()
{
int i,j,n=5;
for(i=n;i>=1;--i)
{
for(j=i;j<=n;++j)
{
printf("%d",j);
}
printf("\n");
}
}

63
170080111047

11: PATTERN
1
22
333
4444
55555

ALGORITHM:
Step 1: let i,j,n;
Step 2: for(i=1;i<=5;i++)
Step 3: for (j=5;j>i;j--)
Step 4: for(k=i;k>=1;k--)
Step 5: Display k
Step 6: End

PROGRAM:
#include<stdio.h>
void main()
{
int i,j,n=5;
for(i=1;i<=n;++i)
{
for(j=1;j<=i;++j)
{
printf(" %d ",i);
}
printf("\n");
}
}

64
170080111047

12: PATTERN
1
121
32123
4321234

ALGORITHM:
Step 1: let i,j,k,l;
Step 2: for(i=1; i<=5; i++)
Step 3: for (j=5; j>i; j--)
Step 4: for(k=i; k>=1; k--)
Step 5: Display k
Step 6: for ( l=2; l <= 1; l++)
Step 7: Display l
Step 8: End

PROGRAM:
#include<stdio.h>
int main()
{
int i,j,k,l;
for(i=1;i<=5;i++)
{
for (j=5;j>i;j--)
{
printf(" ");
}
for(k=i;k>=1;k--)
{
printf("%d",k);
}
for(l=2;l<=i;l++)
{
printf("%d",l);
}
printf("\n");
}

65
170080111047

66
170080111047

13: PATTERN
0
101
21012
3210123

ALGORITHM:
Step 1: let i,j,k,l;
Step 2: for(i=1;i<=4;i++)
Step 3: for(j=4;j>i;j--)
Step 4: print space
Step 5: for(k=i-1;k>=0;k--)
Step 6: print k
Step 7: for(l=1;l<i;l++)
Step 8: print l
Step 9: End

PROGRAM:
#include<stdio.h>
void main()
{
int i,j,k,l;
for(i=1;i<=4;i++)
{
for(j=4;j>i;j--)
{
printf(" ");
}
for(k=i-1;k>=0;k--)
{
printf("%d",k);
}
for(l=1;l<i;l++)
printf("%d",l);
printf("\n");
}
}

67
170080111047

14: PATTERN
4444
333
22
1

ALGORITHM:
Step 1: let i,j,n;
Step 2: Store 4 to n
Step 3: for(i=n;i>=1;--i)
Step 4: for(j=1;j<=i;++j)
Step 5: Display i
Step 6: End

PROGRAM:
#include<stdio.h>
void main()
{
int i,j,n=4;
for(i=n;i>=1;--i)
{
for(j=1;j<=i;++j)
{
printf("%d",i);
}
printf("\n");
}
}

68
170080111047

15: PATTERN
A
A C E
A C E G I
A C E G I K
A C E G I
A C E
A

ALGORITHM:
Step 1: let i,j,s,k,n;
Step 2: char ch;
Step 3: Enter character
Step 4: ch=ch-1
Step 5: Enter n
Step 6: for(i=1;i<=n;i++)
Step 7: for(s=1;s<=n-i;s++)
Step 8: print space
Step 9: for(j=1,k=1;j<=2*i-1;j++,k=k+2)
Step 10: print ch+k
Step 11: for(i=n-1;i>=1;i--)
Step 12: for(s=1;s<=n-i;s++)
Step 13: print space
Step 14: for(j=1,k=1;j<=2*i-1;j++,k=k+2)
Step 15: print ch+k
Step 16: End

PROGRAM:
#include<stdio.h>
void main()
{
char ch;
int i,j,s,k,n;
printf("Enter a character:\n");
scanf("%c",&ch);
ch=ch-1;
printf("Enter total number of lines :");
scanf("%d",&n);
for(i=1;i<=n;i++)
{

69
170080111047

for(s=1;s<=n-i;s++)
{
printf(" ");
}
for(j=1,k=1;j<=2*i-1;j++,k=k+2)
{
printf("%2c",ch+k);
}
printf("\n");
}
for(i=n-1;i>=1;i--)
{
for(s=1;s<=n-i;s++)
{
Printf(" ");
}
for(j=1,k=1;j<=2*i-1;j++,k=k+2)
{
printf("%2c",ch+k);
}
printf("\n");
}
}

70
170080111047

16: PATTERN
1
AB
123
ABCD
12345

ALGORITHM:
Step 1: let r,c,num;
Step 2: Store 5 to num
Step 3: char ch='A';
Step 4: for(r=1;r<=num;r++)
Step 5: if(r==2||r==4) else goto step 8
Step 6: for(c=1;c<=r;c++)
Step 7: print ch
Step 8: for(c=1;c<=r;c++)
Step 9: print c
Step 10: End

PROGRAM:
#include<stdio.h>
int main()
{
int num=5,r,c;
char ch;
for(r=1; r<=num; r++)
{
if(r==2 || r==4)
{
ch='A';
for(c=1; c<=r; c++,ch++)
printf("%c",ch);
}
else
{
for(c=1; c<=r; c++)
printf("%d",c);
}
printf("\n");
}
}
71
170080111047

SET 5: ‘C’ PROGRAMS USING AN ARRAY

72
170080111047

01: WRITE A PROGRAM TO READ N INTEGERS AND PRINT N


INTEGERS.

ALGORITHM:
Step 1: Start
Step 2: Define array “a” of 90 elements.
Step 3: Print –“Enter the integers:” using “printf” function.
Step 4: Now, for (i=0;i<=:++i), print a[i] using “printf” function.
Step 5: Stop.

PROGRAM:
#include<stdio.h>
void main ()
{
int i,n,a[90];
printf(“Enter limit=”);
scanf(“%d”,&n);
for(i=0;i<n;i++)
{
Printf(“\n Element=”);
Scanf(“%d”,&a[i]);
}
for(i=0;i<n;i++)
{
Printf(“\n”, a[i]);
}
}

OUTPUT:
Enter limit=5
Element=10
Element=11
Element=12
Element=13
Element=14

10
11
12
13

73
170080111047

14

74
170080111047

02: WRITE A PROGRAM TO FIND LARGEST AND SMALLEST NUMBER


USING AN ARRAY.

ALGORITHM:
Step 1: Start.
Step 2: Define an array “a” of 90 elements.
Step 3: Print- the limit using “printf” function and enter
Elements using “printf” function.
Step 4: For(i=0;i<n;i++), read “a[i]”; min=a[0] and
max=a[0].
Step 5: Now, for(i=1;i<n;i++), if a[i]>max, then max a[i],
Else “printf” function.
Step 6: print- “Largest is:”, and “Smallest is:”, using
“printf” function.
Step 7: Stop.

PROGRAM:

#include<stdio.h>
#include<conio.h>
Void main()
{
int max,min,a[90],i,n;
printf(“Enter limit=”);
scanf(“%d”,&n);
for(i=0;i<n;i++)
{
printf(“\nElement=”);
scanf(“%d”,&a[i]);
}
max=a[0];
min=a[0];
for(i=0;i<n;i++)
{
if(max<a[i])
{
max=a[0];
}
}
for(i=0;i<n;i++)
{
if(min>a[i])

75
170080111047

{
max=a[i];
}
}
printf(“\n maximum=”, max);
printf(“\n minimum=”, min);
}

OUTPUT:
Enter limit=5
45
23
65
90
92
Maximum: 92
Minimum: 23

76
170080111047

03: WRITE A PROGRAM TO ARRANGE AN ARRAY OF N ELEMENTS IN


ASCENDING ORDER.

ALGORITHM:
Step 1: Start.
Step 2: Define an array “a” of 10 elements, and other variables.
Step 3: Print- “enter limit” using “printf” function and enter
elements using “printf” function.
Step 4: for(i=0;i<n;i++), read “a[i]”
Step 5: now, for(i=0;i<n;i++): For(j=i+1;j<n;j++), if a[i]>a[j],
then
t=a[i]; a[i]=a[j]; implies that a[j]=t.
Step 6: Print “sorted list:” using “printf” function,
For(i=0;i<n;i++), print “a[i]” using “printf”
function.
Step 7: Stop.

PROGRAM:
#include<stdio.h>
#include<conio.h>
void main()
{
int I,j,n,a[90],t;
printf(“Enter limit=”);
scanf(“%d”,&n);
for(i=0;i<n;i++)
{
printf(“\n elements:-“);
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;
}

77
170080111047

}
}
printf(“\n—Ascending order—“);
for(i=0;i<n;i++)
{
printf(“\n %d”, a[i]);
}
}

OUTPUT:
Enter limit=5
5
90
45
33
--Ascending order—
5
33
45
90

78
170080111047

04: WRITE A PROGRAM TO ARRANGE AN ARRAY OF N ELEMENTS IN


DESCENDING ORDER.

ALGORITHM:
Step 1: start
Step 2: read n and enter array elements
Step 3: for loop i=0,i<n,i++
Step 4: for loop j=i+1,j<n,j++
Step 5: temp=arr[i],arr[i]=arr[j],arr[j]=temp
Step 6: print arr[i]

PROGRAM:
#include <stdio.h>
#define MAX 100
int main()
{
int arr[MAX],n,i,j;
int temp;
printf("Enter total number of elements: ");
scanf("%d",&n);
printf("Enter array elements:\n");
for(i=0;i< n;i++)
{
printf("Enter element %d: ",i+1);
scanf("%d",&arr[i]);
}
for(i=0;i< n;i++)
{
for(j=i+1;j< n;j++)
{
if(arr[i]< arr[j])
{
temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}
}
}
printf("\nArray elements after sorting:\n");
for(i=0;i< n;i++)
{
printf("%d\n",arr[i]);
}
}

79
170080111047

OUTPUT:
Enter total number of elements: 5
Enter array elements:
Enter element 1: 100
Enter element 2: 999
Enter element 3: 200
Enter element 4: 800
Enter element 5: 300
Array elements after sorting:
999
800
300
200

80
170080111047

05: WRITE A PROGRAM TO READ THE MARKS OF ONE SUBJECT OF


20 STUDENTS AND COMPUTE IT IN CATEGORIES FAIL, PASS,
FIRST CLASS AND DISTINCTION.

ALGORITHM:
step 1: start
step 2: read marks of 20 students
step 3: for loop i=0,i<20,i++
step 4: if marks<65 print pass
step 5: if marks >85 print first class
step 6: else print dintinction

PROGRAM:
#include<stdio.h>
int main ()
{
int marks[20];
printf("enter the marks of 20 student");
for(int i=1;i<=20;i++)
scanf("%d",&marks[i]);
for(int i=1;i<=20;i++)
{
if(marks[i]<=33)
printf("FAIL\n");
else
{
if(marks[i]<65)
printf("PASS\n ");
else if(85>marks[i])
printf("FIRST CLASS\n");
else
printf("DISTINCTION\n");
}
}
}

OUTPUT:
Enter the marks of 20 student
54
59

81
170080111047

56
66
36
15
24
84
45
82
33
25
45
65
11
75
66
65
75
15
PASS
PASS
PASS
FIRST CLASS
PASS
FAIL
FAIL
FIRST CLASS
PASS
FIRST CLASS
FAIL
FAIL
PASS
FIRST CLASS
FAIL
FIRST CLASS
FIRST CLASS
FIRST CLASS
FIRST CLASS
FAIL

82
170080111047

06: WRITE A PROGRAM TO INSERT VALUE AT ITH LOCATION OR


VALUE ENTERED BY USER USING ONE DIMENSIONAL ARRAY.

ALGORITHM:
Step 1: start
Step 2: read n
Step 3: read location,value
Step 4: for(c=n-1;c>=position-1,c--)
Step 5: location==c
Step 6: print array

PROGRAM
#include <stdio.h>
int main()
{
int array[100], position, c, n, value;
printf("Enter number of elements in array\n");
scanf("%d", &n);
printf("Enter %d elements\n", n);
for (c = 0; c < n; c++)
{
scanf("%d", &array[c]);
printf("Enter the location where you wish to insert an
element:\n");
scanf("%d", &position);
printf("Enter the value to insert\n");
scanf("%d", &value);
}
for (c = n - 1; c >= position - 1; c--)
{
array[c+1] = array[c];
array[position-1] = value;
printf("Resultant array is\n");
}
for (c = 0; c <= n; c++)
{
printf("%d\n", array[c]);
} return 0;
}
OUTPUT

83
170080111047

Enter number of elements in array


5
Enter 5 elements
2
5
4
3
8
Enter the location where you wish to insert an element
4
Enter the value to insert
10
Resultant array is
2
5
4
10
3
8

84
170080111047

07: WRITE A PROGRAM TO DELETE VALUE AT ITH LOCATION OR


VALUE ENTERED BY USER USING USER DEFINED FUNCYION.

ALGORITHM:
Step 1: start
Step 2: read n,location
Step 3: for loop c=0,c<n,c++
Step 4: arr[c]=arr[c+1]
Step 5: print arr[i]

PROGRAM:
#include <stdio.h>
int main()
{
int array[100], position, c, n;
printf("Enter number of elements in array\n");
scanf("%d", &n);
printf("Enter %d elements\n", n);
for ( c = 0 ; c < n ; c++ )
{
scanf("%d", &array[c]);
printf("Enter the location where you wish to delete
element\n");
scanf("%d", &position);
if ( position >= n+1 )
printf("Deletion not possible.\n");
else
{
for( c = position - 1 ; c < n - 1 ; c++ )
{
array[c] = array[c+1];
printf("Resultant array is\n");
for( c = 0 ; c < n - 1 ; c++ )
{printf("%d\n", array[c]); }

}
} return 0;
}

OUTPUT:

85
170080111047

Enter number of elements in array


5
Enter 5 elemets
4
6
8
10
7
Enter the location where you wish to delete elements
2
Resultant array is
4
8
10
7

86
170080111047

08: WRITE A PROGRAM TO ADD TWO 3*3 MATRICES.

ALGORITHM:
Step 1: Start
Step 2: Define the main program
Step 3: let a[i][j],b[i][j],c[i][j],I,j
Step 4: read input from the user for matrix A Read the input for
user for matrix B
Step 5: for(i=0;i<<3;++j)
Step 6: {for(j=0;j<3;++j)
Step 7: Assign values by(“%d”,&A/B[i][j])
Step 8: c[i][j]=a[i][j]+b[i][j]
Step 9: print C[i][j]
Step 10: End

PROGRAM:
#inlcude<stdio.h>
void main()
{
int a[3][3],b[3][3],c[3][3],I,j:
printf(“Enter 3x3 matrix A:\n”);
for(i=0;i<<3;++j)
{
for(j=0;j<3;++j)
{
scanf(“%d”,&b[i][j]);
}
}
printf(“enter an 3x3 matrixB\n”);
{
for(j=0;j<3;++j)
{
scanf(“%d”,&b[i][j]);
}
}
for(i=0;i<3;++i)
{
for(j=0;j<3;++j)
{
c[i][j]=a[i][j]+b[i][j];

87
170080111047

}
}
printf(“the resultant matrix c isC\n”);
for(i=0;i<3;++i)
{
for(j=0;j<3;++j)
{
printf(“%d\t”,c[i][j]);
}
}
printf(“\n);
}
}

OUTPUT:
Enter 3x3 matrix A:
123
123
123
Enter 3x3 matrix B:
123
123
123
Resultant Matrix C:
246
246
246

88
170080111047

SET 6: ‘C’ PROGRAMS USING STRING OPERATION.

89
170080111047

01: WRITE A PROGRAM TO READ STRING FROM USER AND PRINT


THE LENGTH OF THE STRING.

ALGORITHM:
Step 1: Read a string from the user.
Step 2: Read string s1[20]
Step 3: While s1[len]!=0
Step 4: len++
Step 5: Print the length of the string
Step 6: End

PROGRAM:
#include<stdio.h>
void main()
{
int len=0;
char s1[20];
printf("Enter the String :\n");
scanf("%[^\n]",s1);
while (s1[len]!='\0'){
len++;
}
printf("length of the string is %d",len);
}

OUTPUT:
Enter the String:
Sankalp
Length of the string is 7

90
170080111047

02:WRITE A PROGRAM TO REVERSE A STRING.

ALGORITHM:
Step 1: Start
Step 2: Start the main program
Step 3: read values I and j=0
Step 4: read string input from the user
Step 5: str2[j]=str1[i]
Step 6: Print the Reverse string
Step 7: End

PROGRAM:
#include<stdio.h>
#include<string.h>
Void main()
{
char str1[100],str2[100]={0};
int i,j=0;
printf("Enter the string to reverse the string\n");
gets(str1);
for (i=strlen(str1)-1;i>=0;i++)
{
str2[j]=str1[1];
j++;
}
str2[j]='/0';
printf("The reverse of string is %s",s2);
}

OUTPUT:
Enter the string to be reverse
Sankalp
plaknaS

91
170080111047

03: WRITE A PROGRAM TO CHECK WHETHER GIVEN STRING IS


PALINDROME STRING OR NOT.

ALGORITHM:
Step 1:
start
Step 2:
define main program
Step 3:
Let i,j,flag=0
Step 4:
Read input string from the user
Step 5:
for(i=0,j=strlen(s)-1;i<strlen(s)/2;++I,--j)
Step 6:
if(s[i]!=s[j]) {
Flag=1;
Break;}
Step 7: print Pallindrome or not
Step 8: End

PROGRAM:
#include<stdio.h>
void main()
{
char s[20];
int i,j,flag=0;
printf(“Enter string: “);
scanf(“%s”,s);
for(i=0,j=strlen(s)-1;i<strlen(s)/2;++I,--j)
{
if(s[i]!=s[j]) {
Flag=1;
Break;
}
}
if (flag==0) printf(“pallindrome”);
else
{
printf (“not pallindrome”);
}

OUTPUT:
Enter string:
Sankalp
Not palindrome

92
170080111047

Enter string:
Malayalam
Pallindrome

93
170080111047

04: WRITE A PROGRAM TO READ YOUR NAME AND OUTPUT THE


ASCII CODE OF THE FIRST CHARACTER REPRESENTING YOUR NAME.

ALGORITHM:
Step 1: Start
Step 2: char c
Step 3: Enter character
Step 4: print ascii value for %c is %d
Step 5: if((c+1)>127&&(c+1)<0)
Step 6: print there is no next character
Step 7: else
Step 8: print character after %c is %c

PROGRAM:
#include<stdio.h>
main()
{
char c;
printf(“Enter the Character \n”);
scanf(“%c”,&c);
printf(“The ASCII value for %c is %d\n”,c,c);
if((c+1)>127&&(c+1)<0)
{
printf(“There is no next character after %c\n”);
}
Else
{
printf(“The character after %c is %c\n”,c,c+1);
}
}

OUTPUT:
Enter a Character
B
The ASCII value of B is 66
The character after B is C

94
170080111047

06: PROGRAM TO COUNT NUMBER OF WORDS IN A GIVEN STRING.

ALGORITHM:
Step 1: start
Step 2: int i ,word=1 ,char str[100]
Step 3: read string
Step 4: get string
Step 5: repeat steps 6 to 8 until i<strlen(str)
Step 6: using for loop calculate the string length of each word
and increase
Step 7: if str[i]= ‘ ’ then step 7
Step 8: word++
Step 9: print the count of words of string
Step 10 : stop

PROGRAM:
#inlcude<stdio.h>
#include<string.h>
Void main()
{
int I,word=1;
char str[100];
printf(“Enter the string \n”):
gets(str);
for(i=0;i<strlen(str);i++)
{
if ( str[1]==’ ‘)
{
word++;
}
}
printf(“%d\n”,word);
}

OUTPUT:
Enter the string:
My name is Udiit
4

95
170080111047

07: WRITE A PROGRAM TO COUNT NUMBER OF OCCURRENCE OF A


GIVEN CHARACTER IN A GIVEN STRING.

ALGORITHM:
Step 1: start
Step 2: count=0 ,char c,str[100]
Step 3: read string
Step 4: get string
Step 5: read the character to know its repeatance
Step 6: repeat steps 7 to until i<strlen(str)
Step 7: if str[i] is equal to c step 8
Step 8: count++
Step 9: print the character count that is repeated in the string
Step 10: stop

PROGRAM:
#include<stdio.h>
#include<string.h>
Void main()
{
int I,count=0;
char c,str[100];
printf(“Enter a sentence\n”);
gets(str);
printf(“Enter a charcter to know it’s repentance\n”);
scanf(“%c”,c);
for(i=0;i<strnlen(str);i++)
{
if(str[i]==c)
{
count++;
}
}
printf(“The letter %c repeated %d times\n”,c,count);
}

OUTPUT:
Enter a sentence
hello world
Enter a character to know it’s repeatance in sentence

96
170080111047

l
Letter l is repeated 3 times

97
170080111047

08: WRITE A PROGRAM TO COUNT TOTAL WORDS IN TEXT.

ALGORITHM:
Step 1: Start
Step 2: char strs[100],countw=0,strw[15],i;
Step 3: Enter the sentence
Step 4: len=strlen(strs);
Step 5: for(i=0; i<len; i++)
Step 6: if(strs[i]==' ')
Step 7: countw++;
Step 8: print total numbers of word in the sentence
Step 9: End

PROGRAM:
#include<stdio.h>
void main()
{
clrscr();
char strs[100], countw=0, strw[15], i;
printf("Write a sentence : ");
gets(strs);
int len=strlen(strs);
for(i=0; i<len; i++)
{
if(strs[i]==' ')
{
countw++;
}
}
printf("Total number of words in the sentence is %d",countw+1);
getch();
}

OUTPUT:
Write a sentence: hello c program
Total number of words in the sentence: 3

98
170080111047

09: WRITE A PROGRAM TO COPY ONE STRING TO ANOTHER


STRING.

ALGORITHM:
Step 1: start
Step 2: input i,l=0
Step 3: input char source[20],dest[20]
Step 4: print enter source string
Step 5: read the string
Step 6: while source[l]!=null
Step 7: l++
Step 8: for loop i=0;i<=l;++i
Step 9: dest[i]=source[i]
Step 10: print destination string
Step 11: end

PROGRAM:
#include<stdio.h>
void main()
{
int l=0,i;
char source[20],dest[20];
printf("enter source string");
scanf("%[^\n]",source);
while(source[l]!='\0')
l++;
for(i=0;i<=l;++i)
{
dest[i]=source[i];
}
printf("destination string:%s",dest);
}

OUTPUT:
enter source string:hello
destination:hello

99
170080111047

10: WRITE A PROGRAM TO JOIN TWO STRINGS.

ALGORITHM:
step 1: start
step 2: read string s1[20],s2[20]
step 3: read int l1=0,l2=0
step 4: while s1[i]!= null
step 5: l1++
step 6: while s2[i]!= null
step 7: l2++
step 8: for loop j=0,i=l1;j<=l2;++i,++j
step 9: s1[i]=s2[j]
step 10: stop

PROGRAM:
#include<stdio.h>
void main()
{
char s1[20],s2[20];
int l1=0,l2=0,i,j;
printf("enter 1st string:");
scanf("%s",s1);
printf("enter 2nd string:");
scanf("%s",s2);
while(s1[l1]!='\0')
{
l1++;
}
while(s2[l2]!='\0')
{
l2++;
}
for(j=0,i=l1;j<=l2;++i,++j)
{
s1[i]=s2[j];
}
printf("first string after append:%s",s1);
}

100
170080111047

OUTPUT:
enter 1st string:hello
enter 2nd string:world
first string after append:helloworld

101
170080111047

SET 7: ‘C’ PROGRAMS USING FUNCTIONS.

102
170080111047

01: WRITE A PROGRAM TO FIND NUMBER IS ODD OR NOT.

ALGORITHM:
step 1: start
step 2: define function oddeven(int)
step 3: define main program
step 4: input value of number a
step 5: call the function oddeven
step 7: if(a%2==0),go to next step else go to step 10
step 8: print number is even else go to step9
step 9: print number is odd
step 10: stop

PROGRAM:
#include<stdio.h>
void oddeven(int);
void main()
{
int a;
printf("enter a:");
scanf("%d",&a);
oddeven(a);
}
void oddeven(int x)
{
if(x%2==0)
printf("number is even");
else
printf("number is odd");
}

OUTPUT:
enter a: 22
number is even

103
170080111047

02: WRITE A PROGRAM TO FIND OUT FACTORIAL OF GIVEN NUMBER


USING FUNCTION.

ALGORITHM:
Step 1: start
step 2: enter a number
step 3: ans=factorial
step 4: f=1
step 5: for(i=1;i<=m;i++)
step 6: return f
step 7: stop

PROGRAM:
#include<stdio.h>
void main()
{
int n;
long ans;
long factorial(int);
printf("Enter a number:");
scanf("%d",&n);
ans=factorial(n);
printf("Factorial = %ld\n",ans);
}
long factorial(int m)
{
int i;
long f=1;
for(i=1;i<=m;i++)f*=i;
return f;
}

OUTPUT:
Enter a number
5
factorial=120

104
170080111047

03: WRITE A PROGRAM TO GENERATE SERIES1+


X+X^2+X^3......+X^N.

ALGORITHM:
Step 1: start
Step 2: define function series(int a,int n)
Step 3: define variables i,j,k.
Step 4: use for loop for i and k
Step 5: again use for loop for j
Step 6: k=k*a
Step 7: print k
Step 8: define main function
Step 9: enter value of x and sum
Step 10: call function series
Step 11: stop

PROGRAM:
#include<stdio.h>
void series(int a,int n)
{
int i,j,k;
for(i=1,k=1;i<=n;i++)
{
for(j=1;j<i;j++)
{
k=k*a;
}
printf("%d",k);
}
}
void main()
{
int x,num;
printf("enter the value of x and num");
scanf("%d%d",&x,&num);
printf("\n the series is \n");
series(x,num);11
}

105
170080111047

OUTPUT:
Enter the values of x and num:
1 and 3
=111

106
170080111047

04: WRITE A PROGRAM TO FIND MAXIMUM OF THREE NUMBERS.

ALGORITHM:
Step 1: start
Step 2: Enter three numbers
step 3: call function
step 4: maxthree(a,b,c)
step 5: if a>b,else goto step:
step 6: if a>c,
step 7: print a
step 8: if b>c
step 9: print b else print c

PROGRAM:
#include<stdio.h>
void main()
{
int x,y,z,max;
int maxthree(int,int,int);
printf("Enter three numbers:");
scanf("%d%d%d",&x,&y,&z);
max=maxthree(x,y,z);
printf("Maximum=%d \n",max);
}
int maxthree(int a,int b,int c)
{
int maximum;
maximum=((a>b)?((a>c)?a:c):(b>c)?b:c);
return(maximum);
}

OUTPUT:
Enter three numbers:
2
4
3
Maximum=4

107
170080111047

05: WRITE A FUNCTION POWER THAT COMPUTES X^Y FOR INTEGER


X AND Y.

ALGORITHM:
Step 1: start
step 2: float x, ans
step 3: int y
step 4: float power(float,int)
step 5: enter x
step 6: enter y
step 7: ans=power(x,y)
step 8: print ans
step 9: float power(float a,float b)
step 10: int i
step 11: float p=1
step 12: for(i=1;i<=b;i++)
step 13: p*=a
step 14: return p
step 15: stop

PROGRAM:
#include<stdio.h>
void main()
{
float x,ans;
int y;
float power(float,int);
printf("Enter x:");
scanf("%f",&x);
printf("Enter y:");
scanf("%d",&y);
ans=power(x,y);
printf("%f",ans);
}
float power(float a,int b)
{
int i;
float p=1;
for(i=1;i<=b;i++);
{
p*=a;

108
170080111047

}
return p;
}

OUTPUT:
Enter x=3
Enter y=2
ans=3.0000

109
170080111047

06: WRITE A PROGRAM THAT USED USER DEFINED FUNCTION SWAP


() AND INTERCHANGE THE VALUE OF TWO VARIABLES.

ALGORITHM:
Step 1: Start.
Step 2: Declare all header files.
Step 3: Define function exch(int a,int b).
Step 4: int t.
Step 5: t=a :a=b :b=t.
Step 6: print a and b.
Step 7: define main function.
Step 8: input values of x and y from user.
Step 9: call function exch(x,y).
Step 10: stop.

PROGRAM:
#include<stdio.h>
void exch(int a,int b)
{
int t;
t=a;
a=b;
b=t;
printf("%d%d",a,b);
}
Void main()
{
int x,y;
printf("enter the value of x & y : ");
scanf("%d%d",&x,&y);
exch(x,y);
}

OUTPUT:
Enter the values of x & y:-
2 5
5 2

110
170080111047

07: WRITE A CALCULATOR PROGRAM PREPARE USER DEFINED


FUNCTION FOR EACH.

ALGORITHM:
Step 1: Start.
Step 2: declare all header files.
Step 3: define functions add, subtract, multiply, divide.
Step 4: define main functions.
Step 5: input two numbers num1, num2.
Step 6: input the choice of user for calculations and store in i.
Step 7: switch i.
Step 8: call function accordingly.
Step 9: case 1.
Step 10: num1+num2.
Step 11: Break.
Step 12: case2.
Step 13: num1-num2.
Step 14: break.
Step 15: case3.
Step 16: num1*num2.
Step 17: Break.
Step 18: case4.
Step 19: num1/num2.
Step 20: Break.
Step 21: Stop.

PROGRAM:
#include<stdio.h>
void add(int a,int b);
void subtract(int x,int y);
void divide(int c,int d);
void main()
{
int num1,num2;
int i;
printf("enter your two numbers \n");
scanf("%d%d",&num1,&num2);
printf("\n enter 1 for addition, 2 for subtraction, 3 for
multiplication or for division");
scanf("%d",&i);

111
170080111047

switch (i)
{
case 1:
add(num1,num2);
break;
case 2:
subtract(num1,num2);
break;
case 3:
multiply(num1,num2);
break;
case 4:
divide(num1,num2);
break;
}
}
void add(int x,int y)
{
printf("\n addition is %d",(x+y));
}
void subtract(int a,int b)
{
printf("\n subtraction is %d",(a-b));
}
void multiply(int e,int f)
{
printf("\n multiplication is %d",(e*f));
}
void divide(int c,int d)
{
printf("\n division is %f",(c/d));
}

OUTPUT:
Input numbers
5 3
Press 1 for addition, 2 for subtraction, 3 for multiplication or
4 for division
2
Subtraction is 2

112
170080111047

SET-9:‘C’ PROGRAMS USING POINTER

113
170080111047

01: WRITE A PROGRAM TO PRINT THE ADDRESS OF DIFFERENT


DATA TYPE VARIABLES ALONG WITH ITS VALUE.

PROGRAM:
#include<stdio.h>
#include<conio.h>
void main()
{
int a=5;
float b=10.20;
char c=’A’;
printf(“address of a= %u\n”, &a);
printf(“address of b= %u\n”, &b);
printf(“address of c= %u\n”, &c);
}

OUTPUT:
address of a= 65524
address of a= 65520
address of a= 65519

114
170080111047

02: WRITE A PROGRAM USING POINTER TO READ IN AN ARRAY OF


INTEGERS AND PRINT ITS ELEMENTS IN REVERSE ORDER.

PROGRAM:
#include<stdio.h>
#include<conio.h>
void main()
{
int *A, i, n;
printf(“Enter %d element \n”, n);
for(i=0; i<n; ++i)
scanf(“%d”, A+i);
printf(“Reverse order printing\n”);
for(i=n-1; i>=0; --i)
printf(“%d\n” *(A+i));
}

OUTPUT:
enter a string: hello
reverse :olleh

115
170080111047

03: WRITE A PROGRAM THAT COMPARES TWO INTEGER ARRAYS TO


SEE WHETHER THEY ARE IDENTICAL.

PROGRAM:
#include<stdio.h>
void main()
{
int ar1[5]={1,3,5,7,2};
int ar2[5]={2,4,5,6,7};
int i;
for(i=0; i < 5; i++)
{
if(ar1[i] != ar2[j])
{
printf(“Both the arrays are not identical”);
}
else
{
Printf(“Both the arrays are identical”);
}
}
}

OUTPUT:
Both the arrays are not identical

116
170080111047

04: WRITE A PROGRAM TO SORT THE ARRAY IN


ASCENDING/DESCENDING ORDER USING POINTER.
PROGRAM:
#include <stdio.h>
#include <conio.h>
void main()
{
int *arr,i,j,tmp,n;
printf("Enter how many data you want to sort : ");
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",arr+i);
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if( *(arr+i) > *(arr+j)){
tmp = *(arr+i);
*(arr+i) = *(arr+j);
*(arr+j) = tmp;
}
}
}
printf("\n\nAfter Sort\n");
for(i=0;i<n;i++)
printf("%d\n",*(arr+i));
}

OUTPUT:
Enter how many data you want to sort : 5
4
2
5
3
1
After Sort
1
2
3
4
5

117
170080111047

05: WRITE A PROGRAM USING POINTER TO COPY ONE STRING TO


ANOTHER STRING.

PROGRAM:
#include<stdio.h>
void copystr(char*,char*);
void main()
{
char*str1="CPU is easy";
char str2[30];
clrscr();
copystr(str2,str1);
printf("\n %s",str2);
}
void copystr(char *dest, char *src)
{
while(*src!='\0')
*dest++=*src++;
*dest='\0';
return;
}

OUTPUT:
CPU is easy

118
170080111047

SET 8: C PROGRAMS USING STRUCTURE

119
170080111047

01: WRITE A PROGRAM TO PRINT FOLLOWING QUANTITIES FOR ALL


SUBJECTS, TOTAL, MARKS, PERCENTAGE, RESULT & CLASS USING
STRUCTURE.

PROGRAM:
#incude<stdio.h>
#include<conio.h>
#include<string.h>
struct student
{
int roll no;
char name[20];
int m1,m2,m3;
int total;
float percentage;
char result[20];
char sclass[20];
};
void main()
{
int i,n;
struct student s[100];
printf(“how many student”);
scanf(“%d”,&n);
for(i=0;i<n;i++)
{
printf(“enter the roll no,name and their mark\n”);
scanf(“%d %s %d %d %d”,&s[i].roll
no,&s[i].name,&s[i].m1,&s[i].m2,&s[i].m3);
s[i].total=s[i].m1+s[i].m2+s[i].m3;
s[i].percentage=( s[i].m1+s[i].m2+s[i].m3)/3;
if(s[i].m1>=35 && s[i].m2>=35 && s[i].m3>=35)
{
strcpy(s[i].result,”pass”);
if(s[i].percentage>=60)
strcpy(s[i].sclass,”first class”);
else if(s[i].percentage>=50 && s[i].percentage<=59)
strcpy(s[i].sclass,”sc”);
else
strcpy(s[i].sclass,”pc”);
}
else
strcpy(s[i].result,”fail”);
120
170080111047

}
for(i=0;i<n;i++)
{
printf(“%d %s %d %d %d %d %f %s %s\n”,s[i].roll
no,s[i].name,s[i].m1,s[i].m2,s[i].m3,s[i].total,s[i].percentage,
s[i].result,s[i].sclass);
}

OUTPUT:
How many students 2
Enter the roll no, name and their marks
1 nisha 90 90 90
Enter the roll no, name and their marks
2 heta 75 90 80
1 nisha 90 90 90 270 90 pass first class
2 heta 50 60 55 165 55 pass sc

121
170080111047

02: WRITE A PROGRAM TO SEARCH AN ITEM FROM THE ARRAY OF


GIVEN ITEM STRUCTURE USING ITEM CODE.

PROGRAM:
#include<stdio.h>
#include<conio.h>
struct item
{
int code;
char name[20];
int qty;
};
void main()
{
item it[10];
int n,i,icode,flag=0;
printf(“how many items ? ”);
scanf(“%d”,&n);
for(i=0;i<n;++i)
{
printf(“item # %d\n”,i);
printf(“code, name, quantity : “);
scanf(“%d%s%d”,&it[i].code,&it[i].name,&it[i].qty);
}
printf(“enter item code for the item you want to view :”);
scanf(“%d”,&icode);
for(i=0;i<n;++i)
{
if(it[i].code==icode)
{
flag=1;
printf(“name : %s\tquantity : %d\n”,it[i].name,it[i].qty);
break;
}
}
if(flag==0)
printf(“item not found…”);
}

OUTPUT:
How many items ? 4
Item # 0
Code , name , quantity : 101 salt 45

122
170080111047

Item # 1
Code , name , quantity : 201 tea 20
Item # 2
Code , name , quantity : 301 soap 340
Item # 3
Code , name , quantity : 401 toothpaste 20
Enter item code for the item you want to view : 301
Name : soap quantity : 340

123
170080111047

03: WRITE A PROGRAM TO READ THE MARK OF ONE SUBJECT OF


20 STUDENTS AND COMPUTE THE NUMBER OF STUDENTS IN
CATEGORIES FAIL, PASS, FIRST CLASS AND DISTINCTION.
PROGRAM:
#include<stdio.h>
#include<conio.h>
void main()
{
int i,m[20],fail=0,fcls=0,pass=0,dist=0,j;
printf(“enter marks:”);
for(i=0;i<20;i++)
{
printf(“\n %d”,i+1);
scanf(“%d”,&m[i]);
}
for(i=0;i<20;i++)
{
j=0;
j=m[i]/10;
switch(j)
{
case10:
case9:
case8:
dist++;
break;
case7:
case6:
fcls++;
break;
case5:
case4:
pass++;
break;
default:
fail++;
break;
}
}
printf(“\n DISTINCTION=%d”,dist);
printf(“\n FIRST CLASS=%d”,fcls);
printf(“\n PASS=%d”,pass);
printf(“\n FAIL=%d”,fail);
}

124
170080111047

OUTPUT:
Enter
marks:99,89,89,87,56,34,65,67,68,78,65,45,67,75,64,45,56,76,58,3
0
DISTINCTION=4
FIRST CLASS=9
PASS=5
FAIL=2

125
170080111047

126

You might also like