Java program to find the sum of natural number using recursion


Program
import java.util.*; public class NaturalNumberSum { public static void main(String[] args) { Scanner s=new Scanner(System.in); System.out.println("Enter a number range to find its sum using recursion"); int n = s.nextInt(); int sum = RecursiveSum(n); System.out.println("Sum of all natural number is= " + sum); } public static int RecursiveSum(int n) { if (n != 0) return n + RecursiveSum(n - 1); else return n; } }
Input
Output