Download presentation
Presentation is loading. Please wait.
1
Introduction to Algorithms and Programming COMP151
LAB 6 Switch case Introduction to Algorithms and Programming COMP151
2
Review Questions
3
Q1: A car with a 20 litters of petrol tank averages 21.5 Miles Per Litter (MPL) when driven in town and 26.8 Miles Per Litter (MPL) when driven on the high way. Write an algorithm and draw the flowchart that calculates and display the distance the car can travel on one tank of petrol when driven in town and when driven on the high way. Hint: Distance = Numbers of litters * Average Miles Per Litter
4
#include <iostream>
using namespace std; int main() { int petrol=20; float town_average_miles= 21.5; float highway_average_miles= 26.8; float town_ditance, highway_distance; town_ditance= petrol * town_average_miles; highway_distance= petrol * highway_average_miles; cout<<" Distance in town = "<<town_ditance<<endl; cout<<" Distance in High way = "<< highway_distance<<endl; return (0); }
5
Q2: Print the following pattern on the screen: * * * * * * * * * * * * * * *
6
#include <iostream>
using namespace std; int main() { cout<<"\* * * * *\n"; cout<<"\ * * * *\n"; cout<<"\ * * *\n"; cout<<"\ * *\n"; cout<<"\ *\n"; return (0); }
7
Q3: Write a program to display the people category based on their age as given below Age Category Above 60 Really old >=45 and <=60 Old >=30 and <45 Middle age >=20 and <30 Adult >12 and <20 Teens Otherwise Children
9
// (1) Switch case // (2) switch(ch) { case 1: res=a+b;
cout<<"Result of Addition is"<<res<<endl; break; case 2: res=a-b; cout<<"Result of Subtraction is"<<res<<endl; case 3: res=a*b; cout<<"Result of Multiplication is"<<res<<endl; case 4: res=a/b; cout<<"Result of Division is"<<res<<endl; default: cout<<"Enter the choice between 1 and 4"<<endl; } return(0); // (1) #include<iostream> using namespace std; int main() { float a,b,res; int ch; cout<<"Enter the Value for a"<<endl; cin>>a; cout<<"Enter the Value for b"<<endl; cin>>b; cout<<"1.Addition"<<endl; cout<<"2.Subtraction"<<endl; cout<<"3.Multiplication"<<endl; cout<<"4.Division"<<endl; cout<<"Enter your choice"<<endl; cin>>ch;
10
Switch case // (2) case 5: cout<<"Thursday"<<endl; break;
// (1) #include<iostream> using namespace std; int main() { int day; cout<<"Enter the order of day"<<endl; cin>>day; switch(day) case 1: cout<<"Sunday"<<endl; break; case 2: cout<<"Monday"<<endl; case 3: cout<<"Tuesday"<<endl; case 4: cout<<"Wednesday"<<endl; // (2) case 5: cout<<"Thursday"<<endl; break; case 6: cout<<"Friday"<<endl; case 7: cout<<"Saturday"<<endl; default: cout<<"Enter the order between 1 and 7"<<endl; } return(0);
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.