- #include<iostream>
- using namespace std;
- int main(){
- cout<<"In this program you have to enter three numbers";
- while(1)
- {
- cout<<"\nPress 1 to enter them"
- <<"\nPress 2 to find the maximum "
- <<"\nPress 3 to find the minimum"
- <<"\nPress 4 to find the product of them"
- <<"\nPress 5 to exit";
- int choice,n1,n2,n3,product;
- cout<<"\nEnter your choice :";cin>>choice;
- switch(choice)
- {
- case 1: cout<<"\nEnter the first number :";cin>>n1;
- cout<<"\nEnter the second number :";cin>>n2;
- cout<<"\nEnter the third number :";cin>>n3;
- break;
- case 2: int max;
- if(n2>=n1 && n2>=n3)max=n2;
- if(n3>=n1 && n3>=n2)max=n3;
- if(n1>=n2 && n1>=n3)max=n1;
- cout<<"\nThe maximum number is :"<<max;
- break;
- case 3: int min;
- if(n2<=n1 && n2<=n3)min=n2;
- if(n1<=n2 && n1<=n3)min=n1;
- if(n3<=n2 && n3<=n1)min=n3;
- cout<<"\nThe minimum is :"<<min;
- break;
- case 4: int product;product=n1*n2*n3;
- cout<<"\nThe product result is :"<<product;
- break;
- case 5:exit(1);break;
- default: cout<<"You didn't choose any correct number";break;
- }
- }
- return 0;
- }
Explanation
switch is a keyword used to enter one if the case one time. For this example, when you press 1 the user enters 3 numbers and break the switch the the options will show. If the user press 2 the compiler will find the maximum between the three numbers and the switch will break. If he presses 3 the compiler will find the minimum and break the switch, if 4 find the product of three numbers and 5 to exit the while(1) this means the program.
default: this will be executed when the user don't press any number matching the case.
Note: You can check all the keywords anytime you like :
https://philippebalech.blogspot.com/2018/01/keywords-in-c.html
default: this will be executed when the user don't press any number matching the case.
Note: You can check all the keywords anytime you like :
https://philippebalech.blogspot.com/2018/01/keywords-in-c.html
No comments:
Post a Comment