Monday, 9 January 2017

Chapter 18 Exercise 1, Introduction to Java Programming, Tenth Edition Y. Daniel LiangY.

18.1 (Factorial)
Using the BigInteger class introduced in Section 10.9, you can
find the factorial for a large number (e.g., 100!). Implement the
factorial method using recursion. Write a program that prompts the
user to enter an integer and displays its factorial.

import java.math.BigInteger;
import java.util.Scanner;

public class Exercise_01 {

    public static void main(String[] args) {

        System.out.print("Enter an integer: ");
        int n = new Scanner(;

        System.out.println("It's factorial is: " + factorial(new BigInteger(n+"")));

    private static BigInteger factorial(BigInteger n) {
        if (n.compareTo(BigInteger.ONE) == 0)
            return BigInteger.ONE;
            return n.multiply(factorial(n.subtract(BigInteger.ONE)));


No comments :

Post a Comment