Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
16 views34 pages

4 - C - Practical

The document outlines a series of programming experiments, each with algorithms, source code, and output examples. It covers various topics such as generating numbers, calculating averages, reading marks, finding factorials, and solving quadratic equations. Additionally, it includes flow charts to visualize the processes for each experiment.

Uploaded by

anaibafatima12
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views34 pages

4 - C - Practical

The document outlines a series of programming experiments, each with algorithms, source code, and output examples. It covers various topics such as generating numbers, calculating averages, reading marks, finding factorials, and solving quadratic equations. Additionally, it includes flow charts to visualize the processes for each experiment.

Uploaded by

anaibafatima12
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 34

1

Experiment No 1
Write a program that generate 1 to 10 numbers, calculate and print
average of these numbers.
Algorithm:

Step 1 START
SET sum= 0
Step 2 REPEAT a=1 TO 10
i.WRITE a
ii.SET sum=sum+a
Step 3 END REPEAT
Step 4 SET avg=sum/10
Step 5 END

Source Code :
void main()
{
int a,sum=0;
float avg;
clrscr();
for(a=1;a<=10;a++)
{
printf("\t%d",a);
sum+=a;
}
avg=sum/10;
printf(“\n The sum of first ten numbers is=\t%d”,sum);
printf(“\n And the average of first ten numbers is=\t%0.2f”,avg);
getch();
}

Output
1 2 3 4 5 6 7 8 9 10
The sum of first ten numbers is= 55
And the average of first ten numbers is= 5.5
2

Flow Chart
Start

SET sum=0

SET a=1

No
IF a<=10
THEN

Yes

Write a

SET sum=sum+a

SET a=a+1

SET avg=sum/10

SET sum, avg

End
3

Experiment No 2

Write a program that reads marks of five subjects, calculate the


total marks, percentage, and state whether the student is passed or
fail.

Algorithm
Step 1 START
Step 2 READ eng
Step 3 READ urdu
Step 4 READ phy
Step 5 READ comp
Step 6 READ math
Step 7 SET total =eng+urdu+phy+comp+math
Step 8 SET per=total*100/500
Step 10 WRITE total
Step 11 WRITE per
Step 12 IF per>33 THEN
WRITE “pass”
ELSE
WRITH “fail”
END IF
Step 10 END

Source Code
void main()
{
int eng,urdu,phy,comp,math,total;
float per;
printf("\nEnter marks for English\t");
scanf("%d",&eng);
printf("\nEnter marks for Urdu\t");
scanf("%d",&urdu);
printf("\nEnter marks for Physics\t");
scanf("%d",&phy);
printf("\nEnter marks for Computer\t");
scanf("%d",&comp);
printf("\nEnter marks for Mathematics\t");
scanf("%d",&math);
total=eng+urdu+phy+comp+math;
per=total*100/500;
printf("\nYour total marks are\t%d",total);
printf("\nYour Percentage is \t%0.2f"per);
if(per>=33)
4

printf("\nYou are Pass");


else
printf("\nYou are Fail");
getch();
}

Output
Enter marks for English 45
Enter marks for Urdu 77
Enter marks for Physics 63
Enter marks for Computer 48
Enter marks for Mathematics 78
Your total marks are 311
Your Percentage is 62.2
You are Pass
5

Flow Chart

Start

READ eng, urdu,


phy, comp, math

SET total
=eng+urdu+phy+comp+math

SET per=total*100/500

Write total

Write per

IF
Yes
per>33 Write “Pass”
THEN

No

Write “Fail”

End
6

Experiment No 3
Write a program to generate the table of N.
Algorithm
Step 1 START
Step 2 READ N
Step 3 REPEAT a=1 TO 10
WRITE N,a,N*a
END REPEAT
Step 4 END

Source Code
void main()
{
int a,N;
clrscr();
printf("\nEnter value to print its table\t");
scanf("%d"&a);
for(a=1;a<=10;a++)
{
printf("\n%dx%d=%d",N,a,N*a);
}
getch();
}

Output
Enter value to print its table 7
7x1=7
7x2=14
7x3=21
7x4=28
7x5=35
7x6=42
7x7=49
7x8=56
7x9=63
7x10=70
7

Flow Chart

Start

READ N

SET a=1

IF
a<=10 No
THEN

Yes

Write N, a, N*a

SET a=a+1

End
8

Experiment No 4
Find the factorial of N, use scanf() to read the value of N
Algorithm
Step1. Start.
Step2. Set f = 1, i = 0
Step3. Read num
Step4. Repeat WHILE num <= 0.
a. “Write Error! You have enter no. less than 0”
b. Read num
[End of outer WHILE loop]
Step5. Repeat FOR n = 1 to num
a. Set f = f * 1
[End of FOR Loop]
Step6. Write f
Step7. Exit
Source Code
void main()
{
double f=1;
int num,n;
clrscr();
printf("Enter the no. greater than 0 to find its factorial: ");
scanf("%d",&num);
while(num<0)
{
printf("\nError! number is less than 0.\n");
printf("Please enter number greater than 0: ");
scanf("%d",&num);
}
for(n=1;n<=num;n++)
{
f*=n;
}
printf("\nThe factorial of %d is %f",num,f);
getch();
}
Output
Enter the no. greater than 0 to find its factorial: -3

Error! number is less than 0.


Please enter number greater than 0: -5

Error! number is less than 0.


Please enter number greater than 0: 16

The factorial of 16 is 20922789888000.000000


9

Flow Chart

0
10

Experiment No 5
Write a program that finds out the largest number among three
input numbers.
Algorithm
Step 1 START
Step 2 READ a
Step 3 READ b
Step 4 READ c
Step 5 IF a>b AND a>c THEN
WRITE “a is largest”
ELSE IF b>c AND b>a THEN
WRITE “ b is largest”
ELSE
WRITE “ c is largest”
END IF
Step 6 END

Source Code
void main()
{
int a,b,c;
clrscr();
printf("\nEnter First Number\t");
scanf("%d",&a);
printf("\nEnter Second Number\t");
scanf("%d",&b);
printf("\nEnter Third Number\t");
scanf("%d",&c);
if(a>b&&a>c)
printf("\n First number is largest that is %d",a);
else if(b>a&&b>c)
printf("\n Second number is largest that is %d",b);
else
printf("\n Second number is largest that is %d",c);
getch();
}

Output
Enter First Number 45
Enter Second Number 72
Enter Third Number 34
Second number is largest that is 72
11

Flow Chart

Start

READ a, b, c

IF
a>b Yes
AND Write “a is largest”
a>c
THEN

No

IF Yes
a>b Write “b is largest”
AND
a>c
THEN

No

Write “c is largest”

End
12

Experiment No 6
Write a program which uses arithmetical operators to calculate the area of
a triangle, volume of a sphere, etc.
Algorithm
Step1. Start
Step2. Read a, b, r, T, S
Step3. Set T = (1.0/2.0)*(a*b)
S = (4.0/3.0)*(3.142)*(r*r*r)
Step4. Write T, S
Step5. Exit

Source Code
void main()
{
float a,b,r,T,S;
clrscr();
printf("Enter the value for altitude of a triangle: ");
scanf("%f",&a);
printf("\nEnter the value for base of a triangle: ");
scanf("%f",&b);
printf("\nEnter the value for radius of a sphere: ");
scanf("%f",&r);
T=(1.0/2.0)*(a*b);
S=(4.0/3.0)*(3.142)*(r*r*r);
printf("\n\nArea of triangle = %.2f",T);
printf("\nArea of sphere = %.2f",S);
getch();
}

Output
Enter the value for altitude of a triangle: 3.65
Enter the value for base of a triangle: 4.98
Enter the value for radius of a sphere: 9.29

Area of triangle = 9.09


Area of sphere = 3358.86
13

Flow Chart
14

Experiment No 7
Write a program to Print Square and cube of first ten prime numbers.
Algorithm
Step1. Start.
Step2. Repeat FOR p = 1 to 30
a. Set isPrime = 1
b. Repeat FOR d = 2 to p
a. IF p % d is equal to 0 then
b. Set isPrime = 0
c. IF isPrime is not equal to 0 then
d. Write p,pxp,pxpxp
[End of FOR Loop]
[End of FOR Loop]
Step3. Exit
Source Code
void main ()
{
int p, d, isPrime;
clrscr();
for(p=1;p<=30;++p)
{
isPrime=1;
for(d=2;d<p;++d)
if(p%d==0)
isPrime=0;
if (isPrime!=0)
printf ("\n%d\t%d\t%d ", p,p*p,p*p*p);
}
getch();
}
Output
2 3 5 7 11 13 17 19 23 29
2 4 8
3 9 27
5 25 125
7 49 343
11 121 1331
13 169 2179
17 289 4913
19 361 6859
23 529 12167
29 841 24389
15

Flow Chart

Write p, pxp,pxpxp
16

Experiment No 8
Write a program, to calculate the roots of a quadratic equation
ax2+bx+c=0.

Algorithm
Step1. Start.
Step2. Read a, b, c
Step3. Set xp = (-b)+(sqrt(pow(b,2.0)-(4.0*a*c)))/(2.0*a)
xn = (-b)-(sqrt(pow(b,2.0)-(4.0*a*c)))/(2.0*a)
Step4. Write xp, xn
Step5. Exit
Source Code
#include<math.h>
void main()
{
int a,b,c;
float xp,xn;
clrscr();
printf("\nEnter the value for a : \t");
scanf("%d",&a);
printf("\nEnter the value for b : \t");
scanf("%d",&b);
printf("\nEnter the value for c : \t");
scanf("%d",&c);
xp = (-b)+(sqrt(pow(b,2.0)-(4.0*a*c)))/(2.0*a);
xn = (-b)-(sqrt(pow(b,2.0)-(4.0*a*c)))/(2.0*a);
printf("\nThe roots of given equation are (%.2f,%.2f)",xp,xn);
getch();
}
Output
Enter the value for a : 5
Enter the value for b : 10
Enter the value for c : 25

The roots of given equation are (-22.94,-27.09)


17

Flow Chart
18

Experiment No 9
write a program that reads your name and print that name 10 times.

Algorithm
Step1. Start.
Step2. Read name
Step3. REPEAT i=1 TO 10
WRITE Name
END REPEAT
Step4. Exit

Source Code
#include <stdio.h>

void main() {
char Name[50];
int i;
clrscr();
printf("Enter your name: ");
scanf("%s", Name); // reads string until whitespace

for(i = 0; i < 10; i++) {


printf("%d. %s\n", i + 1, Name);
}

getch();
}

Output
Enter your name: ALi
1. ALi
2. ALi
3. ALi
4. ALi
5. ALi
6. ALi
7. ALi
8. ALi
9. ALi
10. ALi
19

Flow Chart
Start

READ Name

SET i =0

IF YE
PRINT Name
I < 10 S

NO

Stop
i +1
20

Experiment No 10
Write a program that calculate power of base without using power
function by taking input both.

Algorithm
Step1. Start.
Step2. Read num, p
Step3. Repeat FOR i = 1 to p
a. Set pow = pow * num
[End of FOR Loop]
Step4. Write pow
Step5. Exit

Source Code
void main()
{
double pow=1;
int i,num,p;
clrscr();
printf("Enter number : ");
scanf("%d",&num);
printf("\nEnter power : ");
scanf("%d",&p);
for(i=1;i<=p;i++)
{
pow*=num;
}
printf("\nAnswer is %.2f",pow);
getch();
}

Output
Enter number : 6
Enter base : 3

Answer is 216.00
21

Flow Chart
22

Experiment No 11
Write a program using for loop to print usual printing characters
corresponding to 32 to 127 ASCII code.

Algorithm
Step1. Start.
Step2. Repeat FOR a = 32 to 127
Write a, a
[End of FOR Loop]
Step3. Exit

Source Code
void main()
{
int a;
clrscr();
for(a=32;a<=127;a++)
{
printf("%3d=%c\t",a,a);
}
getch();
}

Output
32= 33=! 34=" 35=# 36=$ 37=% 38=& 39=' 40=( 41=)
42=* 43=+ 44=, 45=- 46=. 47=/ 48=0 49=1 50=2 51=3
52=4 53=5 54=6 55=7 56=8 57=9 58=: 59=; 60=< 61==
62=> 63=? 64=@ 65=A 66=B 67=C 68=D 69=E 70=F 71=G
72=H 73=I 74=J 75=K 76=L 77=M 78=N 79=O 80=P 81=Q
82=R 83=S 84=T 85=U 86=V 87=W 88=X 89=Y 90=Z 91=[
92=\ 93=] 94=^ 95=_ 96=` 97=a 98=b 99=c 100=d 101=e
102=f 103=g 104=h 105=i 106=j 107=k 108=l 109=m 110=n 111=o
112=p 113=q 114=r 115=s 116=t 117=u 118=v 119=w 120=x 121=y
122=z 123={ 124=| 125=} 126=~ 127=⌂

Flow Chart
23
24

Experiment No 12
Write a program, which uses a switch and break statements.

Algorithm
Step1. Start.
Step2. Read num1, num2
Step3. Read switch op
Step4. IF switch op = ‘+’
Write num1 + num2
IF switch op = ‘-’
Write num1-num2
IF switch op = ‘*’
Write num1 * num2
IF switch op = ‘/’
Write num1 / num2
Step5. ELSE
Write Unknown operator
Step6. Exit

Source Code
void main()
{
float num1=1.0,num2=1.0;
char op, c;
clrscr();
A:
printf("Type number, operator, number : ");
scanf("%f %c %f", &num1, &op, &num2);
switch(op)
{
case'+':
printf(" = %.2f",num1+num2);
break;
case'-':
printf(" = %.2f",num1-num2);
break;
case'*':
printf(" = %.2f",num1*num2);
break;
case'/':
printf(" = %.2f",num1/num2);
break;
default:
printf("\nUnknown operator");
}
25

printf(“\n Do you want to perform again y/n\t”);


scanf(“%c”,&q);
if(q==’y’)
goto A;
getch();
}

Output
Type number, operator, number : 12 + 7
= 19.00
Do you want to perform again y/n y
Type number, operator, number : 2 * 4
= 8.00
Do you want to perform again y/n y
Type number, operator, number : 8 - 7
= 1.00
Do you want to perform again y/n y
Type number, operator, number : 16 / 3
= 5.33
Do you want to perform again y/n n
26

Flow Chart
27

Experiment No 13
Write a program that calculates power of base without using power
function by taking input both.

Algorithm
Step1. Start.
Step2. Read num, p
Step3. Repeat FOR i = 1 to p
a. Set pow = pow * num
[End of FOR Loop]
Step4. Write pow
Step5. Exit

Source Code
void main()
{
double pow=1;
int i,num,p;
clrscr();
printf("Enter number : ");
scanf("%d",&num);
printf("\nEnter power : ");
scanf("%d",&p);
for(i=1;i<=p;i++)
{
pow*=num;
}
printf("\nAnswer is %.2f",pow);
getch();
}
Output
Enter number : 6
Enter base : 3

Answer is 216.00
28

Flow Chart
29

Experiment No 14

Write a program to calculate pay roll of an employee. Read the basic


pay from keyboard, calculate medical allowance as 15% of basic pay,
conveyance allowance as 20%, and house rent allowance as 45% of
basic pay. Calculate gross pay and net pay and print all.

Source Code
void main()
{
float BS,MA,CA,HRA,GPAY,NPAY,TAX=0;
clrscr();
printf("\nEnter the basic pay of employee\t");
scanf("%f",&BS);
MA=15*BS/100;
CA=20*BS/100;
HRA=45*BS/100;
GPAY=BS+MA+CA+HRA;
NPAY=GPAY-TAX;
printf("\nEmployee\'s basic salary is \t%0.2f",BS);
printf("\nEmployee\'s medical allowance is\t %0.2f",MA);
printf("\nEmployee\'s conveyance allowance is\t %0.2f",CA);
printf("\nEmployee\'s house rent allowance is\t %0.2f",HRA);
printf("\nEmployee\'s gross pay is \t%0.2f",GPAY);
printf("\nEmployee\'s net pay is \t %0.2f",NPAY);
getch();
}

Algorithm
Step 1 START
Step 2 READ BS
Step 3 SET TAX=0
Step 4 SET MA=15*BS/100
Step 5 SET CA=20*BS/100
Step 6 SET HRA=45*BS/100
Step 7 SET GPAY=BS+MA+CA+HRA
Step 8 SET NPAY=GPAY-TAX;
Step 9 WRITE BS
Step 10 WRITE MA
Step 11 WRITE HRA
Step 12 WRITE GPAY
Step 13 WRITE NPAY
Step14 END
30

Output
Enter the basic pay of employee 5000
Employee’s basic salary is 5000
Employee’s medical allowance is 750
Employee’s conveyance allowance 1000
Employee’s house rent allowance is 2250
Employee’s gross pay is 9000
Employee’s net pay is 9000

Flow Chart
Start

READ BS

SET MA=15*BS/100

SET CA=20*BS/100

SET HRA=45*BS/100

WRITE BS, MA,


CA, HRA, GPAY,
NPAY

End
31

Experiment No 15
Write a program using nested for loop to print to print the following.
*
***
*****
*******
*********
Algorithm
Step1. START
Step2. SET m=4
Step3. SET n=1
Step4. Repeat FOR y =1 to 4
a. REPEAT FOR x = 1 to m
b. WRITE space
END REPEAT
c. REPEAT FOR a = 1 to n
d. WRITE *
END REPEAT
e. SET n=n-2
f. WRITE new line
END REPEAT
Step5. END

Source Code

void main()
{
int a,x,y,m=4,n=1;
clrscr();
for(y=0;y<=4;y++)
{
for(x=1;x<=m;x++)
{
printf(" ");
}
for(a=1;a<=n;a++)
printf("*");

n=n+2;
m--;
printf("\n");
}
getch();
}
32

Output
*
***
*****
*******
*********

Flow Chart
33

Experimet No 16
Write a program using function that accepts two numbers as its
arguments and returns summation of these two numbers.

Algorithm
Step 1 START
Step 2 READ a
Step 3 READ b
Step 4 SET ans=sum(a,b)
Step 5 RETURN x+y
Step 6 WRITE ans
Step 7 END

Source Code
int sum(int,int);
void main()
{
int a,b,ans;
clrscr();
printf("\nEnter first number\t");
scanf("%d",&a);
printf("\nEnter second number\t");
scanf("%d",&b);
ans=sum(a,b);
printf("\nThe sum of the two numbers is \t%d",ans);
getch()

int sum(int x,int y)


{
return(x+y);
}

Output
Enter first number 35
Enter second number 25
The sum of the two numbers is 60
34

Flow Chart Start

READ a,b

SET ans=sum(a,b)

RETURN x+y

Write ans

End

You might also like