Respuesta :
Write a for loop that prints all the even integers from 80 through 20 inclusive , separated by spaces .
Answer:
The c++ program to display even numbers in the range of 80 through 20, inclusive of the two values, using for loop is given below.
#include <iostream>
using namespace std;
int main() {
int i;
cout<<"This program displays all even numbers from 80 through 20 using for loop."<<endl;
cout<<"The even numbers are as follows"<<endl;
for(i=80;i>=20;i=i-2)
{
cout<<i<<" ";
}
cout<<endl;
return 0;
}
OUTPUT
This program displays all even numbers from 80 through 20 using for loop.
The even numbers are as follows
80 78 76 74 72 70 68 66 64 62 60 58 56 54 52 50 48 46 44 42 40 38 36 34 32 30 28 26 24 22 20
Explanation:
In this program, for loop begins by displaying the first even number, 80. Since, 80 is an even number, the previous even numbers can be obtained by subtracting 2 from 80.
This decrement continues till the lower limit of 20 is reached. 20, being an even number, is also displayed.
The variable declaration is done outside the loop.
int i;
The for loop implementing this logic is as follows.
for(i=80;i>=20;i=i-2)
{
cout<<i<<" ";
}
The variable initialization and condition for the loop are put together.
for(i=80;i>=20;i=i-2)
The variable i equates to 80 and 20 to display these numbers based on the criteria mentioned in the question. The expressions are separated using semi colon.
Variable declaration ends with semicolon.
i=80;
The condition for the loop ends with semi colon.
i>=20;
The decrement expression is the last expression within the brackets.
i=i-2
Alternatively, this range of even numbers is inclusive of 80 and 20 since both these numbers are also tested the even numbers.
The even number is displayed followed by space, as needed.
The above loop presents the easiest way to display the even numbers.
Hope the above answer is helpful.