Assignment 33
Assignment 33
Assignment 33
03
Submitted by:
Ahmad Sameet Bin Manzar
Roll No:
SU92-BIOTM-S24-015
PROGRAMMING FUNDAMENTALS
Submitted to:
Sir Nadeem Sarfraz
1
Q1. Suppose that sale and bonus are double variables. Write an if...else statement that
assigns a value to bonus as follows: If sale is greater than $20,000, the value assigned to
bonus is 0.10, that is 10%; If sale is greater than $10,000 and less than or equal to $20,000, the
value assigned to bonus is 0.05, that is 5%; otherwise the value assigned to bonus is 0, that is
0%.
Program
#include<iostream>
int main()
cin>>sale;
if(20000<sale){
bonus=0.10;
else if (sale>10000&&sale<=20000){
bonus=0.5;
else {
bonus=0;
return 0;
2
Q2. Suppose that overSpeed and fine are double variables. Assign the value to fine as follows:
If 0 < overSpeed <= 5; the value assigned to fine is $20.00; if 5 < overSpeed <= 10; the value
assigned to fine is $75.00; 10 < overSpeed <= 15; the value assigned to fine is $150.00; if
overSpeed > 15; the value assigned to fine is $150.00 plus $20.00 per mile over 15.
Program :
#include<iostream>
int main()
double speed,fine;
cin>>speed;
if(speed>0&&speed<=5){
fine=20.00;
else if(speed>5&&speed<=10){
fine=75.00;
else if(speed>10&&speed<=15){
fine=150.00;
else if (speed>15){
fine=150+(speed-15)*20;
}else
fine=0;
3
}
return 0;
Q3. Write a program that prompts the user to input a number. The program should then output
the number and a message saying whether the number is positive, negative, or zero.
Program:
#include<iostream>
using namespace std;
int main()
{
int number;
if(number>0){
cout<<"number is positive : "<<number;
}
else if(number<0){
cout<<"number is negative : "<<number;
4
else {
cout<<"nmber is zero : "<<number;
}
return 0;
}
Q4. Write a program that prompts the user to input an integer between 0 and 35. If the number
is less than or equal to 9, the program should output the number; otherwise, it should output A
for 10, B for 11, C for 12, . . . , and Z for 35.
Program:
#include<iostream>
int main()
int number;
cin>>number;
switch(number){
case 1 :
cout<<number;
break;
case 2 :
cout<<number ;
break;
case 3 :
cout<<number ;
5
break;
case 4 :
cout<<number ;
break;
case 5 :
cout<<number ;
break;
case 6 :
cout<<number;
break;
case 7 :
cout<<number;
break;
case 8 :
cout<<number;
break;
case 9 :
cout<<number;
break;
case 10 :
cout<<"A";
break;
case 11 :
cout<<"B";
break;
case 12 :
cout<<"C";
break;
case 13 :
6
cout<<"D";
break;
case 14 :
cout<<"E";
break;
case 15 :
cout<<"F";
break;
case 16 :
cout<<"G";
break;
case 17 :
cout<<"H";
break;
case 18 :
cout<<"I";
break;
case 19 :
cout<<"J";
break;
case 20 :
cout<<"K";
break;
case 21 :
cout<<"L";
break;
case 22 :
cout<<"M";
7
break;
case 23 :
cout<<"N";
break;
case 24 :
cout<<"O";
break;
case 25 :
cout<<"P";
break;
case 26 :
cout<<"Q";
break;
case 27 :
cout<<"R";
break;
case 28 :
cout<<"S";
break;
case 29 :
cout<<"T";
break;
case 30:
cout<<"U";
break;
case 31 :
cout<<"V";
break;
case 32:
8
cout<<"W";
break;
case 33 :
cout<<"X";
break;
case 34 :
cout<<"Y";
break;
case 35 :
cout<<"Z";
break;
defoult :
return 0;
Q5. The statements in the following program are in incorrect order. Rearrange the statements
so that they prompt the user to input the shape type (rectangle, circle, or cylinder) and the
appropriate dimension of the shape. The program then outputs the following information
about the shape: For a rectangle, it outputs the area and perimeter; for a circle, it outputs the
area and circumference; and for a cylinder, it outputs the volume and surface area. After
rearranging the statements, your program should be properly indented.
Program:
#include <iostream>
9
int main()
string shape,rectangle,circle,cylinder;
cin>>shape;
if(shape=="rectangle"){
float length,breath,area;
cin>>length;
cin>>breath;
area=length*breath;
float premiter=2*(length+breath);
else if(shape=="circle"){
float radius,circumference;
cin>>radius;
10
float area=pi*radius*radius;
circumference=2*pi*radius;
else if(shape=="cylinder"){
float radius,height,volume,area;
cin>>radius;
cin>>height;
volume=pi*radius*radius*height;
area=2*pi*radius*(radius+height);
else {
return 0;
11
Q6. In a right triangle, the square of the length of one side is equal to the sum of the squares of
the lengths of the other two sides. Write a program that prompts the user to enter the lengths
of three sides of a triangle and then outputs a message indicating whether the triangle is a
right triangle.
Program:
#include<iostream>
int main()
float length1,length2,length3;
cin>>length1>>length2>>length3;
if(length1*length1==length2*length2+length3*length3||
length2*length2==length1*length1+length3*length3||
length3*length3==length1*length1+length2*length2){
else {
return 0;
Q7. Create a program that takes a numerical age as input and checks if a person is eligible to
vote. The voting age is 18 or older. The program should print "eligible" or "ineligible" based on
the user input.
Program:
#include<iostream>
int main()
12
{
int age;
cin>>age;
if(age>=18&&age<=100){
else if(age<18){
else{
return 0;
Q8. If cost price and selling price of an item is input through the keyboard, write a program to
determine whether the seller has made profit or incurred loss. Also determine how much
profit he made or loss he incurred.
Program:
#include<iostream>
int main()
float selling,buying;
cin>>buying;
cin>>selling;
13
float profit =selling-buying;
if(profit>0){
else if(loss>0){
return 0;
Q9. Any year is entered through the keyboard, write a program to determine whether the year is leap
or not. Use the logical operators && and ||.
Program:
#include<iostream>
int main()
int year;
cin>>year;
if(year%4==0&&year%100!=0||year%400==0){
else {
14
return 0;
Q10. A library charges a fine for every book returned late. For first 5 days the fine is 50 paise,
for 6-10 days fine is one rupee and above 10 days fine is 5 rupees. If you return the book after
30 days your membership will be cancelled. Write a program to accept the number of days the
member is late to return the book and display the fine or the appropriate message.
Program:
#include <iostream>
int main()
{ int days;
cin>>days;
if(days<=0){
else if(days<=5){
else if(days<=10){
cout<<"fine rs 1 ";
else if(days<=30){
cout<<"fine rs 5";
15
else{
return 0;
Q11. In a company, worker efficiency is determined on the basis of the time required for a
worker to complete a particular job. If the time taken by the worker is between 2 – 3 hours,
then the worker is said to be highly efficient. If the time required by the worker is between 3 – 4
hours, then the worker is ordered to improve speed. If the time taken is between 4 – 5 hours,
the worker is given training to improve his speed, and if the time taken by the worker is more
than 5 hours, then the worker has to leave the company. If the time taken by the worker is
input through the keyboard, find the efficiency of the worker. Here are five problem
statements designed for exercises involving the use of the `switch` statement in
programming:
Program:
#include<iostream>
int main()
int time;
cin>>time;
switch(time) {
case 2:
case 3:
break;
case 4:
break;
16
case 5:
break;
default:
break;
return 0;
Q12. Write a program that reads the current temperature from the user as an integer. The
program should then use a `switch` statement to output a recommended action based on the
temperature range: - Below 0°C, suggest "Stay indoors or dress very warmly." - Between 0°C
and 18°C, suggest "Wear long sleeves." - Between 19°C and 30°C, suggest "A t-shirt will be
fine." - Above 30°C, suggest "Stay cool and hydrated."
Program:
#include<iostream>
int main()
int temp;
cout<<"enter tempreature";
cin>>temp;
switch(temp){
break;
break;
17
case 19 ... 30:
break;
default:
break;
return 0;
Q 13. Create a program that takes a letter grade (A, B, C, D, F) as input from the user and
outputs a qualitative performance description using a `switch` statement: - 'A' corresponds
to "Excellent" - 'B' corresponds to "Good" - 'C' corresponds to "Average" - 'D' corresponds to
"Below Average" - 'F' corresponds to "Failing"
Program:
#include <iostream>
int main ()
char letter;
cin>>letter;
switch(letter){
case 'A' :
cout<<"Excellent";
break;
case 'B':
cout<<"Good";
break;
case 'C':
18
cout<<"Average";
break;
case 'D' :
cout<<"Below average";
break;
case 'F':
cout<<"Failing";
break;
default:
return 0;
Q 14. Problem Statement: Develop a program that prompts the user for a product category
code (1, 2, 3, 4) and displays the category name using a `switch` statement: - Code 1:
"Beverages" - Code 2: "Bakery" - Code 3: "Meat" - Code 4: "Dairy"
Program:
#include<iostream>
int main()
int product;
cin>>product;
switch(product){
case 1:
cout<<"Beverage";
break;
case 2:
19
cout<<"Bakery";
break;
case 3:
cout<<"Meat";
break;
case 4:
cout<<"Dairy";
break;
default:
cout<<"out of stock";
return 0;
Q15. Problem Statement: Write a program that takes a day of the week (e.g., 1 for Monday, 2 for
Tuesday, etc.) as input and outputs the main activity scheduled for that day using a `switch`
statement: - 1: "Team meeting at 10 AM" - 2: "Client presentations" - 3: "Project workday" - 4:
"Training day" - 5: "Free research day" - 6: "Weekend! Relax" - 7: "Family day"
Program:
#include<iostream>
int main ()
int days;
cin>>days;
switch(days){
20
case 1 :
break;
case 2 :
cout<<"client presentations";
break;
case 3 :
cout<<"project workday";
break;
case 4 :
cout<<"training day";
break;
case 5 :
break;
case 6 :
cout<<"weekend! relax";
break;
case 7 :
cout<<"family day";
break;
default:
return 0;
21
Q16. Problem Statement: Implement a program that simulates a traffic light controller. The
user enters a color (red, yellow, green), and the program uses a `switch` statement to
respond: - "red": "Stop" - "yellow": "Prepare to stop" - "green": "Go"
Program:
#include <iostream>
int main() {
char colour;
cout << "Enter colour ( red for r , yellow for y, green for g): ";
switch (colour) {
case 'r':
break;
case 'y':
break;
case 'g':
break;
default:
return 0;
22
Q17. Write a program that takes two numbers as input and prints "Equal" if they are equal,
"Greater" if the first number is greater, and "Smaller" if the first number is smaller.
Program:
#include <iostream>
int main()
int number1,number2;
cin>>number1;
cin>>number2;
if(number1==number2){
cout<<"equal";
else if(number1>number2){
else{
return 0;
23
Q18. Write a program that takes a numerical year as input and checks whether the given year
is a leap year or not. A leap year is divisible by 4, but not divisible by 100 unless it is divisible by
400. The program should print "Leap year" or "Not leap year" based on the user input.
Program:
#include<iostream>
int main()
int year;
cin>>year;
if(year%4==0&&year%100!=0||year%400==0){
else {
return 0;
Q19. Write a program that takes the three angles of a triangle (in positive degrees) as
numerical input and then classifies the triangle as either "Acute" (all angles less than 90
degrees), "Obtuse" (one angle greater than 90 degrees), "Right" (one angle equal to 90
degrees), or "Invalid" (The sum of the angles is not equal to 180). The program should print the
type of the triangle as classified above.
Program:
#include<iostream>
24
using namespace std;
int main()
int side1,side2,side3;
cin>>side1>>side2>>side3;
if(side1+side2+side3==180){
if(side1<90&&side2<90&&side3<90){
cout<<"acute";
else if(side1==90||side2==90||side3==90){
cout<<"right angle";
else{
cout<<"obtuse";
return 0;
Q20. Write a program that takes the exam score of a student as numerical input and then
determines the grade of the student based on their exam score. Use the following grading
scale: A (90-100), B (80-89), C (70-79), D (60-69), F (0-59). The program should print the grade
according to the user input.
Program:
#include<iostream>
25
using namespace std;
int main()
int score;
cin>>score;
switch(score){
cout<<"A";
break;
case 80 ... 89 :
cout<<"B";
break;
case 70 ... 79 :
cout<<"C0";
break;
case 60 ... 69 :
cout<<"D";
break;
case 0 ... 59 :
cout<<"F";
break;
default:
cout<<"invalid";
return 0;
26
Q21. Write a program using switch statement that takes a day of the week as input (e.g., 1 for
Sunday, 2 for Monday, and so on) and prints the corresponding day
Program:
#include<iostream>
int main()
int days;
cin>>days;
switch(days){
case 1:
cout<<"sunday";
break;
case 2:
cout<<"monday";
break;
case 3:
cout<<"tuesday";
break;
case 4:
cout<<"wednesday";
break;
case 5:
cout<<"thursday";
27
break;
case 6:
cout<<"friday";
break;
case 7:
cout<<"saturday";
break;
default:
cout<<"invalid";
return 0;
28