Write a menu driven program using Switch Case in C++ to input a number and perform the
following tasks:
(i) Count and print the number of its digits.
(ii) Print the sum and product of its digits.
#include <iostream>
using namespace std;
int main() {
int number;
int ch=-1;
while(ch!=3){
cout<<"1. Count and print the number of digits\n";
cout<<"2. Print the sum and product of digits\n";
cout<<"3. Exit\n";
cout<<"Your choice: ";
cin>>ch;
switch(ch){
case 1:{
int count=0;
cout<<"Enter the number: ";
cin>>number;
while (number > 0) {
int d = number % 10;
count++;
number = number / 10;
}
cout<<"The number of digits: "<<count<<"\n";
}
break;
case 2:
{
int sum=0;
long int product=1;
cout<<"Enter the number: ";
cin>>number;
while (number > 0) {
int d = number % 10;
sum+=d;
product*=d;
number = number / 10;
}
cout<<"The sum of its digits: "<<sum<<"\n";
cout<<"The product of its digits: "<<product<<"\n\n";
}
break;
case 3:
break;
default:
break;
}
}
system("pause");
return 0;
}
Comments
Leave a comment