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]
3 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]3 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
If your presentation uses a background, your text should
almond37 [142]

Answer:

be at least 50-points

be placed in bulleted lists

WORK WELL WITH THE BACKGROUND

be written in paragraph form

Explanation:

4 0
3 years ago
Read 2 more answers
I have to make a online presentation, which program is the best
LiRa [457]
If you want an online presentation, I think you mean by a website.
Best thing you can find is Google Slides.
if you want a program,
LibreOffice is your best bet. It is completely free and it is for Linux, Windows and possibly Mac.
4 0
3 years ago
Read 2 more answers
What does it mean to “declare a variable”? create a variable use a variable share a variable modify a variable
lara [203]

You are defining the variable

6 0
3 years ago
Read 2 more answers
Which of the following programs can open a bitmap file?
choli [55]
Windows paint can be used to open a bitmap file. Usually, when you have a picture, you can use that to edit it.
Also, on a side note, notepad is specifically used for html codes. 

Answer: Windows Paint
4 0
4 years ago
Read 2 more answers
If there is a combination of two or more attributes which is being used as the primary key then we call it as
Hunter-Best [27]

Answer:

Composite Keys.

.....Wubba Lubba Dub-Dub :))

5 0
3 years ago
Other questions:
  • Is the protocol that specifies how web browsers and servers communicate.?
    11·1 answer
  • You recently purchased a new laptop for home. You want to ensure that you are safe from malware. You should install a personal _
    10·1 answer
  • What runs horizontally and is identified with numbers?
    12·2 answers
  • This program will convert a set of temperatures from Fahrenheit to Celsius and Kelvin. Your program will be reading in three dou
    11·1 answer
  • A software firm is going to develop an application for the automation of various office activities within an organization. In th
    13·1 answer
  • Qué es un bloque de programación?
    5·1 answer
  • Def find_max(nums: [int]) -&gt; int:
    5·1 answer
  • #include
    12·1 answer
  • David Griffin on How Photography Connects Us
    11·1 answer
  • Answered
    10·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!