Created
December 31, 2023 11:01
-
-
Save iamgauravbisht/0fa0762924790a94287acfc435c4a4f8 to your computer and use it in GitHub Desktop.
Recursion
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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