**Exercise: **

Write a C program to calculate the sum of natural numbers.

#### 1. Calculate using for loop

**Click Here to View the Solution!**

```
#include <stdio.h>
int main() {
int n, i, sum = 0;
printf("Enter a positive integer: ");
scanf("%d", &n);
for (i = 1; i <= n; ++i) {
sum += i;
}
printf("Sum = %d", sum);
return 0;
}
```

**Click Here to View the Output!**

```
Enter a positive integer: 5
Sum = 15
```

**Click Here to View the Explanation!**

- This program is used to calculate the sum of natural numbers using a
`for loop`

. - The program requests the user to enter a positive number and stores it in an
`integer`

variable`n`

using the`scanf()`

function. - A
`for loop`

is initialized that calculates the sum of the natural numbers by adding a number one at a time in each iteration which continues till the number`n`

is reached in the loop. And each time increment the number by one. - The result of additions is stored in a variable
`sum`

which is then printed. - The
`return 0 statement`

is used to exit the program execution.

#### 2.Calculate using while loop

**Click Here to View the Solution!**

```
#include <stdio.h>
int main() {
int n, i=1, sum = 0;
printf("Enter a positive integer: ");
scanf("%d", &n);
while (i <= n) {
sum += i;
++i;
}
printf("Sum = %d", sum);
return 0;
}
```

**Click Here to View the Output!**

```
Enter a positive integer: 68
Sum = 2346
```

**Click Here to View the Explanation!**

- This program is used to calculate the sum of natural numbers using a
`while loop`

. - The program requests the user to enter a positive number and stores it in an integer variable
`n`

using the`scanf()`

function. - The
`while loop`

states a condition which keeps the loop iterating for`n`

number of times and keeps adding the number`i`

in the variable sum and increments`i`

by one each time. - The
`sum`

is then printed and displayed on the screen. - The
`for loop`

is preferred over the`while loop`

since the number of iterations are known in a`for loop`

. - The
`return 0 statement`

is used to exit the program execution.

#### 3. Read Input Until user enters Positive Integer

**Click Here to View the Solution!**

```
#include <stdio.h>
int main() {
int n, i, sum = 0;
do {
printf("Enter a positive integer: ");
scanf("%d", &n);
} while (n <= 0);
for (i = 1; i <= n; ++i) {
sum += i;
}
printf("Sum = %d", sum);
return 0;
}
```

**Click Here to View the Output!**

```
Enter a positive integer: 6
Sum = 21
```

**Click Here to View the Explanation!**

- This program is used to calculate the sum of natural numbers using a
`while loop`

. - The program requests the user to enter a positive number and stores it in an integer variable

using the`n`

`scanf()`

function. - The
`while loop`

states a condition which keeps the loop iterating for

number of times and keeps adding the number`n`

`i`

`i`

- The

is then printed and displayed on the screen.`sum`

- The
`for loop`

is preferred over the`while loop`

since the number of iterations are known in a`for loop`

. - The

is used to exit the program execution.`return 0 statement`

- In order to avoid a negative input, a
`do…while loop`

can be used which requests the user to keep entering a number until a positive number is scanned as an input.