In: Computer Science
Write a menu-driven C++ program with two options: 1) Is it odd or even? 2) End Program.The user can only input a positive integer (0 does not count) and if the input is not a positive integer the program must state "Invalid Input." The program must determine whether the input is even or odd. The program must use functions and can only use the iostream and iomanip libraries.
Note: The program must loop until the 2nd menu option is chosen.
Code:
#include <iostream>
using namespace std;
int main() {
int option;
do{
cout<<"1)Is it odd or even?\n2)End Program."<<endl;
cin>>option;
switch(option){
case 1:
int n;
cout<<":";
cin>>n;
if(n<0){
cout<<"Invalid Input"<<endl;
}else{
if(n%2==0){
cout<<"Even"<<endl;
}else{
cout<<"Odd"<<endl;
}
}
break;
case 2:
break;
default:
cout<<"Choose from menu"<<endl;
}
}while(option!=2);
return 0;
}
Snapshot for indentation:
Sample run: