Skip to main content

program for printing the 1 to n using recursion in java

 program for printing the 1 to n using recursion in java


Code:

import java.util.Scanner;

public class Test{
public static void main(String[] args) {
Scanner scn = new Scanner(System.in);
int n = scn.nextInt();
print(n);
}
public static void print(int n){
if(n==0){
return;
}
print(n-1);
System.out.println(n);
}
}

i/p: 5
o/p:

1
2
3
4
5

Comments