Write a c++ program to find sum of all even numbers between 1-n.- using for loop
#include <iostream>
using namespace std;
int main() {
int n;
cout << "Enter an integer n: ";
cin >> n;
int sum = 0;
for (int i=1; i<=n; i++) {
if (i%2 == 0) {
sum += i;
}
}
cout << "The sum of all even numbers betwen 1 and " << n
<< " is " << sum << endl;
return 0;
}
Comments
Leave a comment