0% found this document useful (0 votes)
35 views36 pages

Lab Sheet 4

Uploaded by

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

Lab Sheet 4

Uploaded by

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

Lab sheet 2022

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

C programming: Grade: 9 Page:- 2


Lab sheet 2022

Flowchart:-

Start

Read:- Name, Address,


age_in_year, Weight, Height

per = (Math + Science + Nepali +


English + Social) /5

True
All subject Fail.
<40

False
per <= 100 True
&& per Extra
>=95 ordinary.

False

A
C programming: Grade: 9 Page:- 3
Lab sheet 2022

per <= True


95&& per Distinction.
>=80

False

per <= 80 True


&& per First.
>=65

False
True
per <= 65
&& per Second.
>=40

False
Display Percentage and
rank

Stop

C programming: Grade: 9 Page:- 4


Lab sheet 2022

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);

per = (Math + Science + Nepali + English + Social) / 5;

printf("\n****************\n\n");
printf("Percentage = %.2f\n", per);

C programming: Grade: 9 Page:- 5


Lab sheet 2022

if( Math<40 || Science<40 || Nepali<40 || English<40 ||


Social<40)
{
printf("Fail.");
}
else if(per <= 100 && per >=95)
{
printf("Extra ordinary.");
}
else if(per <= 95 && per >=80)
{
printf("Distinction.");
}
else if(per <= 80 && per >=65)
{
printf("First.");
}
else if(per <= 65 && per >=40)
{
printf("Second.");
}
return 0;
}

C programming: Grade: 9 Page:- 6


Lab sheet 2022

Output:-
Enter five subjects marks:
Math:94
Science:88
Nepali:75
English:86
Social:69

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

Percentage = 82.00
Distinction.

Discussion and conclusion:-


The program is focused on 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%).

C programming: Grade: 9 Page:- 7


Lab sheet 2022

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

C programming: Grade: 9 Page:- 8


Lab sheet 2022

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

C programming: Grade: 9 Page:- 9


Lab sheet 2022

The largest
else True number is
odd
False

Display Your input from


keyboard

Display Percentage and


rank

Stop

C programming: Grade: 9 Page:- 10


Lab sheet 2022

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;

C programming: Grade: 9 Page:- 11


Lab sheet 2022

}
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");
}
}

C programming: Grade: 9 Page:- 12


Lab sheet 2022

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

Discussion and conclusion:-


The program is focused to find the largest and smallest
among three entered numbers and also display
whether the identified largest / smallest number is
even or odd.

C programming: Grade: 9 Page:- 13


Lab sheet 2022

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

C programming: Grade: 9 Page:- 14


Lab sheet 2022

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

Display input alphabet is


vowel or consonant

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);

lowercase_vowel = (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c ==


'u');

uppercase_vowel = (c == 'A' || c == 'E' || c == 'I' || c == 'O' || c ==


'U');

if (lowercase_vowel || uppercase_vowel)
printf("%c is a vowel.", c);
else
printf("%c is a consonant.", c);
return 0;
}

C programming: Grade: 9 Page:- 16


Lab sheet 2022

Output:-
Enter an alphabet: U
U is a vowel.
AND
Enter an alphabet: F
F is a consonant.

Discussion and conclusion:-


The program is focused to check whether input alphabet is
vowel or not using if-else.

C programming: Grade: 9 Page:- 17


Lab sheet 2022

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

C programming: Grade: 9 Page:- 18


Lab sheet 2022

Flowchart:-

Start

Read reverse, n, remainder

False

n != 0

True

Display Your input from


keyboard

Display Reversed number

Stop

C programming: Grade: 9 Page:- 19


Lab sheet 2022

Program Code:-
#include <stdio.h>

int main()
{

int n, reverse = 0, remainder;

printf("Enter an integer: ");


scanf("%d", &n);

while (n != 0) {
remainder = n % 10;
reverse = reverse * 10 + remainder;
n /= 10;
}

printf("Reversed number = %d", reverse);

return 0;
}

C programming: Grade: 9 Page:- 20


Lab sheet 2022

Output:-
Enter an integer: 987
Reversed number = 789

Discussion and conclusion:-


The program is focused to get input of two or higher integer
number and display in reverse order.

C programming: Grade: 9 Page:- 21


Lab sheet 2022

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

C programming: Grade: 9 Page:- 23


Lab sheet 2022

A
Number is
Else
True not multiple
of 5 and 7
False

Display Your input from


keyboard

Display multiple of 5 or not,


divisible by 7 but not by 11

Stop

Program Code:-

C programming: Grade: 9 Page:- 24


Lab sheet 2022

#include <stdio.h>

int main()
{
int num;

printf("Enter any number: ");


scanf("%d", &num);

if((num % 5 == 0 ) && ( num % 7 ==0 ) && (num % 11 != 0))


{
printf("Number is multiple of 5 and 7 but not by 11");
}
else if (num % 11==0)
{
printf("Number is not multiple of 5 and 7 but , divisible by 11");
}
else
{
printf("Number is not multiple of 5 and 7 but not by 11");
}
return 0;
}
Output:-

C programming: Grade: 9 Page:- 25


Lab sheet 2022

Enter any number: 35


Number is multiple of 5 and 7 but not by 11

Enter any number: 55


Number is not multiple of 5 and 7 but , divisible by 11

Enter any number: 69


Number is not multiple of 5 and 7 but not by 11

Discussion and conclusion:-


The program is focused that asks a number and test he number
whether it is multiple of 5 or not, divisible by 7 but not by 11.

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

C programming: Grade: 9 A Page:- 28


Lab sheet 2022

year % 4 True %d is a leap


== 0
year.

False

True
else %d is not a
leap year.

False
Display Your input from
keyboard

Display the entered year


is leap year or not

Stop

Program Code:-

C programming: Grade: 9 Page:- 29


Lab sheet 2022

#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:-

C programming: Grade: 9 Page:- 30


Lab sheet 2022

Enter a year: 36
36 is a leap year.

Enter a year: 1000


1000 is not a leap year.

Enter a year: 400


400 is a leap year.

Enter a year: 55
55 is not a leap year.

Discussion and conclusion:-


The program is focused 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).

Question no:-7

C programming: Grade: 9 Page:- 31


Lab sheet 2022

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:-

C programming: Grade: 9 Page:- 32


Lab sheet 2022

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

C programming: Grade: 9 Page:- 33


Lab sheet 2022

True root1 =
else %.2lf+%.2lfi
and root2 =
%.2f-%.2fi
False
Display Your input from
keyboard

Display the entered year


is leap year or not

Stop

C programming: Grade: 9 Page:- 34


Lab sheet 2022

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);

C programming: Grade: 9 Page:- 35


Lab sheet 2022

imagPart = sqrt(-discriminant) / (2 * a);


printf("root1 = %.2lf+%.2lfi and root2 = %.2f-%.2fi", realPart,
imagPart, realPart, imagPart);
}
return 0;
}

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;

Discussion and conclusion:-


The program is focused to read the values of coefficients
a, b and c of a quadratic equation ax 2 + bx +_ c = and find the
root of the equation.

C programming: Grade: 9 Page:- 36

You might also like