Answer to Question #321381 in C++ for Micheal

Question #321381

Write a C++ program to find sum of all even numbers between 1 to n. – using for loop


1
Expert's answer
2022-06-22T18:23:01-0400
#include <iostream>
using namespace std;

int main() {
	int n, sum = 0;
	cout << "Enter N: " << "\n";
	cin >> n;
	for(int i=1; i<n; i++){
	    if(!(i%2)){
	        sum += i;
	    }
	}
	printf("Sum of all even numbers between 1 and %d: %d", n, sum); 
	return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog