In: Computer Science
C++
Write a program using switch-case-break that will take an input number as for example 2 and based on switch it will provide various powers of 2.
#include<iostream>
using namespace std;
int main()
{
int n=0;
//takes input number and provides various powers of
2
cout<<"Enter: 1:for 2^1\n2:for 2^2\n3:for
2^3\n4:for 2^4\n5:for 2^5:\nEnter your choice:";
cin>>n;
// switch-case-break
switch(n){
case 1:
cout<<2<<endl;
break;
case 2:
cout<<4<<endl;
break;
case 3:
cout<<8<<endl;
break;
case 4:
cout<<16<<endl;
break;
case 5:
cout<<32<<endl;
break;
default:
cout<<"Invalid choice\n";
}
return 0;
}
output:
Enter: 1:for 2^1
2:for 2^2
3:for 2^3
4:for 2^4
5:for 2^5:
Enter your choice:3
8
Process exited normally.
Press any key to continue . . .