212k views
5 votes
Write a C++ program that finds the sum of even numbers between 20 and 25​

User Adebisi
by
8.1k points

1 Answer

5 votes

Answer:

#include <iostream>

using namespace std;

int main() {

int sum = 0;

for(int n = 20; n<25; n+=2) {

sum += n;

}

cout << sum << endl;

}

Step-by-step explanation:

A shorter, less-readable i.m.o., way would be:

int main() {

int sum = 0;

for(int n = 20; n<25; sum+=n, n+=2);

cout << sum << endl;

}

But don't use tricks like that in production code.

User Djreisch
by
8.9k points

No related questions found