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
One reason it is important to write code that is readable is that
kramer

Answer:

The answer is D:)

Explanation:

D) it makes it easier to follow when revisions or updates are called for.

8 0
3 years ago
Read 2 more answers
1. Digital videos look sharpest when they are displayed at a resolution that is larger than the frame size.
boyakko [2]

Answer:

(d) all of the above

Explanation:

This is because, not only is digital video a core technology for digital television, it also happens to be a core technology for video conferencing and video messaging. This could be seen in its application in messaging apps for private discussion of for holding official meetings between employees in virtual conference.

8 0
2 years ago
Any two differences between third and fourth generation of computer
Setler [38]

Answer:

Third generation computer use integrated circuit(IC) and it was fast and reliable.

Forth generation computer micro computer were introduced and they were highly reliable and fast.

5 0
2 years ago
Read 2 more answers
An administrator helps a user obtain an Internet connection through connecting a laptop with a smartphone via a cable. What has
frez [133]
The administrator set up tethering
8 0
3 years ago
A credit given to an author for his or her work is called a
m_a_m_a [10]
Citation , hoped that helped 

8 0
3 years ago
Read 2 more answers
Other questions:
  • A Color class has a method getColorName that returns a string corresponding to the common name for the color, e.g., yellow, blue
    11·1 answer
  • The class Date has a single constructor that accepts the integer value: a month- (1 for January through 12 for December), a day
    13·1 answer
  • 1.6.6 Night out for codehs
    6·1 answer
  • which student organization helps students with career development by having them become interms to sponsor conferences?
    9·1 answer
  • I need a free flashdrive can anyone tell me any website links so i can get one for free????​
    11·1 answer
  • Which statement is used to create a file object that will append data to an existing file? BufferedWriter salesdata =
    7·2 answers
  • Which option is the strongest password?
    7·2 answers
  • The "Rudolph Rule" is best described by which of the following?
    9·1 answer
  • Which statement is true of Web-based social media?
    12·1 answer
  • This assignment will again take the form of a compare/contrast essay. You will use the fresco of Christ Teaching the Apostles in
    14·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!