import java.util.*;
import java.lang.*;
import java.io.*;
// The main method must be in a class named "Main".
class Main {
// 5 to 1
static void PrintNum(int n) // agr hum yha void ki jgh int use krte toh return ke sath koi integer bhinjata main fn me yha return ke sath kuchh nhi ja rha h
{
if (n==0)
{ return;
} // humne condition isliye lagayi taki recursion infinity time run na ho
// isse hum base case khte h
System.out.println(n); //ab yha aakr 5 print kr dega then
// fir ab 4 print kr dega then
PrintNum(n-1);// then 4 ho jayega
// then 3 hoga eshe 0 hoga tb return ho jayega
}
static void PrintNum1(int n){
if (n>5)
{ return;
}
System.out.println(n);
PrintNum1(n+1);
}
public static void main(String[] args) {
System.out.println("5 to 1 using recursion");
int a = 5;
// now we call the function
PrintNum(a);
// we can also use int n there there is no connection between function and main function int
System.out.println("now 1 to 5");
int b = 1;
PrintNum1(b);
}
}
To embed this program on your website, copy the following code and paste it into your website's HTML: