Skip to content
doubtly

1 answer

Best answer

#include <iostream>

using namespace std;

int main() {
 int sum = 0;
 
 // loop through the first 100 natural numbers and add them to sum
 for (int i = 1; i <= 100; i++) {
 sum += i;
 }
 
 // print the sum
 cout << "The sum of the first 100 natural numbers is " << sum << endl;
 
 return 0;
}


This program declares an integer variable calledsum and initializes it to 0. It then uses a for loop to iterate through the first 100 natural numbers (i.e., 1 to 100), adding each number tosum. Finally, it prints out the value ofsum using thecout statement.

Doubtly · 19 Mar 2023 Report
Was this answer helpful?

Your answer

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