C Program to Find Sum of First N Natural Numbers

4 Min Read

Finding the sum of first N natural numbers is a very popular algebra as well as programming problem from high school to university level. As the problem carries a certain amount of significance in logic building of several softwares, scientific research works and engineering calculations etc., the study of this problem is essential in all streams of physical science.

In C programming, the sum of first N natural numbers can be found by either using loop or without loop. Before going to the source codes, I’ve presented an algorithm (plus pseudo code) to find the sum of first N natural numbers. This algorithm can be used to write code for the given problem in any other programming language as well.

Algorithm:

  1. Start
  2. Declare and initialize variable, i,num, s=0.
  3. Enter the value of num i.e. number upto which sum is to be calculated
  4. Use Loop to add number. Any of while, do-while or for loop can be used.
  5. Print the sum
  6. Stop

Pseudo Code:

  • int i, num, s=0
  • input num
  • i=0
  • do
    s=s+i
    i=i+1
    till the value of i<=num
  • print s as the sum of numbers.

Finding Sum of First N Natural Numbers in C:

Using Do While Loop:

//This source code uses do while loop

#include<stdio.h>
int main()
{
    int i, num, s = 0;// declaration of variables and initialization of sum to 0
    printf("\nEnter the number to which sum is to be calculated: ");
    scanf("%d",&num);
    
    i=0;
    do
    {
        s=s+i;
        i=i++;
    }
    while(i<=num);
    
    printf("\n The sum of first %d natural number is %d\n\n", num,s);
    return 0;

}

This C program to add first N natural numbers includes a single header file: stdio.h to control standard input and output function. As the program is executed, it asks for the value of the number to which the sum of natural numbers is to be found out. Then, the numbers are added using do-while loop. It can also be done with for and while loop as well. Finally, it displays the sum of numbers stored in integer variable ‘s’.

Without Using Loop – Using Formula:

//This source code uses formula: Sn = n(n+1)/2

#include<stdio.h>
main()
{
    int num, sum;
    printf("\n Enter the number: ");
    scanf("%d",&num);
    sum = num*(num+1)/2;
    printf("\n\n The sum of natural number is: %d\n ", sum);
}

In this source code, there is no need of loop. It asks for the value of number and calculates the sum using formula:  Sn = n(n+1)/2.

Also see,
Solving Quadratic Equations in C
Displaying Prime Numbers in C

Both these source code presented here are short, simple and easy to understand. They are compiled in Code::Blocks IDE and are bug-free. If you have any queries or suggestions regarding this post or the source code, discuss them below in the comments section.

You can find more C Tutorials here.

Share This Article
Leave a comment

Leave a Reply

Your email address will not be published. Required fields are marked *

English
Exit mobile version