Answer to Question #299784 in C++ for Nancy

Question #299784

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.

1
Expert's answer
2022-02-19T11:24:10-0500
#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;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog