Sunday 18 June 2017

Java Code to compute the n factorial (n!).

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


No comments:

Post a Comment

Program for primality test in JAVA

Problem: ============= Program for primality test in JAVA What is primality test? A primality test is an algorithm for determining wh...