Program to calculate the sum of first n natural numbers using while loop

C Programming Language / Loop control in C Language

2184

Program:

// Program to calculate the sum of first n natural numbers
// Positive integers 1,2,3...n are known as natural numbers

#include <stdio.h>
int main()
{
    int num, count, sum = 0;

    printf("Enter a positive integer: ");
    scanf("%d", &num);
    
      count = 1; 
     
    while(count <= num)
    {
        sum += count;
        ++count;
    }

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

    return 0;
}

Output:

Enter a positive integer: 10
Sum = 55
Press any key to continue . . .

Explanation:

Program to calculate the sum of first n natural numbers using while loop

This Particular section is dedicated to Programs only. If you want learn more about C Programming Language. Then you can visit below links to get more depth on this subject.