0% found this document useful (0 votes)
19 views28 pages

Assignment 33

Download as pdf or txt
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 28

ASSIGNMENT NO.

03

Submitted by:
Ahmad Sameet Bin Manzar

Roll No:
SU92-BIOTM-S24-015

PROGRAMMING FUNDAMENTALS

THE SUPERIOR UNIVERSITY, LAHORE


Department Of Information & Technology
Section: BSIOT-1A

Submitted to:
Sir Nadeem Sarfraz

April 29, 2024

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>

using namespace std;

int main()

double bonus ,sale;

cout<<"enter sale : ";

cin>>sale;

if(20000<sale){

bonus=0.10;

else if (sale>10000&&sale<=20000){

bonus=0.5;

else {

bonus=0;

cout<<"bonus on sale is : "<<bonus;

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>

using namespace std;

int main()

double speed,fine;

cout<<"enter voileter's speed : ";

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
}

cout<<"overspeed fine : "<<fine<<"$";

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;

cout<<"enter a number : ";


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

using namespace std;

int main()

int number;

cout<<"enter any number from 0 to 35 : ";

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 :

cout<<"you entered wrong number";

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>

using namespace std;

const float pi=3.14;

9
int main()

string shape,rectangle,circle,cylinder;

cout<<"enter shape name of rectangle,cylinder,circle : ";

cin>>shape;

if(shape=="rectangle"){

float length,breath,area;

cout<<"enter length :"<<endl;

cin>>length;

cout<<"enter breath : "<<endl;

cin>>breath;

area=length*breath;

float premiter=2*(length+breath);

cout<<"area of rectangle is = "<<area<<endl;

cout<<"peramiter of a rectangle is = "<<premiter;

else if(shape=="circle"){

float radius,circumference;

cout<<"enter radius of circle : "<<endl;

cin>>radius;

10
float area=pi*radius*radius;

circumference=2*pi*radius;

cout<<"area of circul is : "<<area<<endl;

cout<<"ircumference of a circule is : "<<circumference;

else if(shape=="cylinder"){

float radius,height,volume,area;

cout<<"enter radius of cylender : "<<endl;

cin>>radius;

cout<<"enter height of a cylender : ";

cin>>height;

volume=pi*radius*radius*height;

area=2*pi*radius*(radius+height);

cout<<"volume of a celinder is : "<<volume<<endl;

cout<<"surfce area of a cylinder is : "<<area<<endl;

else {

cout<<"enter the right shape ";

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>

using namespace std;

int main()

float length1,length2,length3;

cout<<"enter the 3 sides of a triangle :";

cin>>length1>>length2>>length3;

if(length1*length1==length2*length2+length3*length3||

length2*length2==length1*length1+length3*length3||

length3*length3==length1*length1+length2*length2){

cout<<"triangle is right trangle";

else {

cout<<"triangle is not a right triangle";

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>

using namespace std;

int main()

12
{

int age;

cout<<"enter your age : ";

cin>>age;

if(age>=18&&age<=100){

cout<<"you are eilgiable to vote : ";

else if(age<18){

cout<<"you are not eligiable for vote";

else{

cout<<"enter the correct age";

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>

using namespace std;

int main()

float selling,buying;

cout<<"enter buying of product : ";

cin>>buying;

cout<<"enter selling of the product : ";

cin>>selling;

13
float profit =selling-buying;

float loss = buying-selling;

if(profit>0){

cout<<"user make a profit of : "<<profit;

else if(loss>0){

cout<<"user make loss of : "<<loss;

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>

using namespace std;

int main()

int year;

cout<<"enter any year : ";

cin>>year;

if(year%4==0&&year%100!=0||year%400==0){

cout<<"year is leap year : "<<year;

else {

cout<< year<<"this year i not leap year ";

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>

using namespace std;

int main()

{ int days;

cout<<"enter late days of subbmission : ";

cin>>days;

if(days<=0){

cout<<"no fine ";

else if(days<=5){

cout<<"fine rs 50 paisa ";

else if(days<=10){

cout<<"fine rs 1 ";

else if(days<=30){

cout<<"fine rs 5";

15
else{

cout<<"your membership cenceled";

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>

using namespace std ;

int main()

int time;

cout<<"enter hours : ";

cin>>time;

switch(time) {

case 2:

case 3:

cout<<"the worker is efficient ";

break;

case 4:

cout<<"the worker sould increase speed" ;

break;

16
case 5:

cout<<"worker need trainning ";

break;

default:

cout<<"worker should leave the company";

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>

using namespace std;

int main()

int temp;

cout<<"enter tempreature";

cin>>temp;

switch(temp){

case -100 ... -1:

cout<<"stay indoor or dress very warmly";

break;

case 0 ... 18:

cout<<"wear long sleeves";

break;

17
case 19 ... 30:

cout<<"a t shirt will be fine ";

break;

default:

cout<<"stay cool and hydrated";

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>

using namespace std;

int main ()

char letter;

cout<<"enter a leeter : ";

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:

cout<<"enter correct letter";

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>

using namespace std;

int main()

int product;

cout<<"enter product code from 1 TO 4 : ";

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>

using namespace std;

int main ()

int days;

cout<<"enter day : ";

cin>>days;

switch(days){

20
case 1 :

cout<<"team meeeting at 10 am";

break;

case 2 :

cout<<"client presentations";

break;

case 3 :

cout<<"project workday";

break;

case 4 :

cout<<"training day";

break;

case 5 :

cout<<"free reserch day";

break;

case 6 :

cout<<"weekend! relax";

break;

case 7 :

cout<<"family day";

break;

default:

cout<<"enter correct day";

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>

using namespace std;

int main() {

char colour;

cout << "Enter colour ( red for r , yellow for y, green for g): ";

cin >> colour;

switch (colour) {

case 'r':

cout << "Stop" << endl;

break;

case 'y':

cout << "Prepare to stop" << endl;

break;

case 'g':

cout << "Go" << endl;

break;

default:

cout << "Invalid colour" << endl;

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>

using namespace std;

int main()

int number1,number2;

cout<<"enter a first number : ";

cin>>number1;

cout<<"enter second number : ";

cin>>number2;

if(number1==number2){

cout<<"equal";

else if(number1>number2){

cout<<"first is greater ";

else{

cout<<"second is greater ";

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>

using namespace std;

int main()

int year;

cout<<"enter any year : ";

cin>>year;

if(year%4==0&&year%100!=0||year%400==0){

cout<<year<<" leap year";

else {

cout<<year<<" not a leap year";

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;

cout<<"enter 3 side of triangle : ";

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

else{ cout<<"invalid sides ";

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;

cout<<"enter exam score : ";

cin>>score;

switch(score){

case 90 ... 100 :

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>

using namespace std;

int main()

int days;

cout<<"enter any number from 1 to 7 : ";

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

You might also like