Sum of n natural numbers
JavaScript
easy
10 mins
Given a number n, return the sum of the first n numbers. Return false for invalid inputs (non-number, negative numbers, or non-integer values).
Examples
Input: n = 5 Output: 15
Input: n = 0 Output: 0
Input: n = 1 Output: 1
Constraints
- Input must be a non-negative integer.
- Return false for invalid inputs.
Function Signature
function sumN(n) { // Your code here }
Test Cases
- Base cases: 0, 1
- Small numbers: 2, 3, 5
- Larger numbers: 10, 100
- Invalid inputs: -1, 2.5, "5", null, undefined, [], {}
Companies:
tcs
infosys
wipro
Accenture
Solve Similar questions 🔥
Want to upskill? Explore our courses!
Namaste DSA
Master DSA from scratch with numerous problems, and expert guidance.
Namaste React
Wanna dive deep into React and become Frontend Expert? Learn with me now!
Namaste Frontend System Design
The most comprehensive and detailed course for frontend system design.
Namaste Node.js
Wanna dive deep into Node.js? Enroll into `Namaste Node.js` now!
