Sum of first N natural numbers in C17 Mar 2025 | 6 min read As 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 |
Perfect Number Program in C
Perfect Number In mathematics, a perfect number is a positive integer that is equal to the sum of its positive divisors, excluding the number itself. For example, 6 is a positive number that is completely divisible by 1, 2, and 3. We know that the number is also...
6 min read
abs() function in C
In this topic, we will discuss the abs function in the C programming language. The abs () function is a predefined function in the stdlib.h header file to return the absolute value of the given integers. So, if we want to return the absolute value of...
4 min read
Character Set in C
Introduction: Character Set is a collection of permissible characters that can be used in a variety of contexts by the program. In this article, we covered the history of Character encoding's. Here, we also discuss the historical encoding system known as EBCDIC, the current coding standard Unicode,...
12 min read
Order of Complexity in C
Order of Complexity is a term used in computer science to measure the efficiency of an algorithm or a program. It refers to the amount of time and resources required to solve a problem or perform a task. In programming, the Order of Complexity is usually...
4 min read
Formatted and Unformatted Input Output in C
Computer programming requires input/output (I/O) operations. Using I/O operations, the data is read and written to and from various sources, including files, keyboards, and screens. I/O operations can be either formatted or unformatted in the C computer language. In this blog post, we will cover the...
4 min read
How to use Pointers in C language
? Pointers are like normal variables, but in the place of storing variable value, pointers store the address of another variable or another pointer. A pointer can hold the addresses of variable of different data types-integer, char, or even an array. When a pointer is holding an...
9 min read
Pattern Matching Algorithm in C
Pattern Matching is widely used in computer science and many other fields. Pattern Matching algorithms are used to search for patterns within a larger text or data set. One of the most popular algorithms for pattern matching is the Boyer-Moore algorithm, which was first published in...
4 min read
How to create your own header files in C
In this article, you will learn how to create your own header files in C with the help of given steps and examples. It is standard practice in C to create your header file to declare functions, data structures, constants, and other declarations that can be shared...
6 min read
Add Node to Linked List in C
A linked list is a data structure used in computer programming that consists of a sequence of elements, each containing a reference (link) to the element. Unlike an array, the elements in a linked list are not stored in contiguous memory locations. Instead, each element...
4 min read
Palindrome Number
Palindrome program in C Palindrome number in c: A palindrome number is a number that is same after reverse. For example 121, 34543, 343, 131, 48984 are the palindrome numbers. Palindrome number algorithm Get the number from user Hold the number in temporary variable Reverse the number Compare the temporary number...
1 min read
We request you to subscribe our newsletter for upcoming updates.

We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India
