Problem: Java Program To Find Factorial
Of A Number
import java.util.Scanner;
class Factorial {
public static void main(String args[]) {
int n, c, fact = 1;
System.out.print("Enter An Integer
To Calculate It's Factorial: ");
Scanner s = new Scanner(System.in);
n = s.nextInt();
if ( n < 0 )
System.out.println("Number Must Be
Non-negative.");
else
{
for ( c = 1 ; c <= n ; c++ )
fact = fact*c;
System.out.println("Factorial Of
"+n+" Is = "+fact);
s.close();
}}}
Problem: Java Program To Find
Factorial Of A Number Using Recursion
import java.util.*;
public class FactorialRecursion {
public static void main(String args[]) {
int num, factorial = 1;
Scanner s = new Scanner(System.in);
System.out.print("Enter An Integer
To Calculate It's Factorial: ");
num = s.nextInt();
if ( num < 0 )
System.out.println("Number Must Be
Non-negative.");
factorial = getfactorial(num);
System.out.println(num + "! =
" + factorial);
s.close();
}}
public static int getfactorial(int num)
{
if (num <= 1)
return 1;
return num * getfactorial(num-1);
}}
Problem: Java Program For Calculating
Factorial Of Large Numbers
import java.util.*;
import java.math.BigInteger;
class FacorialOfBigNuumber {
public static void main(String args[]) {
int n=0, c;
BigInteger inc = new
BigInteger("1");
BigInteger fact = new
BigInteger("1");
Scanner input = new Scanner(System.in);
if ( n < 0 )
System.out.println("Number Must Be
Non-negative.");
else {
System.out.print("Enter An Integer:
");
n = input.nextInt();
for (c = 1; c <= n; c++) {
fact = fact.multiply(inc);
inc = inc.add(BigInteger.ONE);
}
System.out.println(n + "! = "
+ fact);
input.close();
}}}