-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRecursionBasic.java
More file actions
33 lines (30 loc) · 845 Bytes
/
RecursionBasic.java
File metadata and controls
33 lines (30 loc) · 845 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
// Program: Sum of Natural Numbers Using Recursion
// Topic: Recursion and Mathematical Computation
// Description: Calculates the sum of the first ‘n’ natural numbers using a recursive method `sum()`.
// Demonstrates base and recursive conditions where `sum(k)` returns `k + sum(k-1)` until `k` becomes 0.
// The program prints the total sum for a given input value.
package recursion;
/**
*
* @author Samim
*/
public class RecursionBasic {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
int result=sum(40);
System.out.println(result);
}
public static int sum(int k)
{
if(k>0)
{
return k+sum(k-1);
}
else
{
return 0;
}
}
}