Calculating the sum of the given integers using while loop in C programming

 

 

Compiler: Visual C++ Express Edition 2005

Compiled on Platform: Windows XP Pro SP2

Header file: Standard

Additional library: none/default

Additional project setting: Set project to be compiled as C

Project -> your_project_name Properties -> Configuration Properties -> C/C++ -> Advanced -> Compiled As: Compiled as C Code (/TC)

Other info: none

To do: Calculating the sum of the given integers using while loop

To show: How to calculate the sum of the given integers using the while loop

 

 

/* Demonstrates a simple while statement */

#include <stdio.h>

 

int main(void)

{

int calculate, sum = 0;

 

/* print the numbers 1 through 12* /

/* set the initial value... */

calculate = 1;

 

/* set the while condition */

while(calculate <= 10)

{

/* display... */

printf("%d -->Sum = %d\n", calculate, sum);

sum = sum + calculate;

/* increment by 1, repeats */

calculate++;

}

printf("\n");

return 0;

}

 

Output example:

 

1 -->Sum = 0

2 -->Sum = 1

3 -->Sum = 3

4 -->Sum = 6

5 -->Sum = 10

6 -->Sum = 15

7 -->Sum = 21

8 -->Sum = 28

9 -->Sum = 36

10 -->Sum = 45

Press any key to continue . . .

 

 

C and C++ Programming Resources | C & C++ Code Example Index