# Program for Sum of Natural Numbers

## Sum of Natural Numbers

We will write a C program sum of natural numbers and the number will be entered by the user. This will help to understand the basic structure of programming. In this program , we will display the calculation of sum of natural numbers easily by using proper syntax and algorithms.

## Working of Program :

In the program, we will require some numbers from the user to display the calculation of sum of natural numbers.

### Important Note :

• The number which starts from one and end with infinity then that number is natural number.
• Natural number doesn’t include zero.

### Syntax of for() Loop:-

```for (Initialization, condition, updation)
{
//code
}```

### Problem 1

Write a program to calculate the sum of n natural number using for loop.

• Firstly, we have to enter the number.
• Then print the sum of natural numbers.

### Code

Run
```#include<stdio.h>
int main ()
{
int n = 5, i, sum = 0;
for (i = 1; i <= n; ++i)
{
sum += i;
}
printf ("Sum of natural number = %d", sum);
return 0;
}
```

### Output

```Sum of natural number = 15
```

### Note:

In the following program we will display the calculation of sum of natural number using while() loop.

### Syntax for while() Loop:-

```while(condition)
{
// Statements
// Increment / Decrement
}
```
```Problem 2
```

Write a program to calculate the sum of n natural number using while loop.

• Firstly, we have to enter the number.
• Then print the sum of natural numbers.

### Code

Run
```#include<stdio.h>
int main ()
{
int n = 6, i, sum = 0;
i = 1;
while (i <= n)
{
sum += i;
++i;
}
printf ("Sum of natural numbers = %d", sum);
return 0;
}
```

### Output

```Sum of natural numbers = 21
```

### Syntax for do-while() Loop:-

```do
{
// Statements
// Increment / Decrement
}while(condition)
```

### Problem 3

Write a program to calculate the sum of n natural number using do-while loop.

• Firstly, we have to enter the number.
• Then print the sum of natural numbers.

### Code

Run
```#include<stdio.h>
int main ()
{
int n = 4, i, sum = 0;
i = 1;
do
{
sum += i;
++i;
}
while (i <= n);
printf ("Sum of natural numbers = %d", sum);
return 0;
}
```

### Output

```Sum of natural numbers = 10
```

### Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

## Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others