Ask the user for positive integers; display each value entered. Stop asking when
ID: 3924525 • Letter: A
Question
Ask the user for positive integers; display each value entered. Stop asking when the user enters an odd number.
I've got this so far as my solution:
#include <iostream>
using namespace std;
int main()
{
int num;
do{
cout <<"Enter a positive integer: ";
cin >>num;
cout << num << endl;
if(num%2!=0){
cout<<"You entered a non-positive number, so exiting."<<endl;
}
}
while(num>0);
return 0;
}
****** How do I display "Program Timed out" if there is no input provided by the user? ******
Explanation / Answer
#include <iostream>
using namespace std;
int main()
{
int num;
do{
cout <<"Enter a positive integer: ";
cin >>num;
if(num<0){
cout<<"You entered a non-positive number, so exiting."<<endl;
break;
}
if(num%2==1){
cout<<"You entered an odd number, so exiting."<<endl;
break;
}
cout << num << endl;
}
while(true);
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.