Sum of first N natural numbers in CAs we know natural numbers contain all the positive numbers starting from 1, 2, 3, to n numbers or infinity. For example, suppose when we calculate the sum of the first 25 numbers. That means we start adding the numbers from 1 to the given number 25, and the process is called the sum of the first N natural number. In this topic, we will learn how to find the sum of first n numbers using a C program. ![]() Mathematical FormulaFollowing is the representation to find the sum of n natural numbers using the mathematical formula: Where n defines the natural number. Suppose, we want to calculate the sum of the first 20 natural number, we need to put in a mathematical formula to get the sum: Pseudo code
Using for LoopLet's create a C program that determines the sum of n natural numbers using for loop. SumOfNaturalNumber1.c Output: Enter a positive number: 25 Sum of the first 25 number is: 325 Using while LoopLet's create a C program that determines the sum of n natural numbers using while loop. SumOfNaturalNumber2.c Output: Enter a positive number: 20 Sum of the first 20 natural number is: 210 In the above example, when we enter a positive number 20, the while loop continuously iterates over the counter value between i = 0 to 20. At each iteration, the value of i is added to the variable sum, and i is incremented by 1. When the while condition becomes false, it exits the loop and prints the sum of the first 20 natural numbers. Using do-while LoopLet us consider the following example to calculate the sum of natural number using Do while loop. SumOfNaturalNumber3.c Output: Enter a positive number: 30 Sum of the first 30 natural number is: 465 In the above example, when we enter a positive number 30, the do loop continuously iterates the counter value between i = 0 to 30. At each iteration, the value of i is added to the variable sum, and i is incremented by 1. When the while condition becomes false, it exits from the while loop and prints the sum of the first 30 natural numbers. Using the Mathematical FormulaLet's write a program to print the sum of n natural number using the mathematical formula. SumOfNaturalNumber4.c Output: Sum of 40 natural number is = 840 Using FunctionLet's consider the following example to calculate the sum of natural number using function in C. SumOfNaturalNumber5.c Output: Enter a natural number: 100 Sum of the 100 natural number is: 5050 Sum of n natural numbers Between a Given RangeCalculate the sum of n natural number from any starting number to the specify last number. SumOfNaturalNumber6.c Output: Enter the first number: 1 Up to the last number natural number: 25 Sum of natural number is = 325 Using RecursionLet's consider the following example to calculate the sum of natural number using recursion. SumOfNaturalNumber7.c Output: Enter any positive number to calculate the sum of natural no. 50 Sum of the first 50 natural number is: 1275 Using an ArraySumOfNaturalNumber8.c Output: Enter a positive number as we want to sum the natural number: 5 Enter the number one by one: 2 4 5 6 7 Sum of the given number is = 24
Next TopicWhat is getch() in C
|