Menu Driven programs
Question:
C++ program to find maximum of 2 and 3 numbers. Minimum of 2 and 3 numbers?
Sol:
#include<iostream> #include<stdlib.h> #include<conio.h> using namespace std; int max(int n1,int n2) { if(n1>n2) return(n1); else return(n2); } int max(int n1,int n2,int n3) { if(n1>n2 && n1>n3) return(n1); else if(n2>n3) return(n2); else return(n3); } int min(int n1,int n2) { if(n1<n2) return(n1); else return(n2); } int min(int n1,int n2,int n3) { if(n1<n2 && n1<n3) return(n1); else if(n2<n3) return(n2); else return(n3); } //method:2 int main() { int a1,a2,a3,a4,op=0; while(op!=5) { system("cls"); //to clear the screen header file <stdlib.h> cout<<"1. max of 2 nos"<<endl; cout<<"2. Max of 3 nos "<<endl; cout<<"3. Min of 2 nos"<<endl; cout<<"4. Min of 3 nos "<<endl; cout<<"5. Exit"<<endl; cout<<"Enter your choice "; cin>>op; switch(op) { case 1: cout<<"Enter 2 nos "; cin>>a1>>a2; a4=max(a1,a2); cout<<"Max of 2 nos "<<a4<<endl; break; case 2: cout<<"Enter 3 nos "; cin>>a1>>a2>>a3; a4=max(a1,a2,a3); cout<<"Max of 3 nos "<<a4<<endl; break; case 3: cout<<"Enter 2 nos "; cin>>a1>>a2; a4=min(a1,a2); cout<<"Min of 2 nos "<<a4<<endl; break; case 4: cout<<"Enter 3 nos "; cin>>a1>>a2>>a3; a4=min(a1,a2,a3); cout<<"Min of 3 nos "<<a4<<endl; break; case 5: cout<<"End"<<endl; break; default: cout<<"Invalid choice"<<endl; break; }//switch getch(); }//while return(0); }//main
/* Output */ 1. max of 2 nos 2. Max of 3 nos 3. Min of 2 nos 4. Min of 3 nos 5. Exit Enter your choice 2 Enter 3 nos 25 96 34 Max of 3 nos 96 1. max of 2 nos 2. Max of 3 nos 3. Min of 2 nos 4. Min of 3 nos 5. Exit Enter your choice 4 Enter 3 nos 25 98 45 Min of 3 nos 25