Created
June 20, 2016 08:21
-
-
Save abdulateef/684990f682e05c629e00338f50763c9e to your computer and use it in GitHub Desktop.
Write a factorial function that takes a positive integer, N as a parameter and prints the result of N! ( N factorial).
This file contains 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
import java.io.*; | |
import java.util.*; | |
import java.text.*; | |
import java.math.*; | |
import java.util.regex.*; | |
public class Solution { | |
public static int factorial(int n)//factorial method | |
{ | |
if (n == 0) | |
{ | |
return 1; | |
}else if (n < 1) | |
{ | |
return -1; | |
}else | |
{ | |
return n * factorial(n -1); | |
} | |
} | |
public static void main(String[] args) { | |
Scanner input = new Scanner(System.in); | |
// System.out.println("Enter number to factorize "); | |
int n = input.nextInt(); | |
System.out.println(factorial(n) ); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment