C Program to Find the Sum of First N Natural Numbers

devquora
devquora

Posted On: Jan 26, 2023

C Program to Find the Sum of First N Natural Numbers

 

Overview:

In this article, We will discuss the C Program to Find the Sum of the First N Natural Numbers. First of all, I would like to explain what a natural number is. Then, I will demonstrate how to calculate the nth term of the natural number. Moreover, I would explain the logic of Finding the Sum of the First N Natural Numbers. Furthermore, I would write the logic and C Program to Find the Sum of the First N Natural Numbers with output.

Table of contents:

  1. What is a Number?
  2. What is Natural Number?
  3. What is the formula to find the Sum of the First N Natural Numbers?
  4. Demonstration to Find the Sum of First N Natural Numbers
  5. Logic to Find the Sum of First N Natural Numbers
  6. C Program to Find the Sum of First N Natural Numbers
  7. Conclusion

What is a Number?

A number is an object that uses digits to perform mathematical tasks. Calculus which is a branch of mathematics included many numbers such as integers, whole numbers, real numbers, and imaginary numbers. Here, I will discuss numbers to perform arithmetic addition operations. Moreover, a Number is a combination of digits, some symbols, and decimal points. For instance, 127.23 is a rational number. There are many numbers such as whole numbers, natural numbers(positive integers), negative integers, rational numbers, etc.

What is Natural Number?

Natural numbers are the numbers that are positive integers and include numbers from 1 to infinity(∞).

These numbers are countable and are generally used for calculation purposes.

The set of natural numbers is represented by the letter “N”.

N = {1,2,3,4,5,6,7,8,9,10……,∞}

What is the formula to find the Sum of the First N Natural Numbers?

Sums of the First n Natural Numbers

 N natural sum

The natural numbers are 1,2,3,4,... so on

The sum of the first n natural numbers, Sn is as follows:

1 + 2 + 3 + 4 + ... + n

Sn = 1 + 2 + 3 + 4 + ... + n

As we know, that

Sn=n[2a+(n−1)d]/2

According to 'n' natural numbers,

The first term = a = 1

The common difference = d = 1

∴ Sn=n[2×1+(n−1)×1]/2

Sn=n[2+n−1]/2

Sn=n[n+1]/2

Demonstration to Find the Sum of First N Natural Numbers

Let's look at this problem for n=1, 2, 3, 4, and 5 and calculate the sum:

1=1

1+2=3

1+2+3=6

1+2+3+4=10

1+2+3+4+5=15

What is the formula to calculate this sum of n natural numbers?

Demo n natural series

 The sum of n Natural numbers is calculated by using the following formula:

n natural sum

 

Logic to Find the Sum of First N Natural Numbers

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

N natural sum

C Program to Find the Sum of First N Natural Numbers

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

    printf("Enter the value of n(positive integer): ");
    scanf("%d",&n);

    for(count=1; count <= n; count++)
    {
        sum = sum + count;
    }

    printf("Sum of first %d natural numbers is: %d",n, sum);

    return 0;
}

The output of the Program with code:

When the above code is executed, it produces the following results:

Enter the value of n(positive integer): 6

The Sum of the first 6 natural numbers is: 21

Sum of natural Output

Conclusion:

This C program to add first N natural numbers includes a single header file: stdio.h to control standard input and output functions. As the program is executed, it asks for the value of the number to which the sum of natural numbers is to be found. Then, the numbers are added using for 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’.

    Please Login or Register to leave a response.

    Related Articles

    C Program Examples

    C Program to convert Decimal to Binary Number

    A Decimal Number is constructed with any digit from 0 to 9. For instance, 24 is a Decimal Number constructed from the digits 2 and 4. A Binary Number is constructed with digits 0 and 1. For instance, ..

    C Program Examples

    C program to reverse a string

    This program reversed the entered string that means opposite of the previous string sequence...

    C Program Examples

    C program to reverse an Integer number

    Are you want a demonstration of The C Program that reverses an Integer number? This Program reversing the sequence of digits in an Integer. After the successful compilation of the program, a message..