Question

In: Computer Science

In Java please What happens if you call factorial( with a negative value of n? With...

In Java please
What happens if you call factorial( with a negative value of n? With a large value of say 35?
Write a recursive function that takes an integer n as its argument and returns ln(n!)

Solutions

Expert Solution

Below is the Java Solution with output screenshot

Code :

import java.util.Scanner;

public class Solution {
    // main method
    public static void main(String[] args) {
        int n;
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter a number: ");
        n = sc.nextInt();
        System.out.println("Factorial of " + n + " = " + factorial(n));
    }

    // recursive factorial function
    public static int factorial(int n){
        if(n<=0){
            return 1;
        }
        return n*factorial(n-1);
    }
}

Output :

Case 1: if we call factorial() function with negative value

Answer : The factorial is the product of all positive integers less than or equal to n. Hence we can't calculate factorial of negative numbers. and if we do pass a negative value we will get an error.

Case 2 : pass large number like 35 in factorial function

Factorial of a large number like 35 is a very big number (~1040) which can't be stored in integer value because integers can only store values up to 109 range

Hence if we call factorial function with large value i.e factorial(35) then we will get 0 output because of integer overflow.


Related Solutions

In C++ Let n be a non-negative integer. The factorial of n, written n!, is defined...
In C++ Let n be a non-negative integer. The factorial of n, written n!, is defined by 0 ! 5 1, n! = 1·2·3· · ·n if n $ 1. For example, 4! = 1·2·3·4 = 24. Write a program that prompts the user to enter a nonnegative integer and outputs the factorial of the number. Allow the user to repeat the program. Example: If the user enters a 3 then your program should perform answer = 3 * 2...
java Factorials The factorial of n (written n!) is the product of the integers between 1...
java Factorials The factorial of n (written n!) is the product of the integers between 1 and n. Thus 4! = 1*2*3*4 = 24. By definition, 0! = 1. Factorial is not defined for negative numbers. Write a program that asks the user for a non-negative integer and computes and prints the factorial of that integer. You’ll need a while loop to do most of the work—this is a lot like computing a sum, but it’s a product instead. And...
The factorial of a non-negative integer is defined as follows: n! = 1 × 2 ×...
The factorial of a non-negative integer is defined as follows: n! = 1 × 2 × ... × (n − 1) × n A. Write a function that takes an input value n and returns its factorial result n!. Ensure that your function returns an error statement if the input n is either a negative or a non-integer value. You cannot use the prod() or factorial() functions. The Euler number e is calculated as the sum of the infinite series...
1.What happens if you attempt to call a method using a referencevariable that is set...
1.What happens if you attempt to call a method using a reference variable that is set to null?2.Consider the following class declaration:public class Square{   private double side;   public Square (double s){    side = s;}public double getArea(){   return side * side;}public double getSide(){   return side;}}a. Write a toString method for this class. The method should return a string containing the side and area of the square.b. Write a equals method for this class. The method should accept a Square object as...
Please explain in extensive detail what happens to the value of the dollar if the European...
Please explain in extensive detail what happens to the value of the dollar if the European Central Bank (ECB) does quantitative tightening to the money supply? How will this impact the value of the dollar, exports and imports, AD and GDP? Please provide a graph explaining your reason.
As you increase the value of the resistance, what happens to the quality of the circuit’s...
As you increase the value of the resistance, what happens to the quality of the circuit’s resonance?
As you increase the number of periods (t), what happens to the future value of a...
As you increase the number of periods (t), what happens to the future value of a single cash flow? What happens to the present value? Assume that the interest rate (r) remains constant. As you increase the interest rate (r), what happens to the future value of a single cash flow.   What happens to the present value? Assume that the number of periods (t) remains constant. Explain your reasoning.
If the monetary authority responds to the negative supply shock, what happens to the economy?
If the monetary authority responds to the negative supply shock, what happens to the economy?  A. Aggregate demand shifts left and the price level rises.  B. Aggregate demand shifts right and the price level falls.  C. Both the aggregate demand and supply curves shift to a higher price level.  D. Aggregate demand shifts right and the price level increases.  E. Aggregate supply shifts left and the price level rises.
Write a program in C or in Java, that takes an integer value N from the...
Write a program in C or in Java, that takes an integer value N from the command line, generates N random points in the unit square, and computes the distance separating the closest pair of points. A unit square is a square with sides of length 1, at points (0, 0), (0, 1), (1, 0), and (1, 1). If you wish to avoid the command-line processing, you can just assume you will generate a fixed number of points, say between...
What happens to future value of a lump sum if you increase the length of time...
What happens to future value of a lump sum if you increase the length of time involved (for example, going from 5 years to 10 years)?A) No Change to the future valueB) Future value increases.C) Future value decreasesD) The PV becomes negative. If the US T-Bill rate is 1.7%, the US expected inflation rate is 0.6%, the real rate of interest for Brazil is 2.2%, the default risk premium for Amazon is 0.9%, the maturity premium for Amazon debt is...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT