Learn-dsa..in 30 days!



























CC-18 : Find sum of numbers from 1 to N.

Description:

Given an input num N find sum of numbers from 1 to N.

Test cases and expected outputs:

Input Parameters Expected outputs
N: 5 Sum 1 to N: 15
N: 9 Sum 1 to N: 45

Pseudocode:

sum1ToN(N):

If N==1:
return 1.
Else :
Return number calculated by adding N to return value from cum1ToN(N-1).

Code:

private int sum1ToN(int N){
	if (N == 1) {
		return 1;
	}
	int sum= N + sum1ToN(N-1);
	return sum;
}

Click here to download and run code and test cases !