Write a complete basic C++ program that does the following: A) Calculate the squ
ID: 641343 • Letter: W
Question
Write a complete basic C++ program that does the following:
A) Calculate the square of a number
Ask the use to enter a number greater than 0 and less than 10. If the number entered is not within the desired range the program terminates. If it is within the range the program then calculates and prints the square of the given number.
B) Calculate the product of all digits
Program prompts user to enter a positive 3 digit integer. If the number is not within range, the program prints appropriate error message and terminates. If the number is within range the program then calculates and prints the product of all the digits. Program must test for out of bound cases using if and else statements.
Explanation / Answer
#include <iostream>
using namespace std;
int main(){
int n;
cout << "enter a number greater than 0 and less than 10: ";
cin >> n;
if(n > 0 && n < 10){
cout << "Square of " << n << " is " << (n * n) << endl;
}
else{
cout << "Entered Value is not in specified range" << endl;
}
int prod = 1;
cout << "enter a positive 3 digit integer: " << endl;
cin >> n;
if(n > 99 && n < 1000){
while(n != 0){
prod *= (n % 10);
n /= 10;
}
cout << "product of all the digits is " << prod << endl;
}
else{
cout << "Entered Value is not in specified range" << endl;
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.