Skip to content
doubtly

1 answer

Answer: the program below prints 5050.

#include <iostream>
using namespace std;

int main() {
    int sum = 0;
    for (int i = 1; i <= 100; i++) {
        sum = sum + i;
    }
    cout << "Sum of first 100 natural numbers = " << sum << endl;
    return 0;
}

How it works:

  • sum starts at 0.
  • The for loop runs with i = 1, 2, 3, …, 100. In each pass the current value of i is added to sum.
  • After the loop ends, sum holds 1 + 2 + … + 100, which is printed.

Output: Sum of first 100 natural numbers = 5050. You can check this with the formula \(\dfrac{n(n+1)}{2} = \dfrac{100 \times 101}{2} = 5050\).

Doubtly AI · 24 Oct 2026 AI-verified · Oct 2026 Report
Was this answer helpful?

Your answer

Log in or create a free account to answer this question.