193k views
3 votes
Write a main function that calculates the sum from 1 to 99 .

User Idanp
by
9.0k points

1 Answer

0 votes

Final answer:

The main function can be used to calculate the sum from 1 to 99 using a loop.

Step-by-step explanation:

The main function in a program is the entry point of the program, where the execution of the program starts. To calculate the sum from 1 to 99, you can use a loop to iterate through all the numbers from 1 to 99 and keep adding them to a variable.

int main() {
int sum = 0;
for (int i = 1; i <= 99; i++) {
sum += i;
}
return sum;
}

In this code, we initialize a variable 'sum' to 0 and use a 'for' loop to iterate through all the numbers from 1 to 99. Inside the loop, we add each number to the 'sum' variable. Finally, we return the sum.

User Hburde
by
7.8k points