Write a C++ program to call user defined function through switch
statement.
Sample Input/Output:
==========Main Menu==========
[S/s] – Square
[C/c] – Cube
[D/d] – Double
=============================
Enter your choice:d
Enter an Integer number:5
Double of 5 is 10
Do you want to try again? Y/y
==========Main Menu==========
[S/s] – Square
[C/c] – Cube
[D/d] – Double
=============================
Enter your Choice:s
Enter an Integer number:10
Square of 10 is 100
Do you want to try again? Y/y
==========Main Menu==========
[S/s] – Square
[C/c] – Cube
[D/d] – Double
=============================
Enter your Choice:C
Enter an Integer number:4
Cube of 4 is 64
Do you want to try again? N/n
Exit/Terminated
#include <iostream>
using namespace std;
void Menu()
{
cout << "==========Main Menu==========\n"
<< "[S / s] - Square\n"
<< "[C / c] - Cube\n"
<< "[D / d] - Double\n";
}
int main()
{
char ch;
int number;
do
{
Menu();
cout << "Enter your choice: ";
cin >> ch;
cout << "Enter an Integer number:";
cin >> number;
switch (ch)
{
case 'S':case's':
{
cout << "Square of " << number << " is " << number*number;
break;
}
case 'C':case'c':
{
cout << "Cube of " << number << " is " << number*number*number;
break;
}
case 'D':case'd':
{
cout << "Double of " << number << " is " << number*2;
break;
}
}
cout << "\nDo you want to try again? ";
cin >> ch;
} while (ch=='Y'|| ch == 'y');
}
Comments
Leave a comment