Write a program that asks a user to input a single positive integer Alert the us
ID: 3543625 • Letter: W
Question
Write a program that asks a user to input a single positive integer Alert the user if the number is NOT valid (when the input is negative) and end the program If the number is valid, let them know if the number is evenly divisible by 3, 7 and 13. Your ouput should look as follows: Please enter a positive number: 13 Divisible by 3: no Divisible by 7: no Divisible by 13: yes Please enter a positive number: -21 Your input is invalid Write a program that asks the user to input a positive integer x Compute the factorial of that number. If the user's input is invalid, let them know with a message Factorial is computed as follows 5! = 5 times 4 times 3 times 2 times 1 = 120. Enter a value x: 3 3! is: 6 Enter a positive integer x: -45 Your input is invalid.Explanation / Answer
//problem 1
#include <iostream>
using namespace std;
int main()
{
int n;
//Prompt the user for a positive number
cout << "Please enter a positive number: ";
//Read the number in variable n
cin>>n;
//Print a new line
cout<<endl;
//Validate the input
if(n<=0){
cout<<"Your input is invalid"<<endl;
}
//If input is validated
else{
//check the divisibility of n by 3 and print yes or no accordingly
cout<<"Divisible by 3: ";
if(n%3 == 0) cout<<"yes"<<endl;
else cout<<"no"<<endl;
//check the divisibility of n by 7 and print yes or no accordingly
cout<<"Divisible by 7: ";
if(n%7 == 0) cout<<"yes"<<endl;
else cout<<"no"<<endl;
//check the divisibility of n by 13 and print yes or no accordingly
cout<<"Divisible by 13: ";
if(n%13 == 0) cout<<"yes"<<endl;
else cout<<"no"<<endl;
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.