Control Structure and Branching
Control Structure and Branching
Control Structure and Branching
)Week Day
Write a C program to enter the week number (1-7) and print the day of week name using switch
case.
Sample Input 1:
Enter week number(1-7): 6
Sample Output 1:
FRIDAY
Sample Input 2:
Enter week number(1-7): 9
Sample Output 2:
Invalid input! Please enter week number between 1-7.
Sample Input 3:
Enter week number(1-7): 1
Sample Output 3:
SUNDAY
2.)Eldest and Youngest
Take the age of 3 close friends Raj, Shyam and Ram as input and determine the oldest and
youngest among them using a program.
Note: Age should be greater than 0 and less than or equal to 150, else display "Invalid Age" and
terminate the program.
Sample Input 1:
Enter the age of Ram: 15
Enter the age of Raj: 20
Enter the age of Shyam: 25
Sample Output 1:
The eldest person is Shyam
The youngest person is Ram
Sample Input 2:
Enter the age of Ram: 23
Enter the age of Raj: 23
Enter the age of Shyam: 23
Sample Output 2:
All are in the same age
Sample Input 3:
Enter the age of Ram: 20
Enter the age of Raj: 14
Enter the age of Shyam: 14
Sample Output 3:
The eldest person is Ram
Raj and Shyam are younger than Ram
Sample Input 4:
Enter the age of Ram:151
Sample Input 5:
Enter the age of Ram: 20
Sample Output 5:
Invalid Age
Sample Input 6:
Sample Output 6:
Ram and Shyam are elder than Raj
The youngest person is Raj
3.)Grading System
A school has the following rules for grading system:
a. Below 25 - F
b. 25 to 45 - E
c. 45 to 50 - D
d. 50 to 60 - C
e. 60 to 80 - B
f. Above 80 - A
Ask user to enter marks and print the corresponding grade.
Sample Input 1:
Enter marks
20
Sample Output 1:
Grade is:F
Sample Input 2:
Enter marks
-56
Sample Output 2:
Enter valid marks
Sample Input 3:
Enter marks
101
Sample Output 3:
Enter valid marks
4.)Coding Contest
Ram participates in a coding contest, where he got a program in which he needs to get a number
from user and swap the first and last digit of the given number.
Since he got confused with the logic of the program, he seeks help from the online forum. How
will you help Ram to win the contest?
Sample Input 1:
Enter any number: 12345
Sample Output 1:
Original number = 12345
Number after swapping first and last digit: 52341
Sample Input 2:
Enter any number: -55
Sample Output 2:
Invalid number!! Number should be a positive integer
Sample Input 3:
Enter any number: 0
Sample Output 3:
Invalid number!! Number should be a positive integer
Sample Input 4:
Enter any number: 6
Sample Output 4:
Original number = 6
Sample Input 1:
Enter the age:
50
Enter the income:
250000
Sample Output 1:
The Tax amount is: 0.00
Sample Input 2:
Enter the age:
40
Enter the income:
500000
Sample Output 2:
The Tax amount is: 50000.00
Sample Input 3:
Enter the age:
17
Sample Output 3:
Invalid Age
Sample Input 4:
Enter the age:
101
Sample Output 4:
Invalid Age
Sample Input 5:
Enter the age:
90
Sample Output 5:
The Tax amount is: 100000.20
6.)BMI Calculator
Health-Risk assessment:(health-care domain)
A Multi-specialty hospital wants to compute the BMI of a person and calculate the risk associated
with cardiovascular diseases. The given table denotes the range of BMI and its risks. Help the
management to write a program.
Hint: BMI = weight(kg)/( height(m)*height(m) ). Result should be given with respect to one
decimal place.
BMI Risk
27.5 and
High Risk
above
Sample input 1:
Enter the weight of the person(kg):85
Enter the height of the person(m):1.75
Sample output 1:
Your BMI is 27.8 (High Risk).
Sample input 2:
Enter the weight of the person(kg):0
Enter the height of the person(m):1.58
Sample output 2:
Provide a valid input
Sample input 3:
Enter the weight of the person(kg):80
Enter the height of the person(m):-1
Sample output 3:
Provide a valid input
7.)Time for a Task
A task is given to 3 persons to complete it within a particular time. If the person exceeds the time
limit he will be disqualified. Only those who complete it within the given time limit is qualified.
Among the qualified persons, the person who completes the task first will be rewarded.
Write a C program to find the person who is rewarded.
CGPA LAB
Input format :
Input consists of day, month, year of a date.
Output format :
Output consists of day, month, year of next date to the input date.
Magnitude Type
8.0 or Great
greater
Input Statements
The first line of the input contains an integer which corresponds to the number of the month. [
January is 1, Feb is 2 and so on].
The second line of the input consists of a floating point number which corresponds to the room
rent per day.
The third line of the input consists of an integer which corresponds to the number of days stayed
in the hotel.
Output Statements:
Output consists of the hotel tariff to be payed and it should be displayed correct to 2 decimal
places.
Sample Input 1:
Enter the number of the month: 3
Room rent per day: 1500
No. of days stayed: 2
Sample Output 1:
Hotel Tariff: Rs.3000.00
Sample Input 2:
Enter the number of the month:4
Room rent per day:2000
No. of days stayed:2
Sample Output 2:
Hotel Tariff: Rs.4800.00
Sample Input 3:
Enter the number of the month:14
Sample Output 3:
Invalid Input
13.)Age Calculator
As a part of C assessment, the students of Time Engineering College were asked to write a C
program to calculate the age of a person when the user gives their birth year and the current
year. Both the years should be encoded as the last two digits. For example, if the birth year is
1990, then the user will enter only "90"
The program will have to determine, if a two digit value such as "62" corresponds to a year in the
20th century ("1962") or the 21st century ("2062"). If the user gives "00", this will be considered
as year 2000.
The age of the person could be 6 or 106 depending on the assumptions. Assume that the age
will always be less than or equal to 100.
Input Format:
Input consists of 2 integers. The first integer corresponds to the last 2 digits of the birth year. The
second integer corresponds to the last 2 digits of the current year.
Output Format:
Refer sample input and output for further formatting specifications.
Test Case
Input 1
67
Output 1
Hurrah! Go to the Angel House.
Input 2
-78
Output 2
OOPS! Go to the Ghost House.
15.)Promote Dany's Cloth Store
Dany is an owner of Kurinji Textiles, a popular cloth store in your area. For the New Year he
wants to promote his store but at the same time he does not want to spend extra money on
presenting gifts. So he gives gift in a complex manner.
a) Presents gift to the customer whose birthday falls on a leap year.
b) Provides Special Discounts to others.
Help him to find the person whose year of birth falls on a leap year or not.
TestCase
Input 1
Enter your name
Sangeetha
Enter the year of birth
1990
Output 1
Thank You For Shopping with us Sangeetha! You will get a special discount from us.
Input 2
Enter your name
Micky
Enter the year of birth
1996
Output 2
Thank You For Shopping with us Micky! You will get a surprise gift from us.
Input 3
Enter your name
Poliano
Enter the year of birth
-2017
Output 3
Please give a valid year!
Input 4
Enter your name
Johny
Enter the year of birth
0000
Output 4
Please give a valid year!
16.)Inclinometer
Mr.Anderson, captain of the Greenland ship was sailing with his workers in the Atlantic Ocean.
He wants to know the directions with the angle’s 0, 90,180,270 indicate East, West, North and
South respectively. Write a C program to denote the direction graphically corresponding to the
angle entered.
Hint :
0 - 90 indicates East
91 - 180 indicates West
181 - 270 indicates North
271 - 359 indicates South
[Let's consider the angle which has entered is greater than 360 is Invalid.]
TestCase
Input 1
Enter the angle :
99
Output 1
99 corresponds to West
Input 2
Enter the angle :
375
Output 2
Invalid angle
Input 3
Enter the angle:
360
Output 3
360 corresponds to East
17.)Room Temperature
Robin and Jack were worried because they have shifted to a new house. As usual there was
confusion in the house as the AC in their room was not working. They had to stay at some other
room that night. Out of the two rooms, one belongs to their parents and the other belongs to their
sister. Jack feels cold so he prefers a warm room to sleep. Given the room temperature of the
rooms, help them to select their room.Assume the minimum room temperature is 18. If the
temperature is less than 18 or less than zero then should display "The temperature should not be
less than 18."
Sample Input 1
First Room Temperature :
23
Second Room Temperature :
18
Sample Output 1
Robin must stay in the second room and Jack must stay in the first room.
Sample Input 2
First Room Temperature :
18
Second Room Temperature :
18
Sample Output 2
Robin and Jack can stay in any room.
Sample Input 3
First Room Temperature :
20
Second Room Temperature :
25
Sample Output 3
Robin must stay in the first room and Jack must stay in the second room.
18.)Window Seat or not
Sujith is going to Goa this weekend. As the train tickets are unavailable, he decides to go by bus.
He books his tickets through a website. Write a program to find whether he booked a window
seat or not.
Assume that there are maximum 11 rows in a bus.
If the input is less than 1 then display "Invalid Seat Number"
Assuming there are 3 seats in a row, display "Invalid Seat Number" if the user enters a value
greater than 33.
TestCase
Input 1
Enter number of seats per row
4
Enter the seat number
36
Output 1
Window Seat
Input 2
Enter number of seats per row
3
Enter the seat number
20
Output 2
Aisle Seat
Input 3
Enter number of seats per row
4
Enter the seat number
48
Output 3
Invalid Seat Number
19.)Choose Vehicle Type
When Tintu was about to step out from her house, it started raining heavily. So she plans to go
either by a taxi or by her personal car. She can always call Raghav, an on call driver who lives
close to her house. He normally charges Rs.X for an intercity trip. Her Audi car provides a
mileage of Y kms/litre and the petrol costs Rs. Z per litre. She can also hire a taxi. The taxi fares
are a fixed at Rs.50 for the 1st 3 Km and there on it is Rs.W per Km. The distance between her
home and the Brook Fields Shopping mall is V km. She wants to spend a minimal amount on
travel. Help her to decide whether to hire a taxi or to use her Audi.
Display "You have entered a wrong input" if the given input is less than zero.
TestCase
Input 1
Enter the driver salary
200
Enter the car mileage in km per litre
10
Enter the cost of petrol per litre
60
Enter the taxi fare for a km
20
Enter the distance of travel
10.5
Output 1
Minimal cost travel is by taxi
Input 2
Enter the driver salary
250
Enter the car mileage in km per litre
55
Enter the cost of petrol per litre
75
Enter the taxi fare for a km
8
Enter the distance of travel
68
Output 2
Minimal cost travel is by Audi
Input 3
Enter the driver salary
-250
Enter the car mileage in km per litre
55
Enter the cost of petrol per litre
75
Enter the taxi fare for a km
8
Enter the distance of travel
68
Output 3
You have entered a wrong input
20.)Gift Article
In the gifts store, Tintu finds a lot of fancy articles with letters inscribed in it. She decides to select
gifts for her friends such that the first letter of their names is inscribed in the gift articles.
There is a hifi-scanner device in the gifts shop that would illuminate the articles that contain the
letters fed as input. There is another interfacing device that would pick the 1st letters of all names
entered by the user. Suddenly the device started malfunctioning and Nisha the owner of the shop
is very tensed. Tintu is a very close friend of Nisha and she offers to help her in fixing this issue.
She starts rewriting the program to be embedded in the scanner like device.
As per her code, the program should get 3 names of the friends and also should get the a letter
which should be inscribed in the gift articles. If the entered letter is the first letter of any one of
her friend's name, then it should print "yes" else it should print "no". If the letter is a special
character, then it should not consider and it should print "no". Can you help Tintu to write the
code?
TestCase
Input 1
MAHIRL
CHITRA
DEVI
C
Output 1
yes
Input 2
MAHIRL
CHITRA
DEVI
A
Output 2
no
Input 3
JON SNOW
ARYA STARK
HODOR
S
Output 3
No
Input 4
@ON SNOW
ARYA STARK
HODOR
@
Output 4
no
21.)Select a Gift within the budget
Tintu shortlists 4 gifts priced at Rs.r1, Rs.r2, Rs.r3 and Rs.r4 respectively. She wants to buy 3 of
these gifts such that it doesn't exceed her budget of Rs.x. Identify the choices --- one where she
could save more money and the other where she could spend the maximum amount within the
planned budget.
Can you please help her?
[Note : Display "Invalid Amount" for any of the amount which is equal to zero]
TestCase
Input 1
20
30
10
25
80
Output 1
55
75
Input 2
20
30
10
25
-80
Output 2
The budget amount should not be negative
Input 3
20
30
-10
25
80
Output 3
The gift amount should not be negative
Input 4
20
30
0
25
80
Output 4
Invalid Amount
22.)Volume Finder
Mrs.Jeffie teaches the volume for the following shapes in different manner. Help her to write a
program to find the volume.
Test Case
Input 1
1. Cube
2. Cuboid
3. Cylinder
Enter your choice to find volume : 2
Enter length
3
Enter breadth
4
Enter height
2
Output 1
Volume of Cuboid is 24
Input 2
1. Cube
2. Cuboid
3. Cylinder
Enter your choice to find volume
1
Enter length
5
Output 2
Volume of Cube is 125
Input 3
1. Cube
2. Cuboid
3. Cylinder
Enter your choice to find volume
5
Output 3
Invalid choice
Input 4
1. Cube
2. Cuboid
3. Cylinder
Enter your choice to find volume
3
Enter radius
5
Enter height
6
Output 4
Volume of Cylinder is 471
23.)Amusement Park
The Tycon amusement theme park are classifying their visitors with respect to their age and are
allowing those people to play only on that ride. To do so they need to approach a programmer for
developing a program to classify the visitors. The price and the games list is listed below.
Age Name of Games Price List
3-5 Children play area 500
6-9 Balloon Race 1200
10-13 Water ride 2000
14-19 Pendulum ride 2300
above or equal to 20 and below 60 Swing ride 3300
The visitor whose age is above 20 is not allowed to play in "children play area" and "balloon
race"
[ If the age is less than 3 then display the error like Invalid age.]
TestCase
Input 1
Enter your age
7
Output 1
You are allowed to play at the Children play area and the Balloon Race.
Please make a payment of Rs.1700
Input 2
Enter your age
22
Output 2
You are allowed to play the Water ride Pendulum ride Swing ride.
Please make a payment of Rs.7600
Input 3
Enter your age
1
Output 3
Invalid age.
Input 4
Enter your age
65
Output 4
You are not allowed to play
Input 5
Enter your age
16
Output 5
You are allowed to play the Children play area Balloon Race Water ride and Pendulum ride.
Input 2
Enter value of turn 1
3
Enter value of turn 2
9
Enter value of turn 3
12
Output 2
Tweety is in danger
Input 3
Enter value of turn 1
0
Enter value of turn 2
9
Enter value of turn 3
12
Output 3
Invalid Turn
25.)Intersection of Rectangles
Given the coordinates of the lower left corner (x,y), the length (l) and width(w) of 2 rectangles,
Write a program to find the intersection of the two given rectangles.
Assume: Intersection always happens.
NOTE:
i) Find out the min of x and y co-ordinates (i.e) min_x,min_y.
ii) Calculate the max of x and y such that (x1+w1)>(x2+w2)? and (y1+l1)>(y2+l2)? respectively.
iii) Now estimate max of w and l by finding the difference between max-min of x and y co-
ordinates respectively.
iv) min of x and y,max of w and l are the expected output of intersection.
Test Case
Input 1
3 8 1515
2 6 10 10
Output 1
2 6 17 16
Input 2
1234
-5 6 7 8
Output 2
-5 2 11 10
Input 3
0000
0000
Output 3
0000
26.)Speed Limit of a Car
You are driving on NH from Kolkata to Delhi. The car which you are driving is a fully automated
one. It calculates the speed limit from the source to the destination. Write a program that, given
as input the distance (in feet) between two places ,the time (in seconds) taken by the car to travel
from Kolkata to Delhi, and the speed limit (in miles per hour). Determines whether or not the
average speed of the car ---in traveling from Kolkata to Delhi--- exceeded the speed limit.
Note: There are 5280 feet in a mile and 3600 seconds in an hour. Assume the speed limit is
always 55.
Input 1
60.0
0.7
55.0
Output 1
Speed Driving!!!!
Input 2
3.5
0.35
55.0
Output 2
Maintain the same level of Speed. Safe Driving!!!!
Input 3
-1
0.35
55.0
Output 3
Invalid Input
Input 4
500
0.35
54.0
Output 4
Invalid Input
27.)Estimation of Neethu's Travel Cost
May 27th is an auspicious day and Neethu's friends are getting married on that day. So Neethu
decides to purchase gifts for them at “The LandMark” which is in Brook Fields Mall. She is a
regular visitor to this shop. She wants to save energy resources and wants to follow some of the
suggestions given by the Government in order to contribute a small amount towards saving
energy resources.
She calculates and travel .Now help her out in finding which one is more cost effective Luxury
Bus or Share Auto.
The cost of traveling by Luxury Bus is Rs. X and the cost of traveling by Share Auto is Rs. Y. She
plans to spend minimal amount for travel. Given X and Y, help her to decide whether to travel by
the luxury bus or by the share auto.
Note : Both the cost should be valid
TestCase
Input 1
Enter the cost of travel by luxury bus
40
Enter the cost of travel by share auto
35
Output 1
Minimal cost travel is by auto
Input 2
Enter the cost of travel by luxury bus
30
Enter the cost of travel by share auto
45
Ouput 2
Minimal cost travel is by bus
Input 3
Enter the cost of travel by luxury bus
-30
Enter the cost of travel by share auto
45
Ouput 3
Cost should not be negative
28.)Locate The Land Mark
The Land Mark store is in the nth floor. Tintu gets into the lift in the ground floor. The lift that Tintu
gets in does not stop in all floors. It stops in only 3 floors numbered n1, n2 and n3.She wants to
get down in the floor that is closest to n. If there are 2 choices, she always prefers to climb down
the stairs rather than climbing up. Help Tintu in deciding the floor she should get down from the
lift. Tell to tintu like Invalid Floor number when she enters the input which is less than 0.
TestCase
Input 1
Enter the floor you want to get down at
10
Enter the floors in which the lift will stop
8
4
15
Output 1
You may get down at floor number 15
Input 2
Enter the floor you want to get down at
10
Enter the floors in which the lift will stop
2
6
9
Output 2
You may get down at floor number 9
Input 3
Enter the floor you want to get down at
-10
Output 3
Invalid Floor number
Input 4
Enter the floor you want to get down at
10
Enter the floors in which the lift will stop
3 2 -6
Output 4
Invalid Floor number
29.)Where is the Car?
Tintu finally reaches the Shopping Mall in her Audi and she gets down at the entrance and she
asks the driver to park the car in the 1st freely available parking space(if it is available) or at the
ground adjacent to the mall. The driver asks her to give a call when she is done with her
shopping and leaves to park the car. Suddenly Tintu remembers that she has left her mobile at
home. She sees a display board at the Mall entrance that gives the status of the occupancy of
the parking lot. There are 4 basements --- B1, B2, B3 and B4.
Assuming that only when B1 is full, cars would be allowed to be parked in B2 and so on. The
number of cars that can be parked in the basements B1, B2, B3 and B4 are N1, N2, N3 and N4.
The total number of cars parked in the parking lot currently is N. The values of N1, N2, N3, N4
and N are displayed in the giant display board in the mall entrance. Help Tintu in quickly deciding
where Raghav would have parked the car. For the negative inputs display the error "Please
check the given input"
[Assume that no other car is in queue to get parked and no other car leaves at this time].
TestCase
Input 1
50
50
40
40
120
Output 1
B3
Input 2
50
50
40
40
180
Output 2
B4
Input 3
50
-50
40
40
180
Output 3
Please check the given input
30.)Describe Damaged Items
Nisha knows that Neetu is an expert in embedded programming and she asks her to write a
program that would make her high tech scanner like device to identify the damaged gift articles.
She is only concerned with the gift articles that have 1 character inscribed on it. She wants to
classify the gift items as items with lower case letters inscribed, items with upper case letters
inscribed, items with digits (0 to 9) and special characters are damaged items.
Can you help Tintu in writing this program?
TestCase
Input 1
A
Output 1
upper case
Input 2
&
Output 2
damaged
Input 3
a
Output 3
lower case
Input 4
$A
Output 4
damaged