Question

Factorial.java This is an exercise in using repetition structures to calculate the mathematical values of the...

Factorial.java

This is an exercise in using repetition structures to calculate the mathematical values of the factorial function. Since methods have not yet been covered in detail, you may do the calculations in a class that contains just a main method. The looping structures require the use of count-controlled loops. This program computes the factorial function of a non-negative integer, which is written mathematically as n! (the parameter n followed by an exclamation mark). For values of n equal to or greater than 1, n! is defined as: n! = n × (n - 1) × (n - 2) × … × 1, and for the value n == 0, is defined as: 0! = 1. The value computed must be a Java integral value (either int or long). The program should ask the user for a value, n ≥ 0. If the value entered is less than zero, a message must be given the user, and the user allowed to try again. If the value is 0, the program should output the value of 0! and exit. If the value is greater than zero, the factorial value of that value n should be printed (if possible). The program must then continue to loop and to ask the user for another number for which the factorial may be computed (after each result has been output), until a zero value is entered. If the user enters a number for which the factorial cannot be computed or cannot be computed correctly, tell the user that is the problem, and keep asking for another number until an acceptable number (or 0) is entered.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Note: Could you plz go this code and let me know if u need any changes in this.Thank You
_________________

// Factorial.java

import java.util.Scanner;

public class Factorial {

   public static void main(String[] args) {
       // Declaring variables
       int n, res;

       /*
       * Creating an Scanner class object which is used to get the inputs
       * entered by the user
       */
       Scanner sc = new Scanner(System.in);

       while (true) {
           // Getting the input entered by the user
           System.out.print("\nEnter a number (Zero to QUIT):");
           n = sc.nextInt();
           if (n == 0) {
               System.out.println("** PROGRAM EXIT **");
               break;
           } else if (n > 1) {
               res = 1;
               for (int i = 1; i <= n; i++) {
                   res *= i;
               }

               // Displaying the output
               System.out.println(n + "! = " + res);

           } else {
               System.out.println("** Invalid.Must be greater than zero **");
           }

       }

   }

}
____________________

Output:


Enter a number (Zero to QUIT):-8
** Invalid.Must be greater than zero **

Enter a number (Zero to QUIT):8
8! = 40320

Enter a number (Zero to QUIT):6
6! = 720

Enter a number (Zero to QUIT):12
12! = 479001600

Enter a number (Zero to QUIT):0
** PROGRAM EXIT **

_____________Thank You

Add a comment
Know the answer?
Add Answer to:
Factorial.java This is an exercise in using repetition structures to calculate the mathematical values of the...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • 1. Write a program to add positive numbers. Ask the user to enter numbers greater than...

    1. Write a program to add positive numbers. Ask the user to enter numbers greater than zero. If a 0, is entered stop requesting numbers and display the total. If a number is entered that is less than 0, display the message ‘Number must be greater than 0’, then ask for another number to be entered. Repeat until a number greater than 0 or 0 is entered. use math lab so I can copy the answers

  • USING PYTHON PLEASE Write a program that calculates the factorial value of a number entered by...

    USING PYTHON PLEASE Write a program that calculates the factorial value of a number entered by the user. Remember that x! =x* (x-1)* (x-2)*... *3+ 2* 1. Your program should check the value input by the user to ensure it is valid (i.e., that it is a number > =1). To do this, consider looking at the is digit() function available in Python. If the user enters an incorrect input, your program should continue to ask them to enter a...

  • In C language using printf and scanf statements: Write a program to display a histogram based...

    In C language using printf and scanf statements: Write a program to display a histogram based on a number entered by the user. A histogram is a graphical representation of a number (in our case, using the asterisk character). On the same line after displaying the histogram, display the number. The entire program will repeat until the user enters zero or a negative number. Before the program ends, display "Bye...". The program will ask the user to enter a non-zero...

  • If n is an integer greater than 0, n factorial (n!) is the product: n* (n-1)...

    If n is an integer greater than 0, n factorial (n!) is the product: n* (n-1) * (n-2) * ( n-3)… * By convention, 0! = 1. You must write a program that allows a user to enter an integer between 1 and 7. Your program must then compute the factorial of the number entered by the user. Your solution MUST actually perform a computation (i.e., you may not simply print “5040” to the screen as a literal value if...

  • 3). Repetition Structure: Note: this question is based on question 2), feel free to add the...

    3). Repetition Structure: Note: this question is based on question 2), feel free to add the loops directly into the code for question 2) if you want. a). Add a validation icop for question 2). The validation loop should verify that the user's input for the number of destinations is a positive number, otherwise it should display an error message and ask for another input. b). Add a loop for question 2) so that the program repeats when the user...

  • For this interactive assignment, you will continue to utilize loops and functions and write a Python...

    For this interactive assignment, you will continue to utilize loops and functions and write a Python program that asks the user for an integer (number) greater than 1. Once the user enters an integer, it should then allow the user to CHOOSE between the following two options: If 1 is entered, a countdown from that number to zero is printed. If 2 is entered, the factorial of the number is printed. If the user inputs a number less than 1,...

  • C++ only Implement a program that prompts a user to enter a number N where N...

    C++ only Implement a program that prompts a user to enter a number N where N is a positive number. The program must validate a user input and ask a user to re-enter until an input is valid. Implement a function that pass N as an argument and return a result of calculates N! (N-factorial): The function must use loop for the implementation. Hint: Factorial: N! = N * (N-1) * (N-2) * …. * 1 ( This is only...

  • Just need help solving this CSIS 123 Chapter 5 Controls Structures – Repetition/Looping Fall is football...

    Just need help solving this CSIS 123 Chapter 5 Controls Structures – Repetition/Looping Fall is football season. For this assignment, you will be writing a football score board program using the looping structures that you learned in Chapter 5. Initially, display a banner telling your user about the program. Then, prompt them for the name of the home team and the visitor team USING A FOR LOOP structure, prompt for the number of touchdowns and number of field goals for...

  • Write a complete C++ program that will: Declare a vector of integers Use a pointer to...

    Write a complete C++ program that will: Declare a vector of integers Use a pointer to dynamically allocate an array of 10 elements Ask the user how many values they would like to have in the data structures Read in the user’s response and make sure that it is greater than 20 (error loop) Generate the number of integers that the user asked for and store them into both data structures. The integer values should be unique unordered values (no...

  • BME 2013 Spring 2019 Exam 3 Matlab Review Sheet 1. Given the vector of patient body temperatures ...

    can anyone help me figure out how fo write matlab code for these problems? BME 2013 Spring 2019 Exam 3 Matlab Review Sheet 1. Given the vector of patient body temperatures T (98.6, 102, 101, 99,98.9, 102.1, 100.3, 101.2, 98.4 97.6, 100] use conditional statements and loops to write a program that determines and displays (using the fprintf function) the number of patients with a fever and patients without a fever. The program must ask the user to input the...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT