Practical file Computer Science XII
Practical 1
Writing a Program that prints a Text of four lines consisting
of Characters, integers values and floating point values using
printf statement.
Algorithm
Step 1. Write character.
Step 2. Write string.
Step 3. Write integer.
Step 4. Write floating point number.
Step 5. Exit.
Flowchart
Start
Write
character, string, integer,
floating point number
Stop
Department of Computer Science
1|Page
Practical file Computer Science XII
Source Code
#include<conio.h>
#include<stdio.h>
void main(void)
{
char ch = 'A', str[] = "My College";
int num = 2;
float fnum = 12.47;
clrscr();
printf("Character = %c\n", ch);
printf("String = %s\n", str);
printf("Integer = %d\n", num);
printf("float = %f\n", fnum);
getch();
}
Output
Character = A
String = My College
Integer = 2
float = 12.470000
Department of Computer Science
2|Page
Practical file Computer Science XII
Practical 2
Writing a Program that reads and prints the data using escape
sequence (asking the name, age, height and gender of student
using scanf and printf statement).
Algorithm
Step 6. Read gender.
Step 7. Read name.
Step 8. Read age.
Step 9. Read height.
Step 10. Write name.
Step 11. Write age.
Step 12. Write height.
Step 13. Write gender.
Step 14. Exit.
Flowchart
Start
Read
gender, name, age,
height
Write
name, age, height,
gender
Stop
Department of Computer Science
3|Page
Practical file Computer Science XII
Source Code
#include<conio.h>
#include<stdio.h>
void main(void)
{
char gender, name[25];
int age;
float height;
clrscr();
printf("\nEnter Your Gender(M/F): ");
scanf("%c", &gender);
printf("\nEnter Your Name: ");
scanf("%s", name);
printf("\nEnter Your Age (in years): ");
scanf("%d", &age);
printf("\nEnter Your Height (in ft.): ");
scanf("%f", &height);
clrscr();
printf("\n\tName: %s", name);
printf("\n\tAge: %d", age);
printf("\n\tHeight: %0.2f", height);
printf("\n\tGender: %c", gender);
getch();
}
Output
Name: Ikram_Azam
Age: 45
Height: 5.75
Gender: M
Department of Computer Science
4|Page
Practical file Computer Science XII
Practical 3
Writing a Program which uses operator (calculate the area of
triangle, volume of sphere and arrange the resultant values in
ascending order).
Algorithm
1. Read a, b, r, t, s.
1
2. Set Area of Triangle t a b.
2
4
Area of Sphere s r3 .
3
3. IF t < s, then
Write t, s.
ELSE
Write s, t.
[End of IF-ELSE structure.]
4. Exit.
Flowchart
Start
Read
a, b, r, t, s
Area of Triangle
Area of Sphere
No
If t < s?
Yes
Write t, s Write s, t
Stop
Department of Computer Science
5|Page
Practical file Computer Science XII
Source Code
#include<stdio.h>
#include<conio.h>
#include<math.h>
#define pi 3.1415927
void main(void)
{
float a, b, r, t, s;
clrscr();
printf("\n\n\t\tEnter the value for Altitude of a Triangle. ");
scanf("%f", &a);
printf("\n\n\t\tEnter the value for Base of a Triangle. ");
scanf("%f", &b);
printf("\n\n\t\tEnter the value for Radius of a Sphere. ");
scanf("%f", &r);
t=(1.0/2.0)*(a*b);
s=(4.0/3.0)*pi*pow(r,3);
if (t<s)
{
printf("\n\n\t\tArea of Triangle = %.2f",t);
printf("\n\n\t\tArea of Sphere = %.2f",s);
}
else
{
printf("\n\n\t\tArea of Sphere = %.2f",s);
printf("\n\n\t\tArea of Triangle = %.2f",t);
}
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.43
Department of Computer Science
6|Page
Practical file Computer Science XII
Practical 4
Writing a Program which uses ‘for’ loop statement, (generate
the multiplication tables from 2 to 20).
Algorithm
Step 15. Set j = 0.
Step 16. Read n.
Step 17. IF n >= 2 and n <= 20, then
Step 18. Repeat FOR i = 1 to 10
a. Set j = n i.
b. Write j.
[End of FOR loop.]
Step 19. ELSE
Write Error!!! Enter number from 2 to 20.
[End of IF-ELSE structure.]
Step 20. Exit.
Flowchart
No
If i<=10?
Start
Yes
j=ni
j=0
Write j
Read n
i=i1
No
If n>=2 and A
n<=20?
A
Yes
Write Error!!! Enter number from 2
i=1 to 20
Stop
Department of Computer Science
7|Page
Practical file Computer Science XII
Source Code
#include<stdio.h>
#include<conio.h>
void main (void)
{
int i,n,j=0;
clrscr();
printf("\n\t\tEnter Table No. ");
scanf("%d",&n);
if(n>=2 && n<=20)
for(i=1;i<=10;i++)
{
j=n*i;
printf("\n\t\t%d * %d = %d",n,i,j);
}
else
printf("\nError!!! Enter number from 2 to 20");
getch();
}
Output
Enter Table No. 7
7 * 1 = 7
7 * 2 = 14
7 * 3 = 21
7 * 4 = 28
7 * 5 = 35
7 * 6 = 42
7 * 7 = 49
7 * 8 = 56
7 * 9 = 63
7 * 10 = 70
Department of Computer Science
8|Page
Practical file Computer Science XII
Practical 5(a)
Writing a program which uses ‘while’ loop and nested while
loop, (use ‘for’ loop and continue the process in ‘while’ loop
satisfying this condition i.e. write a program to calculate
average marks of a student using while and nested while loop.
Algorithm
Step 21. Set count = 1 and sum = 0.
Step 22. Read n. [number of subjects.]
Step 23. Repeat WHILE count n
a. Read marks of n subjects.
b. Repeat WHILE marks are < 0 or > 100
i. Write Invalid marks, try again.
ii. Read marks.
[End of inner WHILE loop.]
c. sum = sum + marks.
d. count = count + 1.
[End of outer WHILE loop.]
Step 24. average = sum/n.
Step 25. Write average.
Step 26. Exit.
Department of Computer Science
9|Page
Practical file Computer Science XII
Flowchart Start
count=1, sum=0
Read n
If countn?
No
Yes
Read
marks of n subjects
If
marks<0 or marks>100? No
Yes
Write
Invalid marks, try again
Read marks
sum=sum+marks
count=count+1
average=sum/n
Write average
Stop
Department of Computer Science
10 | P a g e
Practical file Computer Science XII
Source Code
#include<stdio.h>
#include<conio.h>
void main(void)
{
clrscr();
int n, count = 1;
float marks, average, sum = 0;
// initialise and read in a value for n
printf("How many Subjects? ");
scanf("%d", &n);
printf("\n");
// read marks
while(count <= n)
{
printf("Marks in Subject %d = ", count);
scanf("%f", &marks);
while(marks < 0 ¦¦ marks > 100)
{
printf("Invalid marks, try again: ");
scanf("%f", &marks);
}
sum += marks;
++count;
}
// calculate the average and print the answer
average = sum/n;
printf("\nThe average marks are = %f", average);
getch();
}
Output
How many Subjects? 5
Marks in Subject 1 = 69
Marks in Subject 2 = 58.5
Marks in Subject 3 = 37
Marks in Subject 4 = 83
Marks in Subject 5 = 65
The average marks are = 62.500000
Department of Computer Science
11 | P a g e
Practical file Computer Science XII
Practical 5(b)
Writing a program which uses ‘while’ loop and nested while
loop, (use ‘for’ loop and continue the process in 'while' loop
satisfying this condition i.e. write a program to calculate
average marks of a student using for and nested while loop.
Algorithm
Step 27. Set sum = 0.
Step 28. Read n. [number of subjects.]
Step 29. Repeat FOR count = 1 to n
e. Read marks of n subjects.
f. Repeat WHILE marks are < 0 or > 100
i. Write Invalid marks, try again.
ii. Read marks.
[End of WHILE loop.]
g. sum = sum + marks.
[End of FOR loop.]
Step 30. average = sum/n.
Step 31. Write average.
Step 32. Exit.
Department of Computer Science
12 | P a g e
Practical file Computer Science XII
Flowchart
Start
sum=0
Read n
count=1
average=sum/n
If countn? No
1
Yes
Read Write average
marks of n subjects
If
marks<0 or No
marks>100?
Stop
Yes
Write
Invalid marks, try again
Read marks
sum=sum+marks
count=count+1
Department of Computer Science
13 | P a g e
Practical file Computer Science XII
Source Code
#include<stdio.h>
#include<conio.h>
void main(void)
{
clrscr();
int n, count;
float marks, average, sum = 0;
// initialise and read in a value for n
printf("How many Subjects? ");
scanf("%d", &n);
printf("\n");
// read marks
for(count =1; count <= n; count++)
{
printf("Marks in Subject %d = ", count);
scanf("%f", &marks);
while(marks < 0 ¦¦ marks > 100)
{
printf("Invalid marks, try again: ");
scanf("%f", &marks);
}
sum += marks;
}
// calculate the average and print the answer
average = sum/n;
printf("\nThe average marks are = %f", average);
getch();
}
Output
How many Subjects? 5
Marks in Subject 1 = 69
Marks in Subject 2 = 58.5
Marks in Subject 3 = 37
Marks in Subject 4 = 83
Marks in Subject 5 = 65
The average marks are = 62.500000
Department of Computer Science
14 | P a g e
Practical file Computer Science XII
Practical 6
Finding the Factorial of n using ‘while’ loop, read value of n
using scanf, and print the factorial of n.
Algorithm
Step 33. Set f = 1 and i = 1.
Step 34. Read num.
Step 35. IF num 0 then goto Step 4.
ELSE goto Step 6.
Step 36. Repeat WHILE i num
h. f = f * i.
i. i = i + 1.
Step 37. Write f.
Step 38. ELSE
Write Sorry!!
Step 39. Exit.
Start
Flowchart
f = 1, i = 1
Read num
No
If num0?
Yes
Write
If inum? Sorry!!
Yes
f=f*i
i=i1
Write f
Stop
Department of Computer Science
15 | P a g e
Practical file Computer Science XII
Source Code
#include<stdio.h>
#include<conio.h>
void main(void)
{
clrscr();
double f=1;
int num, i;
printf("\n\n\n\t\tEnter the number to find its Factorial: ");
scanf("%d", &num);
if (num>=0)
{
i = 1;
while (i<=num)
{
f=f*i;
i++;
}
printf("\n\n\n\t\tThe factorial of %d is %lf", num, f);
}
else
printf("\n\n\n\t\t Sorry!!");
getch();
}
Output
1st Run
Enter the number to find its Factorial: 4
The factorial of 4 is 24
2nd Run
Enter the number to find its Factorial: 75
The factorial of 75 is
2.480914081139539140000000000000000000000e+109
3rd Run
Enter the number to find its Factorial: -7
Sorry!!
4th Run
Enter the number to find its Factorial: 0
The factorial of 0 is 1
Department of Computer Science
16 | P a g e
Practical file Computer Science XII
Practical 7(a)
Draw a Check-board and print it using if-else statement, and
extend the program using nested if-else.
Algorithm
Step 40. Read a, b.
Step 41. Repeat FOR b = 1 to b < 12
Step 42. Repeat FOR a = 1 to a < 12
a. IF (a+b)(mod 2) = 0, then
Write \xDB.
b. ELSE
Write one blank space.
[End of IF-ELSE structure.]
[End of inner FOR loop.]
Write new line.
[End of outer FOR loop.]
Step 43. Exit.
Department of Computer Science
17 | P a g e
Practical file Computer Science XII
Flowchart
Start
Read a, b
b=1
No
If b<12? Stop
Yes
a=1
No
If a<12?
Yes
If (a+b)(mod2)=0?
No
Yes
Write Write
one blank space \xDB
a=a1
Write new
line
b=b1
Department of Computer Science
18 | P a g e
Practical file Computer Science XII
Source Code
#include<stdio.h>
#include<conio.h>
void main (void)
{
clrscr();
int a, b;
for(b=1; b<12; b++) /* stepping down */
{
for(a=1; a<12; a++) /* stepping right */
if((a+b)%2 == 0) /* even numbered square */
printf("\xDB"); /* print filled square */
else
printf(" "); /* print blank square */
printf("\n"); /* new line */
}
getch();
}
Output
█ █ █ █ █ █
█ █ █ █ █
█ █ █ █ █ █
█ █ █ █ █
█ █ █ █ █ █
█ █ █ █ █
█ █ █ █ █ █
█ █ █ █ █
█ █ █ █ █ █
█ █ █ █ █
█ █ █ █ █ █
Department of Computer Science
19 | P a g e
Practical file Computer Science XII
Practical 7(b)
Extend the Check-board using nested if-else statement.
Algorithm
Step 1. Read a, b.
Step 2. Repeat FOR b = 1 to b < 12
Step 3. Repeat FOR a = 1 to a < 12
c. IF a = b, then
Write \xB0.
d. ELSE IF (a+b)(mod 2) = 0, then
Write \xDB.
e. ELSE
Write one blank space.
[End of IF-ELSE-IF structure.]
[End of inner FOR loop.]
Write new line.
[End of outer FOR loop.]
Step 4. Exit.
Department of Computer Science
20 | P a g e
Practical file Computer Science XII
Flowchart
Start
Read a, b
b=1
No
If b<12? Stop
Yes
a=1
No
If a<12?
Yes
Write Yes
\xB0 If a=b?
No
If (a+b)(mod2)=0?
Write Yes
\xDB
No
Write
one blank space
a=a1
Write
new line
b=b1
Department of Computer Science
21 | P a g e
Practical file Computer Science XII
Source Code
#include<stdio.h>
#include<conio.h>
void main (void)
{
clrscr();
int a, b;
for(b=1; b<12; b++) /* stepping down */
{
for(a=1; a<12; a++) /* stepping right */
if(a==b) /* left top diagonal */
printf("\xB0"); /* print grey */
else if((a+b)%2==0)
printf("\xDB"); /* print filled square */
else
printf(" "); /* print blank square */
printf("\n"); /* new line */
}
getch();
}
Output
▒ █ █ █ █ █
▒ █ █ █ █
█ ▒ █ █ █ █
█ ▒ █ █ █
█ █ ▒ █ █ █
█ █ ▒ █ █
█ █ █ ▒ █ █
█ █ █ ▒ █
█ █ █ █ ▒ █
█ █ █ █ ▒
█ █ █ █ █ ▒
Department of Computer Science
22 | P a g e
Practical file Computer Science XII
Practical 8
Writing a Program which uses a switch statement and breaks the
program if certain condition is observed i.e. writing a
Program which uses a switch, case and break statements and
interconvert the temperature scales using Menu options.
Algorithm
Step 1. Read switch variable.
Step 2. IF switch variable = 1
j. Read c.
9
k. Convert Celsius to Fahrenheit using f 32 c.
5
IF switch variable = 2
l. Read f.
5
m. Convert Fahrenheit to Celsius using c (f 32) .
9
IF switch variable = 3
n. Read c.
o. Convert Celsius to Kelvin using k c 273.15 .
IF switch variable = 4
p. Read f.
5
q. Convert Fahrenheit to Kelvin using k (f 32) 273.15 .
9
Step 3. ELSE
Write Invalid switch variable.
Step 4. Exit.
Department of Computer Science
23 | P a g e
Practical file Computer Science XII
Flowchart
Start
Read
switch variable
case 1
from Celsius to Yes Read 9
Fahrenheit Yes c f 32 c
5
No
case 2
from Fahrenheit to Yes Read 5
Celsius Yes f c (f 32)
9
No
case 3
from Celsius to Kelvin Yes Read
Yes c k c 273.15
No
case 4
from Fahrenheit to Yes Read 5
Kelvin Yes f k (f 32) 273.15
9
No
Write
Invalid switch variable
Stop
Department of Computer Science
24 | P a g e
Practical file Computer Science XII
Source Code
#include<stdio.h>
#include<conio.h>
void main(void)
{
clrscr();
int i;
float c, f, k;
printf("\n\n\n");
printf("\t\t\tMENU FOR TEMPERATURE SCALE CONVERSION");
printf("\n\t\t\t_____________________________________\n\n");
printf("1. From Celsius to Fahrenheit scale\n");
printf("2. From Fahrenheit to Celsius scale\n");
printf("3. From Celsius to Kelvin scale\n");
printf("4. From Fahrenheit to Kelvin scale\n");
printf("\nEnter the MEMU option: ");
scanf("%d", &i);
switch(i)
{
case 1:
printf("Enter temperature in Celsius: ");
scanf("%f", &c);
f = 32.0+(9.0/5.0)*c;
printf("The temperature in Fahrenheit is = %.2f", f);
break;
case 2:
printf("Enter temperature in Fahrenheit: ");
scanf("%f", &f);
c = 5.0/9.0*(f-32.0);
printf("The temperature in Celsius is = %.2f", c);
break;
case 3:
printf("Enter temperature in Celsius: ");
scanf("%f", &c);
k = c + 273.15;
printf("The temperature in Kelvin is = %.2f", k);
break;
case 4:
printf("Enter temperature in Fahrenheit: ");
scanf("%f", &f);
k = (5.0/9.0*(f-32.0)) + 273.15;
printf("The temperature in Kelvin is = %.2f", k);
break;
default:
printf("Invalid MENU option\n\n");
}
getch();
}
Department of Computer Science
25 | P a g e
Practical file Computer Science XII
Output
MENU FOR TEMPERATURE SCALE CONVERSION
1. From Celsius to Fahrenheit scale
2. From Fahrenheit to Celsius scale
3. From Celsius to Kelvin scale
4. From Fahrenheit to Kelvin scale
Enter the MEMU option: 2
Enter temperature in Fahrenheit: 145.3
The temperature in Celsius is = 62.94
Department of Computer Science
26 | P a g e
Practical file Computer Science XII
Practical 9
Writing a function, which generates factorial of n and calls
this function in the ‘main’ program.
Algorithm
Step 1. Declare a function double factorial(int x), which accepts an
integer as an argument and returns a double value.
The Function main
Step 2. Read num.
Step 3. IF num 0, then
Call the function factorial(num) and store the value it returns in
fact.
(where num is the actual argument)
Write fact.
ELSE
Write Sorry!!
[End of IF-ELSE structure.]
Step 4. Exit.
Defining the Function double factorial(int x)
(where x is the formal argument)
Step 5. Set f = 1 and i = 1.
Step 6. Repeat FOR i =1 to i x
f = f * i.
[End of FOR loop.]
Step 7. Return f.
Flowchart
Start factorial(int x)
f = 1, i = 1
Read
num
No
If ix?
No
If num0?
Yes
Yes
f=f*i
fact = Write
call factorial(num) Sorry!!
i=i1
Write fact
return(f)
Stop
Stop
Department of Computer Science
27 | P a g e
Practical file Computer Science XII
Source Code
#include<stdio.h>
#include<conio.h>
double factorial(int x); /* function prototype */
void main(void)
{
clrscr();
int num;
double fact;
printf("\n\n\n\t\tEnter the number to find its Factorial: ");
scanf("%d", &num);
if (num>=0)
{
fact = factorial(num);
printf("\n\n\n\t\tThe factorial of %d is %lf", num, fact);
}
else
printf("\n\n\n\t\t Sorry!!");
getch();
}
double factorial(int x) /* factorial function */
{
int i;
double f=1;
for(i=1; i<=x; i++)
f=f*i;
return(f); /* return function */
}
Output
1st Run
Enter the number to find its Factorial: 4
The factorial of 4 is 24.000000
2nd Run
Enter the number to find its Factorial: 75
The factorial of 75 is
2.480914081139539140000000000000000000000e+109
3rd Run
Enter the number to find its Factorial: -7
Sorry!!
4th Run
Enter the number to find its Factorial: 0
The factorial of 0 is 1.000000
Department of Computer Science
28 | P a g e
Practical file Computer Science XII
Practical 10
Writing a program which uses multiple arguments in a function.
(Develop a user-defined function to generate a rectangle. Use
the function for passing arguments to draw different sizes of
rectangles and squares).
Algorithm
Step 1. Declare a function void rectangle(int, int), which accepts two
integer arguments but returns no value.
The Function main
Step 2. Write Drawing room.
Step 3. Call function rectangle(6,3).
(6 and 3 are length and width of the room)
Step 4. Write Bedroom.
Step 5. Call function rectangle(8,2).
Step 6. Write Bathroom.
Step 7. Call function rectangle(2,1).
Step 8. Write Kitchen.
Step 9. Call function rectangle(4,3).
Step 10. Exit.
Defining the Function void rectangle(int length, int width)
Step 11. Repeat FOR j = 1 to j <= width
Step 12. Repeat FOR k = 1 to k <= length
Write \xDB.
(where \xDB is the code for one rectangle)
[End of inner FOR loop.]
Write new line.
[End of outer FOR loop.]
Step 13. Exit.
Department of Computer Science
29 | P a g e
Practical file Computer Science XII
Flowchart
Start rectangle(int length, int width)
Write Drawing room j=1
call function
rectangle(6,3) No
Stop If j<=width?
Write Yes j=j1
Bedroom
k=1
call function Write
rectangle(8,2) new line
If k<=length? No
Write Bathroom
Yes
call function
rectangle(2,1) Write
\xDB
Write Kitchen
k=k1
call function
rectangle(4,3)
Stop
Department of Computer Science
30 | P a g e
Practical file Computer Science XII
Source Code
#include<stdio.h>
#include<conio.h>
void rectangle(int length, int width); /* function prototype */
void main(void)
{
clrscr();
printf("\nDrawing room\n"); /* print room name */
rectangle(6,3); /* draw room */
printf("\nBedroom\n"); /* etc. */
rectangle(8,2);
printf("\nBathroom\n");
rectangle(2,1);
printf("\nKitchen\n");
rectangle(4,3);
getch();
}
/* function draws rectangle of particular length and width */
void rectangle(int length, int width)
{
int j,k;
for(j=1; j<=width; j++) /* number of lines */
{
for(k=1; k<=length; k++) /* line of rectangles */
printf("\xDB"); /* print one rectangle */
printf("\n"); /* new line */
}
}
Output
Drawing room
Bedroom
Bathroom
Kitchen
Department of Computer Science
31 | P a g e