Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

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;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote