Java Recursion Program Practise 4 - to input a number and display sum from 1 to that number
Java Program to input a number and display sum from 1 to that number using Recursion
import java.util.*;
class Main {
int add(int n){
if(n>0){
return (n + add(n-1));
}
else
return 0;
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int a;
System.out.println("Enter the number of which the sum is desired from 1 to the number n");
a = in.nextInt();
Main ob = new Main();
System.out.println( ob.add(a));
}
}
Comments
Post a Comment