Natural numbers are all positive integers ranging from 1 to n or infinity. Sum of first n natural number, for any number n, sum is defined as 1+2+3—-n, which is an arithmetic series whose sum is (n * (n + 1))/2. For example, If n=3, sum of the first 3 natural numbers is 1 + 2 + 3 = 6. Formula: Formula to calculate the Sum of First N Natural Numbers are (n * (n + 1))/2. Example: If n=4, sum of the first 4 natural numbers is => (n * (n + 1))/2 => (4 * (4 + 1))/2 => 20/2=> 10 Write a C program to find the sum of first n natural numbers. 1. Take a number as input. 2. Find the sum of first n natural numbers using loops, function and formula. 3. Print the final result and exit. Sum of First N Natural Numbers in C can be found out in following ways: Method 1: (Using For Loop) In this approach, we find the sum of the first n natural number using a for loop and in each iteration, and add ‘i’ to the sum, where i is the variable which is used to initialize the loop and sum is the variable which stores the sum of n natural numbers. Example:
Check this: Computer Science Books | Advanced C Programming Videos Let n = 4 Initially sum = 0, Iterate from i=1 to i=4 using a for loop, and in each iteration add i to sum. So sum = 1+2+3+4 = 10. Algorithm: Step 1: Start the program. Step 2: Input the value of n. Step 3: Initialize a for loop from 1 to n. Step 4: Print sum of first n natural numbers. Step 5: End the Program. Here is source code of the C program to calculate the sum of first N natural numbers using for loop. The C program is successfully compiled and run on a Linux system. The program output is also shown below.
1. Input a number n Time Complexity: O(n) Space Complexity: O(1) To find the sum of the first n natural numbers, we enter the value “4” as input. Enter number: 4 sum of first 4 numbers is: 10 Method 2: (Using While Loop) In this approach, we find the sum of the first n natural number using a while loop. Initially i=1, execute loop until i<=n and in each iteration add i to sum and increment i. Example: Let n = 5 Initially, sum = 0 and i = 1 Iterate until i<=5 using a while loop and in each iteration add i to sum. So sum = 1+2+3+4+5=15 Algorithm: Step 1: Start the program. Step 2: Input the value of n. Step 3: Initialize a variable i and run a while loop from 1 to n. Step 4: Print sum of first n natural numbers. Step 5: End the Program. Here is source code of the C program to calculate the sum of first N natural numbers using while loop. The C program is successfully compiled and run on a Linux system. The program output is also shown below.
1. Input a number n Time Complexity: O(n) Space Complexity: O(1) To find the sum of the first n natural numbers, we enter the value “5” as input. Enter number: 5 sum of first 5 numbers is: 15 Method 3: (Using Do While Loop) In this approach, we find the sum of the first n natural number using a do while loop. Initialize a variable i and run a Do while loop from 1 to n. After the loop is executed, sum contains the sum of first n natural numbers. Example: Let n=6 Initially, sum = 0 and i=1, Iterate until i<=6 using a do while loop and in each iteration add i to sum. So sum = 1+2+3+4+5+6=21 Here is source code of the C program to calculate the sum of first N natural numbers using do while loop. The C program is successfully compiled and run on a Linux system. The program output is also shown below.
1. Input a number n Time Complexity: O(n) Space Complexity: O(1) To find the sum of the first n natural numbers, we enter the value “6” as input. Enter number: 6 sum of first 6 numbers is: 21 Method 4: (Using Formula) For any integer n, the sum is 1+2+3 —— n. This corresponds to an arithmetic series whose sum is (n *(n+1))/2. So in this approach, sum is calculated using the formula: (n *(n+1))/2. Example: If n=7, sum = (7 * (7 + 1))/2 = 56/2 = 28 Here is source code of the C program to calculate the sum of first N natural numbers using formula. The C program is successfully compiled and run on a Linux system. The program output is also shown below.
1. Input a number n Time Complexity: O(1) Space Complexity: O(1) To find the sum of the first n natural numbers, we enter the value “7” as input. Enter number: 7 sum of first 7 numbers is: 28 Method 5: (Using Recursion) The prototype of the recursive function is: int sum(int n). Following is the function definition: int sum(int n) { if(n==1) return 1; else return n + sum(n-1); } Example: Now let n=5; So function calls would be as follows. sum(5) = 5 + sum(4) —— ——— ——— ——— ——— ——— 15 sum(4) = 4 + sum(3) ——— ——— ————— —— 10 sum (3) = 3 + sum(2) ——— ——— —- 6 sum(2) = 2 + sum(1) ——— 3 sum(1) = 1 // Base Case Algorithm: Step 1: Start the program. Step 2: Input the value of n. Step 3: Declare a function func to find sum.
Step 4: Print the value of sum as returned by the func. Here is source code of the C program to calculate the sum of first N natural numbers using recursion. The C program is successfully compiled and run on a Linux system. The program output is also shown below.
1. Input a number n Time Complexity: O(n) Space Complexity: O(1) To find the sum of the first n natural numbers, we enter the value “5” as input. Enter number: 5 sum of first 5 numbers is: 15 Method 6: (Using Function) The prototype of the recursive function is: int sum_n(int n). It takes n as the parameter and returns the sum of first n natural numbers. Algorithm: Step 1: Start the program. Step 2: Input the value of n. Step 3: Declare a function func to find sum. Step 4: Print the value of sum as returned by the func. Step 5: End the Program. Here is source code of the C program to calculate the sum of first N natural numbers using function. The C program is successfully compiled and run on a Linux system. The program output is also shown below.
1. Input a number n Time Complexity: O(n) Space Complexity: O(1) To find the sum of the first n natural numbers, we enter the value “120” as input. Enter number: 15 sum of first 15 numbers is: 120 In this approach, we store the natural numbers in an array then find the sum of the array which is equal the sum of first n natural numbers. Example: If n=5; Sum of array = 1+2+3+4+5=15; Sum of first 5 natural numbers=sum of array=15 Here is source code of the C program to calculate the sum of first N natural numbers using array. The C program is successfully compiled and run on a Linux system. The program output is also shown below.
1. Input a number n Time Complexity: O(n) Space Complexity: O(1) To find the sum of the first n natural numbers, we enter the value “5” as input. Enter number: 5 sum of first 5 numbers is: 15 Method 8: Sum of N Natural Numbers in a Given Range In this approach we traverse between the intervals using the iterative statement to find the sum of natural numbers in the interval. Example: Let low = 2; high = 9; So sum = 2+3+4+5+6+7+8+9 (from low to high) sum = 44. Here is source code of the C program to calculate the sum of N natural numbers in a Given Range. The C program is successfully compiled and run on a Linux system. The program output is also shown below.
1. Input the two ends of the interval i.e high and low. Time Complexity: O(n) Space Complexity: O(1) To find the sum of n natural numbers in a given range, we enter the values low = “2” and high = “9” as input. Enter ends of interval to find sum:2 9 Sum is: 44 To practice programs on every topic in C, please visit “Programming Examples in C”, “Data Structures in C” and “Algorithms in C”. Next Steps:
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn. Subscribe to his free Masterclasses at Youtube & technical discussions at Telegram SanfoundryClasses. |