Question

In: Computer Science

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.

  1. 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 you’ll need to think about what should happen if the user enters 0.

  2. Now modify your program so that it checks to see if the user entered a negative number. If so, the program should print a message saying that a nonnegative number is required and ask the user the enter another number. The program should keep doing this until the user enters a nonnegative number, after which it should compute the factorial of that number. Hint: you will need another while loop before the loop that computes the factorial. You should not need to change any of the code that computes the factorial!

Solutions

Expert Solution

Code is Given Below:

==========================

import java.util.Scanner;

public class Factorials {
   public static void main(String[] args) {
       //creating Scanner object to get input from user
       Scanner scn=new Scanner(System.in);
       System.out.print("Enter non-negative integer : ");
       int n=scn.nextInt();
       int fact=1,i=1;
       //checking if number is 0
       if(n==0) {
           System.out.println("Factorial of "+n+" is "+fact);
           return;
       }
       //finding factorial
       while(i<=n) {
           fact=fact*i;
           i++;
       }
       //displaying result
       System.out.println("Factorial of "+n+" is "+fact);
   }

}

Output:

=================

Code Snapshot:

=========================

Modified Code:- Handling Negative values

============================================

import java.util.Scanner;

public class Factorials {
   public static void main(String[] args) {
       //creating Scanner object to get input from user
       Scanner scn=new Scanner(System.in);
       System.out.print("Enter non-negative integer : ");
       int n=scn.nextInt();
       //checking if number is negative
       while(n<0) {
           System.out.print("Enter non-negative integer : ");
           n=scn.nextInt();
       }
       int fact=1,i=1;
       //checking if number is 0
       if(n==0) {
           System.out.println("Factorial of "+n+" is "+fact);
           return;
       }
       //finding factorial
       while(i<=n) {
           fact=fact*i;
           i++;
       }
       //displaying result
       System.out.println("Factorial of "+n+" is "+fact);
   }

}

Output:

=============

Code Snapshot

=================


Related Solutions

Throwing Exceptions - JAVA File Factorials contains a program that calls the factorial method of theMathUtils...
Throwing Exceptions - JAVA File Factorials contains a program that calls the factorial method of theMathUtils class to compute the factorials of integers entered by the user. Save these files to your directory and study the code in both, then compile and run Factorials to see how it works. Try several positive integers, then try a negative number. You should find that it works for small positive integers (values < 17), but that it returns a large negative value for...
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...
P1 Consider an array of length n that contains unique integers between 1 and n+1. For...
P1 Consider an array of length n that contains unique integers between 1 and n+1. For example, an array A1 of length 5 might contain the integers 3, 6, 5, 1, and 4. In an array of this form, one number between 1 and n+1 will be missing. In A1, the integer 2 is missing from the array. As another example, consider the array A2 that contain the integers 4, 7, 5, 2, 6, and 1. A2 has size 6,...
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!)
Written in JAVA Code Write a program that inserts 25 random integers from 0 to 100...
Written in JAVA Code Write a program that inserts 25 random integers from 0 to 100 in order into a LinkedList object. The program should sort the elements, then calculate the sum of the elements and the floating-point average of the elements.
Java Language The array s of ints contain integers each of which is between 1 and...
Java Language The array s of ints contain integers each of which is between 1 and 1000 (inclusive). Write code that stores in the variable ordinals the array of Strings consisting of each number followed by its ordinal number abbreviation, "st", "nd", "rd", or "th". For example if s is the array { 1, 2, 3, 4, 5, 10, 11, 12, 13, 21, 22, 973, 1000 } then your code should set ordinals to the array { "1st", "2nd", "3rd",...
Let X1 and X2 be uniform on the consecutive integers -n, -(n+1), ... , n-1, n....
Let X1 and X2 be uniform on the consecutive integers -n, -(n+1), ... , n-1, n. Use convolution to find the mass function for X1 + X2.
Let iqsort(A, 1, n) be an algorithm that sorts an array A with n integers. It...
Let iqsort(A, 1, n) be an algorithm that sorts an array A with n integers. It works as follows: iqsort(A, p, q){ if p ≥ q, return; r=partition(A, p, q); //run quick sort on the low part quicksort(A, p, r − 1); //run insert sort on the high part insertsort(A, r + 1, q); } Compute the best-case, worst-case, and average-case complexities of iqsort.
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...
The factorial of a natural number n is denoted by n! and is defined as follows:...
The factorial of a natural number n is denoted by n! and is defined as follows: the factorial of 0 is 1, the factorial of 1 is 1, and the factorial of any other positive integer is the product of all the integers from 2 to that integer. Write a VBA function called MyFactorial with one input of data type Integer which computes and returns the value of the factorial of the input if it is greater than or equal...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT