204k views
2 votes
Write a program segment with a do-while loop that displays whether a user-entered integer is even or odd. The code should then ask the user if he or she wants to test another number. The loop should repeat as long as the user enters 'Y' or 'y' . Use a logical OR operator in the do-while loop test expression

User Danbruegge
by
6.0k points

1 Answer

3 votes

Answer:

Following is the program in C++ program

#include<iostream> // header file

using namespace std; // namespace

int main() // main function

{

int num1 = 0; // variable declaration

char test;// variable declaration

do

{

cout << " Enter the number: ";

cin >> num1; //Read the input by the user

if (num1 % 2 == 0) // check the condition of even

cout << " number is even.\\";

else // check the condition of odd

cout << "Number is odd.\\";

cout << " Do you wanted the another test of number (y/n)? ";

cin >> test; //Read the input by user

} while (test == 'y' || test == 'Y'); //terating the loop

return 0;

}

Output:

Enter the number:45

number is even.

Do you wanted the another test of number (y/n) Y

Enter the number:5

Number is odd.

Do you wanted the another test of number (y/n) n

Step-by-step explanation:

Following are the description of program

  • Declared a variable "num1" as integer type and initialized as 0 to them
  • Declared a variable "test" as char type .
  • After that iterating the do -while loop .Read the value by the user in the "num1" variable .
  • Check the condition of even and odd by using % operator .
  • Read the value of test by the user .If the user enter Y or y then loop again executing otherwise not .
User David Brower
by
6.7k points