Write a C++ program to find sum of all even numbers between 1 to n. – using for loop
#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;
}
Comments
Leave a comment