Answer to Question #253676 in Java | JSP | JSF for johanes

Question #253676

Using a while loop create a program that will prompt the user for a positive number and then print out a multiplication list of all the between the given number and one(inclusive), These are referred to as the factorial and it is only applicable for positive numbers, see link for more info(https://en.wikipedia.org/wiki/Factorial)

Sample Run1

Enter a number: 4

Output1: Result = 4 x 3 x 2 x 1 = 24

Sample Run2

Enter a number: -6




1
Expert's answer
2021-10-21T16:07:40-0400


package factorial;


import java.util.Scanner;




public class Factorial {


   
    public static void main(String[] args) {
        int fact = 1;
        System.out.println("Enter a number:  ");
        Scanner scan  = new Scanner(System.in);
        int n = scan.nextInt();
        int i = 1;
        while(i<=n){
            
            fact *= i;
            i++;
        }
        System.out.println("Result:  "+fact);
    }
    
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS