Mahmoud_Hawara

sum of even numbers from 1 to n

Oct 28th, 2024
300
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.45 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int main()
  5. {
  6.     cout << "Program to calculate the sum from of even numbers from 1 to n\n";
  7.     cout << "-------------------------------------------------------------\n";
  8.     cout << "Enter n: ";
  9.     int n;
  10.     cin >> n;
  11.     int ans = 0;
  12.     for (int i = 2; i <= n; i += 2)
  13.     {
  14.         ans += i;
  15.     }
  16.     cout << "The sum of even numbers from 1 to " << n << " is: " << ans << '\n';
  17.     return 0;
  18. }
Advertisement
Add Comment
Please, Sign In to add comment