Factorial
- 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.
- Check if the value is equal to zero.
- Give the main method “public static void main ()”
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)));
}
}
Enter an integer of any size: 50
Factorial of 50 is 30414093201713378043612608166064768844377641568960512000000000000
Want to see more full solutions like this?
Chapter 18 Solutions
Introduction to Java Programming and Data Structures, Comprehensive Version (11th Edition)
- (GREATEST COMMON DIVISOR) The greatest common divisor of integers x and y is the largest integer that evenly divides into both x and y. Write and test a recursive function gcd that returns the greatest common divisor of x and y. The gcd of x and y is defined recursively as follows: If y is equal to 0, then gcd (x, y) is x; otherwise, gcd (x, y) is gcd (y, x % y), where % is the remainder operator.arrow_forwardQ2) Note:- Please read the question carefully and type this java code as described in the given question and also include all the things that are asked to put in the code. Also please provide the output for this code. THANK YOU :)arrow_forward(Check password) Some websites impose certain rules for passwords. Write a method that checks whether a string is a valid password. Suppose the password rules are as follows: A password must have at least eight characters. A password consists of only letters and digits. A password must contain at least two digits. Write a program that prompts the user to enter a password and displays Valid Password if the rules are followed or Invalid Password otherwise.arrow_forward
- (YOU ARE NOT ALLOWED TO USE ARRAYLIST IN THIS PROJECT)Write a Java program to simulate a blackjack game of cards. The computer will play the role of the dealer. The program will randomly generate the cards dealt to the player and dealer during the game. Cards in this game will be represented by numbers 1 to 13 with Ace being represented by a 1. Remember, that face cards (i.e. Jack, Queen, and King) are worth 10 points to a hand while an Ace can be worth 1 or 11 points depending on the user’s choice. The numbered cards are worth their number value to the hand.arrow_forward(Intro to Java) Explain the answers to the below questions. include a written answer to the question using step-by-step explanation 1. Write a method called arrayTimesFive The method takes one array of doubles as a parameter It multiplies each element in the array by 5 and stores the result It returns nothingarrow_forwardName:- Mit (Question-1) Note:- Please type this java code and also need an output for this given Java program.arrow_forward
- - Use java RECURSION to print out this shape. - You need to call the method 2 times - use 2 or 3 for loops hints: - for (int i = 0; i < ?????; i++) sout(" ") - for (int i = 0; i < ?????; i++) sout(symbol) - put different if statements when you call yourself, one gets activated when you go up, one gets activated when you go down. - This method should be used; public static void printShape4(int row, char symbol, int currentRow) { } !!! do not use other methods, everything should be inside this methodarrow_forward(using recursive function) Write programs that implement the following equations п Σ Y = 7K k=0 **please answer quicklyarrow_forward(Intro to Java) Explain the answers to the below questions. include a written answer to the question using step-by-step explanation 4. Write the linearSearch method as follows using the algorithm provided in class: The method is named linearSearch It takes on array of doubles as a parameter It takes a double value to locate in the array as a second parameter It returns the integer location (index) in the array where the value is located Or, it returns -1 if the value cannot be foundarrow_forward
- def winning_card(cards, trump=None): Playing cards are again represented as tuples of (rank,suit) as in the cardproblems.pylecture example program. In trick taking games such as whist or bridge, four players each play one card from their hand to the trick, committing to their play in clockwise order starting from the player who plays first into the trick. The winner of the trick is determined by the following rules:1. If one or more cards of the trump suit have been played to the trick, the trick is won by the highest ranking trump card, regardless of the other cards played.2. If no trump cards have been played to the trick, the trick is won by the highest card of the suit of the first card played to the trick. Cards of any other suits, regardless of their rank, are powerless to win that trick.3. Ace is the highest card in each suit.Note that the order in which the cards are played to the trick greatly affects the outcome of that trick, since the first card played in the trick…arrow_forwardWrite a recursive function that displays the number of even and odd digits in an integer using the following header: void evenAndOddCount(int value) Write a test program that prompts the user to enter an integer and displays the number of even and odd digits in it.arrow_forward(Intro to Java) explain the answers to the below questions using step-by-step explanation.arrow_forward
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education