Skip to content

Instantly share code, notes, and snippets.

@iamgauravbisht
Created December 31, 2023 11:01
Show Gist options
  • Save iamgauravbisht/0fa0762924790a94287acfc435c4a4f8 to your computer and use it in GitHub Desktop.
Save iamgauravbisht/0fa0762924790a94287acfc435c4a4f8 to your computer and use it in GitHub Desktop.
Recursion
package Recursion;
import java.util.Scanner;
public class Recursion {
//Factorial
public static int fact( int n ){
System.out.println("factorial of " + n);
if( n <= 1 ) return 1; //Base Condition
return n * fact(n-1);
}
//Homework
public static void Qone(int n) {
System.out.print(n);
if( n > 1) {
Qone(n-1);
}
}
public static int Qtwo(int n){
if( n <= 1 ) return 1; // Base Condition
return n + Qtwo(n-1);
}
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out.print("Enter a number : ");
int n = s.nextInt();
System.out.println("Factorial : " + fact(n));
Qone(n);
System.out.println("\n Addition : " + Qtwo(n));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment