Java 8 Coding challenge: Factorial!

Problem:

You have been given a positive integer N. You need to find and print the Factorial of this number. The Factorial of a positive integer N refers to the product of all number in the range from 1 to N.

Input Format:
The first and only line of the input contains a single integer N denoting the number whose factorial you need to find.

Output Format
Output a single line denoting the factorial of the number N.

Constraints
1≤N≤101≤N≤10
SAMPLE INPUT
2
SAMPLE OUTPUT
2

Code:

import java.util.*;
 
 
class TestClass {
 public static void main(String args[] ) throws Exception {
 
 try (Scanner input = new Scanner(System.in)) {
 
 int fatorial = input.nextInt();
 
 int total=fatorial;
 
 for(int i=fatorial-1;i>=1;i--){
 total = total * i;
 }
 
 System.out.println(total);
 
 
 } catch (Throwable e) {
 e.printStackTrace();
 }
 
 }
}

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s