Sum of first N numbers

Beginner Problems Basic Recursion Easy
  • Fun Fact: The concept of summing up the first N natural numbers is often used in the development of performance analysis and benchmarking tools
  • These tools help developers understand the performance of their software by simulating a series of operations, such as calculating how long it takes to sum up a series of numbers, and then analyze the time complexity
  • The principle also has real-world usages in databases when dealing with sequence generation or serial numbers, and in building progress bar logic in numerous softwares or apps

Given an integer N, return the sum of first N natural numbers. Try to solve this using recursion.

Examples:

Input : N = 4

Output : 10

Explanation : first four natural numbers are 1, 2, 3, 4.

Sum is 1 + 2 + 3 + 4 => 10.

Input : N = 2

Output : 3

Explanation : first two natural numbers are 1, 2.

Sum is 1 + 2 => 3.

Input : N = 10

Constraints

  • 1 <= N <= 103

Company Tags

TCS Cognizant Accenture Infosys Capgemini Wipro IBM HCL Tech Mahindra MindTree