Program to compute sum 1 to N numbers using recursion

Program:
import java.util.*;
public class Sum
{
public static void main(String args[])
{
System.out.print("Enter any number:");
Scanner sc = new Scanner(System.in);
int number = sc.nextInt();
int sum = sumN(number);
System.out.println("Sum of " + number + " Numbers

using Recursion is : " + sum);
}
public static int sumN(int num)
{
int sum;
if(num==1) return(1);
else sum=num + sumN(num-1);
return sum;
}

Output:


Comments

Popular posts from this blog

Sequential File Allocation

Indexed File Allocation method

Linked File Allocation