97.1k views
2 votes
Write an expression that executes the loop body as long as the user enters a non-negative number. Note: If the submitted code has an infinite loop, the system will stop running the code after a few seconds and report "Program end never reached." The system doesn't print the test case that caused the reported message. Sample outputs with inputs: 9 5 2 -1 Body Done.

User Joe H
by
3.4k points

1 Answer

2 votes

Answer:

Following are the program in C++ language

#include <iostream> // header file

using namespace std; // namespace

int main() // main function

{

int number=0; // variable declaration

while(number>-1) // iterating the while loop

{

cout<<"body"<<endl; // dsiplay the statement

cin>>number; // Read the value by the user

}

cout<<endl<<"Done";// dsiplay the statement

return 0;

}

Output:

body

5

body

45

body

-8

Done

Explanation:

Following are the description of the statement

  • Declared a variable "number" of int type and initialized with 0 to them
  • iterating the while loop. In this loop print the message "body" and Read the value by the user in the "number" variable. This loop will be executed repeatedly until the user do not read the negative value.
  • After the execution of the while loop, it prints the message "Done".
User Srimanth
by
4.4k points