Lab Sheet 4
Lab Sheet 4
Table of content
S. Question Page
N
1. ALL 2-36
2. 1 2-7
3. 2 8-13
4. 3 14-17
5. 4 18-21
6. 5 22-26
7. 6 27-31
8. 7 32-36
C programming: Grade: 9 Page:- 1
Lab sheet 2022
Question No 1
Title:
Write a program to input marks of 5 subjects for a
students. Display the rank of each subjects and also the
result of total marks and percentage obtained with his /
her rank in the class. The rank is categorized as fail
(marks < 40%). First (marks between 65 to 80%),
distinction (marks between 80 to 95%), extra ordinary
(marks above 95 to 100%).
Objection(s):
To familiar with syntax and structure of c-
programming.
To verify rank , percentage and solving techniques
using C.
Problem Analysis:-
Input variable: Name, Address, age_in_year,
Weight, Height
Calculation:-percentage
Output variable:-Your input from keyboard is:-
Algorithm:-
Step 1:-Start
Step 2:-Read:- Math , Science , Nepali , English , Social ,
per
Step 3:-per = (Math + Science + Nepali + English +
Social) /5
Step 4:Display Your input from keyboard
Step 5:-Display Percentage and rank
Step 6:-Stop
Flowchart:-
Start
True
All subject Fail.
<40
False
per <= 100 True
&& per Extra
>=95 ordinary.
False
A
C programming: Grade: 9 Page:- 3
Lab sheet 2022
False
False
True
per <= 65
&& per Second.
>=40
False
Display Percentage and
rank
Stop
Program Code:-
#include <stdio.h>
int main()
{
int Math,Science,Nepali,English,Social;
float per;
printf("Enter five subjects marks:\n ");
printf("Math:");
scanf("%d",&Math);
printf("\n Science:");
scanf("%d",&Science);
printf("\n Nepali:");
scanf("%d",&Nepali);
printf("\n English:");
scanf("%d",&English);
printf("\n Social:");
scanf("%d",&Social);
printf("\n****************\n\n");
printf("Percentage = %.2f\n", per);
Output:-
Enter five subjects marks:
Math:94
Science:88
Nepali:75
English:86
Social:69
****************
Percentage = 82.00
Distinction.
Question no:-2
Title:
Write a program to find the largest and smallest
among three entered numbers and also display
whether the identified largest / smallest number is
even or odd.
Objection(s):
To familiar with syntax and structure of c-
programming.
To verify rank and solving techniques using C.
Problem Analysis:-
Input variable: integer: a,b,c,min,max
Output variable:- largest / smallest number is
even or odd
Algorithm:-
Step 1:-Start
Step 2:-Read:- a,b,c,min,max
Step 3:Display Your input from keyboard
Step 4:-Display largest / smallest number is even or odd
Step 5:-Stop
Flowchart:-
Start
Read
Math,Science,Nepali ,Engli
sh,Social
The
min%2 True smallest
== 0 number is
even
False
The
True smallest
else
number is
odd
False
The largest
max%2 True number is
== 0 even
False
The largest
else True number is
odd
False
Stop
Program Code:-
#include<stdio.h>
#include<stdlib.h>
int main()
{
int a,b,c,min,max;
printf("Enter Three number :\n ");
scanf("%d%d%d",&a,&b,&c);
if(a < b&& a < c){
min = a;
}
else if (b < a && b < c){
min = b;
}
else{
min = c;
}
if(a > b && a > c){
max = a;
}
else if ( b > a && b > c){
max = b;
}
else{
max = c;
}
printf("**********");
printf("\nThe smallest number is : %d",min);
if(min%2 == 0){
printf("\n The smallest number is even");
}
else{
printf("\n The smallest number is odd");
}
printf("\nThe largest number is : %d",max);
if(max%2 == 0){
printf("\n The largest number is even");
}
else{
printf("\n The largest number is odd");
}
}
Output:-
Enter Three number :
78
89
99
**********
The smallest number is : 78
The smallest number is even
The largest number is : 99
The largest number is odd
Question no:-3
Title:
Write a program to check whether input alphabet
is vowel or not using if-else and switch statement.
Objection(s):
To familiar with syntax and structure of c-
programming.
To verify whether input alphabet is vowel or
consonant and solving techniques using C.
Problem Analysis:-
Input variable: integer: lowercase_vowel,
uppercase_vowel
Character: c
Output variable:- input alphabet is vowel or
consonant
Algorithm:-
Step 1:-Start
Step 2:-Read:- lowercase_vowel, uppercase_vowel, c
Step 3:Display Your input from keyboard
Step 4:-Display input alphabet is vowel or consonant
Step 5:-Stop
Flowchart:-
Start
Read lowercase_vowel,
uppercase_vowel, c
lowercase_ True
vowel || Vowel letter
uppercase_
False
True
Consonant
else
letter
False
Display Your input from
keyboard
Stop
C programming: Grade: 9 Page:- 15
Lab sheet 2022
Program Code:-
#include <stdio.h>
int main()
{
char c;
int lowercase_vowel, uppercase_vowel;
printf("Enter an alphabet: ");
scanf("%c", &c);
if (lowercase_vowel || uppercase_vowel)
printf("%c is a vowel.", c);
else
printf("%c is a consonant.", c);
return 0;
}
Output:-
Enter an alphabet: U
U is a vowel.
AND
Enter an alphabet: F
F is a consonant.
Question no:-4
Title:
Write a program to get input of two or higher
integer number and display in reverse order.
Objection(s):
To familiar with syntax and structure of c-
programming.
To verify get input of two or higher integer number
and display in reverse order and solving techniques
using C.
Problem Analysis:-
Input variable: integer: reverse, n, remainder
Output variable:- input alphabet is vowel or
consonant
Algorithm:-
Step 1:-Start
Step 2:-Read:- reverse, n, remainder
Step 3:Display Your input from keyboard
Step 4:-Display Reversed number
Step 5:-Stop
Flowchart:-
Start
False
n != 0
True
Stop
Program Code:-
#include <stdio.h>
int main()
{
while (n != 0) {
remainder = n % 10;
reverse = reverse * 10 + remainder;
n /= 10;
}
return 0;
}
Output:-
Enter an integer: 987
Reversed number = 789
Question no:-5
Title:
Write a program that asks a number and test he number
whether it is multiple of 5 or not, divisible by 7 but not by 11.
Objection(s):
To familiar with syntax and structure of c-
programming.
To verify get multiple of 5 or not, divisible by 7 but
not by 11 and solving techniques using C.
Problem Analysis:-
Input variable: integer: num
Output variable:- multiple of 5 or not, divisible
by 7 but not by 11.
Algorithm:-
Step 1:-Start
Step 2:-Read:- num
Step 3:Display Your input from keyboard
Step 4:-Display multiple of 5 or not, divisible by 7 but not
by 11
Step 5:-Stop
Flowchart:-
C programming: Grade: 9 Page:- 22
Lab sheet 2022
Start
Read:- num
Number is
True multiple of 5
(num % 5 == 0 )
&& ( num % 7 and 7 but not
==0 ) && (num by 11
% 11 != 0)
False
Number is
num %
True not multiple
11==0 of 5 and 7
but ,
divisible by
False 11
A
Number is
Else
True not multiple
of 5 and 7
False
Stop
Program Code:-
#include <stdio.h>
int main()
{
int num;
Question no:-6
C programming: Grade: 9 Page:- 26
Lab sheet 2022
Title:
Write a program to check whether the entered
year is leap year or not (a year is leap year if it is
divisible by 4 and divisible by 100 or 400).
Objection(s):
To familiar with syntax and structure of c-
programming.
To verify get is leap year or not and solving
techniques using C.
Problem Analysis:-
Input variable: integer: year
Output variable:- leap year or not
Algorithm:-
Step 1:-Start
Step 2:-Read:- year
Step 3:Display Your input from keyboard
Step 4:-Display the entered year is leap year or not
Step 5:-Stop
Flowchart:-
Start
C programming: Grade: 9 Page:- 27
Lab sheet 2022
Read:- year
True %d is a leap
year % 400
== 0 year
False
year % 100
True
%d is not a
== 0
leap year.
False
False
True
else %d is not a
leap year.
False
Display Your input from
keyboard
Stop
Program Code:-
#include <stdio.h>
int main()
{
int year;
printf("Enter a year: ");
scanf("%d", &year);
if (year % 400 == 0) {
printf("%d is a leap year.", year);
}
else if (year % 100 == 0) {
printf("%d is not a leap year.", year);
}
else if (year % 4 == 0) {
printf("%d is a leap year.", year);
}
else {
printf("%d is not a leap year.", year);
}
return 0;
}
Output:-
Enter a year: 36
36 is a leap year.
Enter a year: 55
55 is not a leap year.
Question no:-7
Title:
Write a program to read the values of coefficients
a, b and c of a quadratic equation ax2 + bx +_ c =
and find the root of the equation.
Objection(s):
To familiar with syntax and structure of c-
programming.
To verify get ax2 + bx +_ c = and find the root of the
equation and solving techniques using C.
Problem Analysis:-
Input variable: double : a, b, c, discriminant,
root1, root2, realPart, imagPart
Output variable:- the root of the equation
Algorithm:-
Step 1:-Start
Step 2:-Read:- a, b, c, discriminant, root1, root2, realPart,
imagPart
Step 3:Display Your input from keyboard
Step 4:-Display the root of the equation
Step 5:-Stop
Flowchart:-
Start
Read:- a, b, c, discriminant,
root1, root2, realPart,
imagPart
root1 =
%.2lf and
True root2 =
discriminant
>0 %.2lf",
root1, root2
False
root1 =
discriminan
True %.2lf+%.2lfi
t == 0
and root2 =
%.2f-%.2fi
False
True root1 =
else %.2lf+%.2lfi
and root2 =
%.2f-%.2fi
False
Display Your input from
keyboard
Stop
Program Code:-
#include <stdio.h>
#include <math.h>
int main()
{
double a, b, c, discriminant, root1, root2, realPart, imagPart;
printf("Enter coefficients a, b and c: ");
scanf("%lf %lf %lf", &a, &b, &c);
discriminant = b * b - 4 * a * c;
if (discriminant > 0) {
root1 = (-b + sqrt(discriminant)) / (2 * a);
root2 = (-b - sqrt(discriminant)) / (2 * a);
printf("root1 = %.2lf and root2 = %.2lf", root1, root2);
}
else if (discriminant == 0) {
root1 = root2 = -b / (2 * a);
printf("root1 = root2 = %.2lf;", root1);
}
else {
realPart = -b / (2 * a);
Output:-
Enter coefficients a, b and c: 88
66
33BU
root1 = -0.38+0.48i and root2 = -0.38-0.48i
And
Enter coefficients a, b and c: s
root1 = root2 = -1.#J;