Skip to content

Instantly share code, notes, and snippets.

@abdulateef
Created June 20, 2016 08:21
Show Gist options
  • Save abdulateef/684990f682e05c629e00338f50763c9e to your computer and use it in GitHub Desktop.
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).
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