Section 2.4 of "Algorithm Notes"-C/C++ Quick Start -> Cyclic Structure Example 5-1-2 Summation of Continuous Natural Numbers

Example 5-1-2 Summing continuous natural numbers

Title description
Seek 1+2+3+…+100, that is, it is required to use the do…while statement to achieve
input and
no
output.
Calculate the result. Pay attention to the output line break at the end.
Sample input Copy
No
sample output Copy
5050

#include <stdio.h>

int main(void) {
    
    
    int i = 1;
    int sum = 0;
    do {
    
    
        sum += i;
        i++;
    } while (i <= 100);
    printf("%d", sum);
    return 0;
}

Guess you like

Origin blog.csdn.net/DoMoreSpeakLess/article/details/109881069