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
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);
}
}
Comments
Leave a comment