Decision Structures Labsheet
Decision Structures Labsheet
1. Write a program that allows the user to input the radius of a circle, and it calculates
and displays the area. The program should only accept non-zero positive values for the
radius and display an error message if the user tries to input a zero or negative value.
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
float r, a;
if (r<=0){
cout<<"Error! The value of the radius cannot be less or equal to zero.";
}
else{
a=M_PI*r*r;
cout<<"The area is: "<<a;
}
return 0;
}
2. Write a program that asks the user his/her year of birth and calculates his/her age. If
he/she is below 18 years old, the program must display “You are a child aged !”.
Otherwise, it must display “You are an adult aged age years old!”.
(Assume that the age is calculated based on the year only)
Sample input: Input your year of Birth: 2000
Output: You are a child aged 14
Sample input: Input your year of Birth: 1994
Output: You are an adult aged 20
#include <iostream>
using namespace std;
int main()
{
int currentyear=2022;
int DOB, age;
age=currentyear-DOB;
if (DOB>2004) {
cout<<"You are a child aged "<<age<<endl;
}
else {
cout<<"You are an adult aged "<<age<<endl;
}
return 0;
}
3. A factory pays its workers at the rate of Rs 100 per hour if the number of hours worked
(per week) does not exceed 40. Otherwise, the hourly rate is Rs 150 for any hour worked
above 40 in a given week. Write a program that allows the input of number of hours
worked in a week and calculates and displays the wages for that week.
Sample Input: Input no. of hours worked: 35
Output: Total wages for the week: Rs 3500
Sample Input: Input no. of hours worked: 45
Output: Total wages for the week: Rs 4750
#include <iostream>
using namespace std;
int main()
{
int hr, ot, sal;
sal=hr*100;
ot=hr-40;
if (hr>40) {
sal=sal+(ot*50);
}
cout<<"Total wages for the week: Rs "<<sal<<endl;
return 0;
}
4. A baby-sitter charges Rs 250 an hour until 21:00 hrs. when the rate drops to Rs 175 an
hour (the children are in bed). Write a program that accepts a starting time and ending
time in hours and calculates the total baby-sitting bill. You can assume that the
babysitter leaves at latest by 23: hrs. (Note: Consider full hours only).
Sample Input: Please input the time of entry: 16
Please input the time of leaving: 20
Output: Bill: Rs 1000
Sample Input: Please input the time of entry: 16
Please input the time of leaving: 23
Output: Bill: Rs 1600
#include <iostream>
using namespace std;
int main()
{
int enter, leave, hours, sal;
if (leave<21) {
sal=(leave-enter)*250;
cout<<"Bill: Rs "<<sal;
else {
sal=((21-enter)*250)+(leave-21)*175;
cout<<"Bill: Rs "<<sal;
return 0;
}
5. Write a program that allows the input of two numbers and tells the user which of the
two numbers is larger. Note: Assume that the two numbers entered are not equal.
#include <iostream>
using namespace std;
int main()
{
int a, b;
if (a>b) {
cout<<"The larger number is: "<<a;
}
else{
cout<<"The larger number is: "<<b;
}
return 0;
}
6. Write a program that allows the coefficients a,b and c of a quadratic equation of the
form ax2 + bx + c=0 and it displays the roots of the equation, whether the roots are real
or complex.
//This program finds the real and imaginary roots of a quadratic equation
#include <iostream>
#include<cmath>
using namespace std;
int main()
{
float a, b, c;
float discRoot=sqrt(b*b-4*a*c);
float root1=(-b+discRoot)/(2*a);
float root2=(-b-discRoot)/(2*a);
if ((b*b)<(4*a*c)){
float discRoot=sqrt(-(b*b-4*a*c));
float root=(-b)/(2*a);
else {
cout<<"The solutions are:"<<root1<<" and "<<root2<<endl;
}
return 0;
}
7. Write a program that reads an integer value, num, and determines if it is a perfect
square. If it is a perfect square, then the program displays a message saying that num is
a perfect square. Note: A perfect square is a number whose square root is an integer, e.g.
4 and 25 are perfect squares while 3 and 8 are not.
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
int num, s;
float r;
r=(sqrt(num));
s=r;
if (s==r){
cout<<"num is a perfect square";
}
else{
cout<<"num is not a perfect square";
}
return 0;
}
8. A lecturer gives 5-point quizzes that are graded as follows: 5 – A, 4 – B, 3 – C, 2 – D, 1 –
E, 0 – F. Write a program that accepts a quiz score as input and uses a decision structure
to display the above grades.
#include <iostream>
using namespace std;
int main()
{
int mark;
cout<<"Enter marks: ";
cin>>mark;
if (mark==0){
cout<<"F"<<endl;
}
else if (mark==1){
cout<<"E";
}
else if (mark==2){
cout<<"D";
}
else if (mark==3){
cout<<"C";
}
else if (mark==4){
cout<<"B";
}
else if (mark==5){
cout<<"A";
}
else {
cout<<"Invalid marks";
}
return 0;
}
9. At a University, 100-point exams are graded as follows: Marks Grade 70 – 100 A 60 – 69
B 50 – 59 C 40 – 49 D < 40 F Write a program that accepts an exam score as input and
uses a decision structure to find the corresponding grade. Display suitable messages if
the marks do not lie in the range 0 - 100.
#include <iostream>
using namespace std;
int main()
{
int score;
if (score<0){
cout<<"Marks cannot be less than zero";
}
else if(score<40){
cout<<"F";
}
else if(score<50){
cout<<"D";
}
else if(score<60){
cout<<"C";
}
else if(score<70){
cout<<"B";
}
else if(score<=100){
cout<<"A";
}
else if(score>100){
cout<<"Exam score cannot be more than 100";
}
return 0;
}
10. A speeding ticket fine policy is Rs 500 plus Rs 10 for each km/hr over the speed limit
of 90 km/hr. Write a program that accepts speed in km/hr as input, and displays a
message indicating that the speed limit has not been exceeded or prints the amount of
the fine that has to be paid. Also, speed should be in the range of 0 – 300 km/hr. All
speeds outside this range should be rejected as invalid and a suitable message is to be
displayed
#include <iostream>
using namespace std;
int main()
{
int speed, excess, fine;
if (speed<0){
cout<<"Speed cannot be less than zero.";
}
else if (speed<=90){
cout<<"Speed is within limit.";
}
else if (speed<=300){
excess=speed-90;
fine=500+(excess*10);
cout<<"Fine to be paid is: "<<fine;
}
else if (speed>300){
cout<<"This is not F1 racing";
}
return 0;
}