96.3k views
1 vote
g Write a main program that asks the user for a number between 10 and 20 and it prints the even numbers between that number and 50. If the number is not in the 10-20 range, inclusive, the program will keep prompting the user for a valid number. Input Format A number between 10 and 20

1 Answer

5 votes

Answer:

The cpp program is given below.

#include <iostream>

using namespace std;

int main() {

// variables declared

int num;

do

num>20)

cout<<" Number is out of range. Enter valid number."<<endl;

while(num<10 || num>20);

cout<<" "<<endl;

cout<<"Even numbers between "<<num<<" and 50 are "<<endl;

// printing even numbers between num and 50

for(int n=num; n<=50; n++)

{

if(n%2 == 0)

cout<<n<<" ";

}

return 0;

}

OUTPUT

Enter a number between 10 and 20 (inclusive): 1

Number is out of range. Enter valid number.

Enter a number between 10 and 20 (inclusive): 11

Even numbers between 11 and 50 are

12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 46 48 50

Step-by-step explanation:

The program is described below.

1. Variable, num, is declared with integer datatype.

2. User input is taken for variable, num, inside do-while loop.

3. The loop executes till the user input is within the given range, beginning from 10 till 20. The while() clause contains two conditions since the input is taken within the given range. The two conditions are combined using || operator, known as OR.

4. Inside for loop which executes over integer variable, n, which is initialized to num till its value reaches 50; the variable, num, is tested for evenness by taking modulo of num with 2.

if(num%2 == 0)

5. If the remainder is 0, number is even and displayed to the console followed by a space.

6. All the code is written inside main() which has return type of int. Hence, The program ends with a return statement.

return 0;

7. All the requirements mentioned in the question are followed in the above program.

User Reinhard Behrens
by
3.5k points