Introduction to Java Programming and Data Structures, Comprehensive Version, Student Value Edition (11th Edition)
Question
Book Icon
Chapter 18, Problem 18.1PE
Expert Solution & Answer
Check Mark
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.
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
Need help writing code to answer this question in Python! (image attached)
Need help with python code! How do I simplify my code for a beginner to understand, simple fixed format and centering? Such as:  print(f"As an int variable: {age_int:^7}") print(f"In numeric binary: {age_int:^7b}") My Code:name = input("Enter your name: ")print(f"In text name is: {' '.join(name)}")decimal_values = []binary_values = []for letter in name:   ascii_val = ord(letter)   binary_val = format(ascii_val, '08b')   decimal_values.append(str(ascii_val))   binary_values.append(binary_val)# Loop through each letter:print(f"In ASCII decimal: {' '.join(decimal_values)}")print(f"In ASCII binary: {' '.join(binary_values)}")# Ageage_str = input("Enter your age: ")age_int = int(age_str)print(f"As a string \"{age_str}\": {' '.join(age_str)}")age_decimal_values = []age_binary_values = []for digit in age_str:   ascii_val = ord(digit)   binary_val = format(ascii_val, '07b')   age_decimal_values.append(str(ascii_val))   age_binary_values.append(binary_val)print(f"In ASCII decimal: {'…
Don't use chatgpt or any other AI

Chapter 18 Solutions

Introduction to Java Programming and Data Structures, Comprehensive Version, Student Value Edition (11th Edition)

Chapter 18.4, Problem 18.4.2CPChapter 18.4, Problem 18.4.3CPChapter 18.5, Problem 18.5.1CPChapter 18.5, Problem 18.5.2CPChapter 18.5, Problem 18.5.3CPChapter 18.6, Problem 18.6.1CPChapter 18.6, Problem 18.6.2CPChapter 18.6, Problem 18.6.3CPChapter 18.6, Problem 18.6.4CPChapter 18.6, Problem 18.6.5CPChapter 18.6, Problem 18.6.6CPChapter 18.7, Problem 18.7.1CPChapter 18.8, Problem 18.8.1CPChapter 18.8, Problem 18.8.2CPChapter 18.8, Problem 18.8.3CPChapter 18.8, Problem 18.8.4CPChapter 18.8, Problem 18.8.5CPChapter 18.9, Problem 18.9.1CPChapter 18.9, Problem 18.9.2CPChapter 18.10, Problem 18.10.1CPChapter 18.10, Problem 18.10.2CPChapter 18, Problem 18.1PEChapter 18, Problem 18.2PEChapter 18, Problem 18.3PEChapter 18, Problem 18.4PEChapter 18, Problem 18.5PEChapter 18, Problem 18.6PEChapter 18, Problem 18.7PEChapter 18, Problem 18.8PEChapter 18, Problem 18.9PEChapter 18, Problem 18.10PEChapter 18, Problem 18.11PEChapter 18, Problem 18.12PEChapter 18, Problem 18.13PEChapter 18, Problem 18.14PEChapter 18, Problem 18.15PEChapter 18, Problem 18.16PEChapter 18, Problem 18.17PEChapter 18, Problem 18.18PEChapter 18, Problem 18.19PEChapter 18, Problem 18.20PEChapter 18, Problem 18.21PEChapter 18, Problem 18.22PEChapter 18, Problem 18.23PEChapter 18, Problem 18.24PEChapter 18, Problem 18.25PEChapter 18, Problem 18.26PEChapter 18, Problem 18.27PEChapter 18, Problem 18.28PEChapter 18, Problem 18.29PEChapter 18, Problem 18.32PEChapter 18, Problem 18.33PEChapter 18, Problem 18.34PEChapter 18, Problem 18.35PEChapter 18, Problem 18.36PEChapter 18, Problem 18.37PEChapter 18, Problem 18.38PEChapter 18, Problem 18.39PE
Knowledge Booster
Background pattern image
Similar questions
SEE MORE QUESTIONS
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
Text book image
C++ for Engineers and Scientists
Computer Science
ISBN:9781133187844
Author:Bronson, Gary J.
Publisher:Course Technology Ptr
Text book image
Microsoft Visual C#
Computer Science
ISBN:9781337102100
Author:Joyce, Farrell.
Publisher:Cengage Learning,
Text book image
Programming Logic & Design Comprehensive
Computer Science
ISBN:9781337669405
Author:FARRELL
Publisher:Cengage
Text book image
EBK JAVA PROGRAMMING
Computer Science
ISBN:9781337671385
Author:FARRELL
Publisher:CENGAGE LEARNING - CONSIGNMENT