Live Batches
Masterclasses
Menu
Free Courses
Account
Login / Sign Up

Sum Of First N Natural Numbers

Easy Acceptance 60.00% Points 20.00

You are given a positive integer n. Your task is to find and print the sum of the first n natural numbers, that is, add all numbers starting from 1 up to n and return the final sum.

The result should be displayed as a single integer.

Examples
Example 1

Example 1

Input: 5

Output: 15

Explanation: 1+2+3+4+5 = 15

Example 2

Example 2

Input: 10

Output: 55

Explanation: 1+2+3+4+5+6+7+8+9+10 = 55

Hints
Hint 1
N/A
Constraints
  • 1 < n < 106
  • n is a positive integer.
Companies
Microsoft Samsung Walmart
Topics
Recursion
Solution.cs C#JavaPythonC++Javascript

Unlock the code editor

Sign in to write, run, and submit your solution against the full test suite.

  • Run code against sample & hidden test cases
  • Save submissions and track your streak
  • Compare with editorial & community solutions