Introduction to Java Programming and Data Structures: Brief Version (11th Global Edition)
Introduction to Java Programming and Data Structures: Brief Version (11th Global Edition)
11th Edition
ISBN: 9780134671710
Author: Y. Daniel Liang
Publisher: PEARSON
Question
Book Icon
Chapter 18, Problem 18.1PE
Program Plan Intro

Factorial

Program plan:

  • Import required packages
  • Declare the main class named “Main”.
    • Give the main method “public static void main ()”
      • Create an object “sc” for the scanner class.
      • Get a number from the user and store it in a variable “numberString”.
      • Create an object “bigNum” for the static method “BigInteger”.
      • Print the output.
    • Give function definition for the static method “factorial ()”.
      • Check if the value is equal to zero.
        • Return 1.
      • Else,
        • Call the function “factorial ()” recursively until the result is found.

Expert Solution & Answer
Check Mark
Program Description Answer

The below program is used to find the factorial for the given number using “BigInteger” class.

Explanation of Solution

Program:

//Import required packages

import java.math.*;

import java.util.*;

//Class name

public class Main

{

//Main method

public static void main(String[] args)

{

//Create an object for Scanner

Scanner sc = new Scanner(System.in);

//Print the message

System.out.print("Enter an integer of any size: ");

//Get the string from the user

String numberString = sc.nextLine();

//Create an object for the static method

BigInteger bigNum = new BigInteger(numberString);

//Print the output

System.out.println("Factorial of " + bigNum + " is " + factorial(bigNum));

}

//Static method "factorial"

public static BigInteger factorial(BigInteger value)

{

//Check if the value is equal to zero

if (value.equals(BigInteger.ZERO))

//Return 1

return BigInteger.ONE;

//Else

else

/*Call the function recursively to till the result is found*/

return value.multiply(factorial(value.subtract(BigInteger.ONE)));

}

}

Sample Output

Enter an integer of any size:  50

Factorial of 50 is 30414093201713378043612608166064768844377641568960512000000000000

Want to see more full solutions like this?

Subscribe now to access step-by-step solutions to millions of textbook problems written by subject matter experts!
Students have asked these similar questions
Write the following method
Do the trace in detail and submit  The source Code As Well From the following recursive method Trace (in details) the calling of the above method with any input from your choice such that thereturned value will be 19. public static int think(int x) {if(x<10)if(x%2!=0)return x;elsereturn 0;elseif(x%2!=0)return x%10 + think(x/10);elsereturn think(x/10);}
Fill in the missing statements.  See attached photo for the problem

Chapter 18 Solutions

Introduction to Java Programming and Data Structures: Brief Version (11th Global Edition)

Ch. 18.4 - Prob. 18.4.2CPCh. 18.4 - Prob. 18.4.3CPCh. 18.5 - Prob. 18.5.1CPCh. 18.5 - Prob. 18.5.2CPCh. 18.5 - What is a recursive helper method?Ch. 18.6 - Prob. 18.6.1CPCh. 18.6 - How does the program get all files and directories...Ch. 18.6 - How many times will the getSize method be invoked...Ch. 18.6 - Will the program work if the directory is empty...Ch. 18.6 - Will the program work if line 20 is replaced by...Ch. 18.6 - Will the program work if lines 20 and 21 are...Ch. 18.7 - Prob. 18.7.1CPCh. 18.8 - Prob. 18.8.1CPCh. 18.8 - Prob. 18.8.2CPCh. 18.8 - How many times is the displayTriangles method...Ch. 18.8 - Prob. 18.8.4CPCh. 18.8 - Prob. 18.8.5CPCh. 18.9 - Which of the following statements are true? a. Any...Ch. 18.9 - Prob. 18.9.2CPCh. 18.10 - Identify tail-recursive methods in this chapter.Ch. 18.10 - Rewrite the fib method in Listing 18.2 using tail...Ch. 18 - Prob. 18.1PECh. 18 - Prob. 18.2PECh. 18 - (Compute greatest common divisor using recursion)...Ch. 18 - (Sum series) Write a recursive method to compute...Ch. 18 - (Sum series) Write a recursive method to compute...Ch. 18 - (Sum series) Write a recursive method to compute...Ch. 18 - (Fibonacci series) Modify Listing 18.2,...Ch. 18 - Prob. 18.8PECh. 18 - (Print the characters in a string reversely) Write...Ch. 18 - (Occurrences of a specified character in a string)...Ch. 18 - Prob. 18.11PECh. 18 - (Print the characters in a string reversely)...Ch. 18 - (Find the largest number in an array) Write a...Ch. 18 - (Find the number of uppercase letters in a string)...Ch. 18 - Prob. 18.15PECh. 18 - (Find the number of uppercase letters in an array)...Ch. 18 - (Occurrences of a specified character in an array)...Ch. 18 - (Tower of Hanoi) Modify Listing 18.8,...Ch. 18 - Prob. 18.19PECh. 18 - (Display circles) Write a Java program that...Ch. 18 - (Decimal to binary) Write a recursive method that...Ch. 18 - (Decimal to hex) Write a recursive method that...Ch. 18 - (Binary to decimal) Write a recursive method that...Ch. 18 - (Hex to decimal) Write a recursive method that...Ch. 18 - Prob. 18.25PECh. 18 - (Create a maze) Write a program that will find a...Ch. 18 - (Koch snowflake fractal) The text presented the...Ch. 18 - (Nonrecursive directory size) Rewrite Listing...Ch. 18 - (Number of files in a directory) Write a program...Ch. 18 - (Game: Knights Tour) The Knights Tour is an...Ch. 18 - (Game: Knights Tour animation) Write a program for...Ch. 18 - (Game: Eight Queens) The Eight Queens problem is...Ch. 18 - Prob. 18.35PECh. 18 - (Sierpinski triangle) Write a program that lets...Ch. 18 - (Hilbert curve) The Hilbert curve, first described...Ch. 18 - (Recursive tree) Write a program to display a...Ch. 18 - Prob. 18.39PE
Knowledge Booster
Background pattern image
Recommended textbooks for you
Text book image
C++ Programming: From Problem Analysis to Program...
Computer Science
ISBN:9781337102087
Author:D. S. Malik
Publisher:Cengage Learning