Problem:
Given a number n, compute n factorial (n!).
Solution:
package com.myprograms;
import java.util.Scanner;
public class FactorialComputation {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out.println("enter how many numbers you want to do factorial");
int n = s.nextInt();
int product = 1;
for(int i = 1; i <=n; i++){
product = product * i;
}
System.out.println("the factorial of " + n + " is " + product);
s.close();
}
}
Output:
enter how many numbers you want to do factorial
10
the factorial of 10 is 3628800
Given a number n, compute n factorial (n!).
Solution:
package com.myprograms;
import java.util.Scanner;
public class FactorialComputation {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out.println("enter how many numbers you want to do factorial");
int n = s.nextInt();
int product = 1;
for(int i = 1; i <=n; i++){
product = product * i;
}
System.out.println("the factorial of " + n + " is " + product);
s.close();
}
}
Output:
enter how many numbers you want to do factorial
10
the factorial of 10 is 3628800
No comments:
Post a Comment