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
What are the two elements of creating a flyer
Shtirlitz [24]
Idk but I use to know because I took computer classes
8 0
3 years ago
Read 2 more answers
What is the maximum number of VLANs that can be configured on a switch supporting the 802.1Q protocol? Why?
KATRIN_1 [288]

Answer:

Under IEEE 802.1Q, the maximum number of VLANs on a given Ethernet network is 4,094 (4,096 values provided by the 12-bit VID field minus reserved values at each end of the range, 0 and 4,095).

Explanation: found it from google

6 0
2 years ago
Read 2 more answers
Analyze the error in the html code :<br><br> HTML
Andrei [34K]

Answer:

The World Wide Web Consortium provide a simple online tool (https://validator.w3.org/) that automatically check your HTML code and point out any problems/errors your code might have, such as missing closing tags or missing quotes around attributes.

Explanation:

your question is not clear

hope it helps

6 0
3 years ago
A .jpg file is an example of which of the following file types
egoroff_w [7]
A .jpg file is going to be a picture. =)
5 0
3 years ago
Read 2 more answers
The part of the computer that provides access to the Internet is the
andrew11 [14]

Answer:

MODEM

Explanation:

5 0
2 years ago
Other questions:
  • In a program called Nature's Notebook, citizen volunteers make and report observations about seasonal changes in plants and anim
    15·1 answer
  • Studying MyMagic+ helps one understand the costs associated with information systems deployment at scale. According to your read
    12·1 answer
  • "The network layer is responsible for transferring packets of data from the A.Source computer to the destination computer on adj
    11·1 answer
  • What does a data bar in a cell represents​
    12·2 answers
  • Which of these statements makes the most sense? a folder is contained within a file. a file is contained within a folder. a driv
    9·2 answers
  • Write a function called show_info that takes a name, a home city, and a home state (a total of 3 arguments) and returns a full s
    12·1 answer
  • What is data analysing data and give three examples ?​
    9·1 answer
  • Write an application that inputs a five digit integer (The number must be entered only as ONE input) and separates the number in
    10·1 answer
  • Mario kart is mercedes lol
    13·2 answers
  • Data becomes _______ when it is presented in a format that people can understand and use.
    12·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!