C Program to Find the Sum of First N Natural Numbers
Sum of Natural Numbers in C
In this article, we will see C Program to Find the Sum of First N Natural Numbers. A Natural number is the same as a Counting number. They are used to Count the number of real physical objects. Natural numbers start from 1 and go on infinite.
The positive numbers 1,2,3 ... are known as natural numbers. Formula for Sum of First n natural numbers is : n(n+1)/2. If you want to add first 5 Natural number then we find the Sum of 1+2+3+4+5 =15.
What we will learn
- Method 1: Iterative way
- Method 2: Direct Formula
- Method 3: Recursive Approach
Method 1
For an input n
- Create variable sum = 0
- Run a for loop in iteration (I) from 1 -> n
- Each time adding I to sum
- Print the sum value after the loop terminates
Code
Run
#include <stdio.h> int main() { int n; scanf("%d",&n); int sum = 0; for(int i=1;i<=n;i++) // is same as writing sum = sum + i sum += i; printf("Sum is %d",sum); return 0; } // Time complexity : O(n) // Space complexity : O(1)
Output
5 Sum is 15
Method 2
For an input n
- Create variable sum = 0
- Use formula sum = n(n+1)/2
- Print the sum value
Code
Run
#include <stdio.h> int main() { int n; scanf("%d",&n); int sum = n*(n+1)/2; printf("The sum is %d",sum); return 0; } // Time complexity : O(1) // Space complexity : O(1)
Output
6 Sum is 21
Method 3
For an input n
- Create variable sum = 0
- Call function getSum(sum, n)
- Base Case, n == 0 return sum
- Others return n + getSum(sum, n-1)
- Print the sum value in main
Code
Run
#include <stdio.h> int getSum(int sum,int n) { if(n==0) return sum; return n+getSum(sum,n-1); } int main() { int n, sum = 0; scanf("%d",&n); printf("%d",getSum(sum, n)); return 0; } // Time complexity : O(n) // Space complexity : O(1) // Auxilary space complexity : O(N) // Due to function call stack
Output
5 Sum is 15
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
Getting Started
- ASCII Table
- Positive or Negative number: C | C++ | Java | Python
- Even or Odd number: C | C++ | Java | Python
- Sum of First N Natural numbers: C | C++ | Java | Python
- Sum of N natural numbers: C | C++ | Java | Python
- Sum of numbers in a given range: C | C++ | Java | Python
- Greatest of two numbers: C | C++ | Java | Python
- Greatest of the Three numbers: C | C++ | Java | Python
- Leap year or not: C | C++ | Java | Python
- Prime number: C | C++ | Java | Python
- Prime number within a given range: C | C++ | Java | Python
- Sum of digits of a number: C | C++ | Java | Python
- Reverse of a number : C | C++ | Java | Python
- Palindrome number: C | C++ | Java | Python
- Armstrong number : C | C++ | Java | Python
- Armstrong number in a given range : C | C++ | Java | Python
- Fibonacci Series upto nth term : C | C++ | Java | Python
- Find the Nth Term of the Fibonacci Series : C | C++ | Java | Python
- Factorial of a number : C | C++ | Java | Python
- Power of a number : C | C++ | Java | Python
- Factor of a number : C | C++ | Java | Python
- Finding Prime Factors of a number : C | C++ | Java | Python
- Strong number : C | C++ | Java | Python
- Perfect number : C | C++ | Java | Python
- Automorphic number : C | C++ | Java | Python
- Harshad number : C | C++ | Java | Python
- Abundant number : C| C++ | Java | Python
- Friendly pair : C | C++ | Java | Python