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

The factorial of a nonnegative integer n is written n! (Pronounced “n factorial”) and is defined...
The factorial of a nonnegative integer n is written n! (Pronounced “n factorial”) and is defined as follows: n! = n.(n-1). (n-2).……...1 (for values of n greater than 1) n!=1 (for n = 0 or 1) For instance, 5! = 5.4.3.2.1 which is 120. Write a Python program that reads a nonnegative integer n and calculates and prints its factorial. Your program should display a suitable error message if n entered as a negative integer.    Figure   6.   Exercise   6  ...
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...
In Java In mathematics, factorial of a non-negative integer n, denoted by n! , is the...
In Java In mathematics, factorial of a non-negative integer n, denoted by n! , is the product of all positive integers less than or equal to n. For example, 5! = 5 * 4 * 3 * 2* 1 = 120 3! = 3 * 2 * 1 = 6 2! = 2 * 1 = 2 The value of 0! is 1. Write a program that asks user to enter an integer > 0; if a valid value is...
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 mathematics, the notation n! represents the factorial of the nonnegative integer n. The factorial of...
In mathematics, the notation n! represents the factorial of the nonnegative integer n. The factorial of n is the product of non-negative numbers from 1 to n. Design a program that asks the user to enter a nonnegative integer and then displays the factorial of that number. Module main. Asks the user to enter a non-negative integer. A loop is used to require user input until a nonnegative number is entered. Once a nonnegative number is entered, the integer is...
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!)
Need these written in Java script please Problem 1: Given an array A[0 ... n-1], where...
Need these written in Java script please Problem 1: Given an array A[0 ... n-1], where each element of the array represents a vote in the election. Assume that each vote is given as integers representing the ID of the chosen candidate. Write the code determining who wins the election. Problem 2: How do we find the number which appeared maximum number of times in an array?
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",...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT