1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
Aleks [24]
2 years ago
6

2. The factorial of a positive integer n is the product of the integers from 1 to n. You can express the factorial of a positive

integer n (in mathematics this is denoted by n!) using the following formula: n! = 1 * 2 * 3 * ... * (n - 1) * n Write a Java program that will compute the factorial of some numbers n (input from the user, accept only range 1 - 10). For each valid number in input, the output should be the value of n!. Your program should use a loop, to allow the user to input more than one number (count-controlled or sentinel-controlled, your choice)
Computers and Technology
1 answer:
Molodets [167]2 years ago
6 0

Answer:

import java.io.*;

import java.util.Scanner;//importing the scanner.

class Factorial {

public static void main (String[] args) {

    Scanner fact=new Scanner(System.in);//creating a scanner object for taking the input.

    int t,n;//t for number of times the user want to calculate the factorial and n for factorial.

    System.out.println("How many times you want to calculate the factorial");

    t=fact.nextInt();//taking input of t.

    while(t>0)

    {

        int f=1;//f for calculating the variable.

        n=fact.nextInt();//taking input of n .

        if(n>10||n<1)//if n is out of range then again taking input.

        {

           while(n>10 || n<1)

                {

                        System.out.println("Please Enter the Valid Input");

                 n=fact.nextInt();

                }

               for(int i=1;i<=n;i++)//calculating the factorial.

               {

                   f*=i;

               }

               System.out.println("The factorial is: "+ f);

        }

        else // if n is in  range then definitely calculating the factorial.

        {

             for(int i=1;i<=n;i++)// calculating the factorial.

               {

                   f*=i;

               }

               System.out.println("The factorial is: "+ f);

        }

    }

}

}

Output:-

How many times you want to calculate the factorial

2

-5

Please Enter the Valid Input

4

The factorial is: 24

5

The factorial is: 120

Explanation:

The above written code is for calculating the factorial of an integer the number of times user want to calculate the factorial.The code wants user to enter again until the value entered by the user is in range.If the value is in range then it definitely calculates the factorial.

You might be interested in
Your worksheet has the value 27 in cell B3. What value is<br>returned by the function =MOD(B3,6)?​
Digiron [165]

Answer:

3

Explanation:

MOD means the remainder after a division calculation.

\frac{27}{6}= 4r3

The remainder is 3

5 0
3 years ago
Which task can be completed with the Template Organizer?
spin [16.1K]

Answer:

B is the answer

Explanation:

i just got it right

8 0
2 years ago
Read 2 more answers
What is a URL (Please explain)?
GalinKa [24]
Definition

URL a.k.a uniform resource locater, is simply the address of a World Wide Webpage.

Sentence example:

"Type a URL into a browser's address bar."
5 0
3 years ago
The freemen's bureau was the U.S.' first ____ program.​
Ludmilka [50]

Answer:

welfare

Explanation:

The Freemen's bureau was the first Welfare program in the US that was put forward in the year 1865 just after the assassination of Abraham Lincoln. It was headed by Oliver O Howard. And it was brought for providing the food, clothing, medical care, and education to the recently freed white refugees and the freed slaves. And it was supposed to be a kind of welfare.  

6 0
3 years ago
What is cloud storage?​
Aleks [24]

Cloud storage is a service model in which data is maintained, managed, backed up remotely and made available to users over a network (typically the Internet).

3 0
3 years ago
Other questions:
  • Encryption that uses 16-character keys is known as ____. strong 128-bit encryption strong 256-bit encryption military-strength 5
    9·1 answer
  • How did The Gates by Christo and Jeanne-Claude affect the site?
    11·2 answers
  • Write a program that first reads in the name of an input file and then reads the input file using the file.readlines() method. T
    14·1 answer
  • Taken together, the physical and data link layers are called the ____________________. Internet layer Hardware layer Internetwor
    15·1 answer
  • FIGURE A-2—Use the information in this chart to answer Question 2.
    11·1 answer
  • TWO MINUTES LEFT<br> ________ __________ is the physical arrangement of devices on a network.
    9·1 answer
  • True or False The two types of general construction projects are residential for homes or dwellings and commercial for a commerc
    7·1 answer
  • Assume an ArrayList and a Linked List each contain 10,000 items. Which operation is performed fastest
    14·1 answer
  • Which of the following statements is/are correct? a. At the network layer, entitlement can map identity and/or attributes to fun
    8·1 answer
  • How to transfer polygon from eth to polygon in ledger live
    6·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!