Natural Number Sum
Definition & Explanation
Program to calculate the sum of the first N natural numbers. Natural numbers, also known as counting numbers, are used to represent the quantity of real physical objects. They commence from 1 and extend infinitely.
Example:
Input: N = 5
Output: Sum of the first 5 natural numbers is 15
Approaches to Find the Sum of First N Natural Numbers:
Method 1: Iterative Way
- Use a loop to iterate from 1 to N and accumulate the sum.
Method 2: Direct Formula
- Utilize the formula for the sum of the first N natural numbers: ( \text{Sum} = \frac{N \times (N + 1)}{2} ).
Method 3: Recursive Approach
- Define a recursive function to calculate the sum by adding the current number to the sum of the previous numbers until reaching N.
C
Method 1 : Â Iterative way
#include <stdio.h>
int main() {
// Input: Get the value of N from the user.
int n;
scanf("%d", &n);
// Initialize sum to 0.
int sum = 0;
// Iterate from 1 to N and accumulate the sum.
for (int i = 1; i <= n; i++) {
// Equivalent to sum = sum + i.
sum += i;
}
// Output: Print the calculated sum.
printf("Sum is %d", sum);
return 0;
}
// Time complexity: O(n) - linear time
// Space complexity: O(1) - constant space
Output :
7
Sum is 28
Method 2 : Direct Formula
#include <stdio.h>
int main() {
// Input: Get the value of N from the user.
int n;
scanf("%d", &n);
// Direct formula to calculate the sum of the first N natural numbers.
int sum = n * (n + 1) / 2;
// Output: Print the calculated sum.
printf("The sum is %d", sum);
return 0;
}
// Time complexity: O(1) - constant time
// Space complexity: O(1) - constant space
Output :
7
The sum is 28
Method 3 : Â Recursive Approach
#include <stdio.h>
// Recursive function to calculate the sum of the first N natural numbers.
int getSum(int sum, int n) {
if (n == 0)
return sum;
return n + getSum(sum, n - 1);
}
int main() {
// Input: Get the value of N from the user.
int n, sum = 0;
scanf("%d", &n);
// Output: Print the calculated sum using the recursive function.
printf("%d", getSum(sum, n));
return 0;
}
// Time complexity: O(n) - linear time
// Space complexity: O(1) - constant space
// Auxiliary space complexity: O(N) - due to function call stack
Output :
Enter the value of N: 7
28
C++
Method 1 : Iterative way
#include <iostream>
int main() {
// Input: Get the value of N from the user.
int n;
std::cin >> n;
// Initialize sum to 0.
int sum = 0;
// Iterate from 1 to N and accumulate the sum.
for (int i = 1; i <= n; i++) {
// Equivalent to sum = sum + i.
sum += i;
}
// Output: Print the calculated sum.
std::cout << "Sum is " << sum;
return 0;
}
// Time complexity: O(n) - linear time
// Space complexity: O(1) - constant space
Output :
Enter the value of N: 7
Sum is 28
Method 2: Direct Formula (C++)
#include <iostream>
int main() {
// Input: Get the value of N from the user.
int n;
std::cin >> n;
// Direct formula to calculate the sum of the first N natural numbers.
int sum = n * (n + 1) / 2;
// Output: Print the calculated sum.
std::cout << "The sum is " << sum;
return 0;
}
// Time complexity: O(1) - constant time
// Space complexity: O(1) - constant space
Output :
Enter the value of N: 7
Sum is 28
Method 3: Recursive Approach (C++)
#include <iostream>
// Recursive function to calculate the sum of the first N natural numbers.
int getSum(int sum, int n) {
if (n == 0)
return sum;
return n + getSum(sum, n - 1);
}
int main() {
// Input: Get the value of N from the user.
int n;
std::cin >> n;
// Output: Print the calculated sum using the recursive function.
std::cout << getSum(0, n);
return 0;
}
// Time complexity: O(n) - linear time
// Space complexity: O(1) - constant space
// Auxiliary space complexity: O(N) - due to function call stack
Output :
Enter the value of N: 7
Sum is 28
JAVA
Method 1: Iterative Way (Java)
import java.util.Scanner;
public class SumIterative {
public static void main(String[] args) {
// Input: Get the value of N from the user.
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the value of N: ");
int n = scanner.nextInt();
// Initialize sum to 0.
int sum = 0;
// Iterate from 1 to N and accumulate the sum.
for (int i = 1; i <= n; i++) {
// Equivalent to sum = sum + i.
sum += i;
}
// Output: Print the calculated sum.
System.out.println("Sum is " + sum);
}
}
Output :
Enter the value of N: 7
Sum is 28
Method 2: Direct Formula (Java)
import java.util.Scanner;
public class SumDirectFormula {
public static void main(String[] args) {
// Input: Get the value of N from the user.
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the value of N: ");
int n = scanner.nextInt();
// Direct formula to calculate the sum of the first N natural numbers.
int sum = n * (n + 1) / 2;
// Output: Print the calculated sum.
System.out.println("The sum is " + sum);
}
}
Output :
Enter the value of N: 7
Sum is 28
Method 3: Recursive Approach (Java)
import java.util.Scanner;
public class SumRecursive {
// Recursive function to calculate the sum of the first N natural numbers.
public static int getSum(int sum, int n) {
if (n == 0)
return sum;
return n + getSum(sum, n - 1);
}
public static void main(String[] args) {
// Input: Get the value of N from the user.
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the value of N: ");
int n = scanner.nextInt();
// Output: Print the calculated sum using the recursive function.
System.out.println(getSum(0, n));
}
}
Output :
Enter the value of N: 7
Sum is 28
Python
Method 1: Iterative Way (Python)
# Input: Get the value of N from the user.
n = int(input("Enter the value of N: "))
# Initialize sum to 0.
sum_val = 0
# Iterate from 1 to N and accumulate the sum.
for i in range(1, n+1):
# Equivalent to sum = sum + i.
sum_val += i
# Output: Print the calculated sum.
print("Sum is", sum_val)
Output :
Enter the value of N: 7
Sum is 28
Method 2: Direct Formula (Python)
# Input: Get the value of N from the user.
n = int(input("Enter the value of N: "))
# Direct formula to calculate the sum of the first N natural numbers.
sum_val = n * (n + 1) // 2
# Output: Print the calculated sum.
print("The sum is", sum_val)
Output :
Enter the value of N: 7
Sum is 28
Method 3: Recursive Approach (Python)
# Recursive function to calculate the sum of the first N natural numbers.
def get_sum(sum_val, n):
if n == 0:
return sum_val
return n + get_sum(sum_val, n - 1)
# Input: Get the value of N from the user.
n = int(input("Enter the value of N: "))
# Output: Print the calculated sum using the recursive function.
print(get_sum(0, n))
Output :
Enter the value of N: 7
Sum is 28