Factorial in Java HackerEarth Basic Problem Solution.
Factorial Program in Java: Factorial of n is the product of all positive descending integers. Factorial of n is denoted by n!. For example 4! = 4*3*2*1 = 24
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 numbers in the range from 1 to N. You can read more about the factorial of a number here.
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//import for Scanner and other utility classes | |
import java.util.*; | |
class TestClass { | |
public static void main(String args[] ) throws Exception { | |
// Write your code here | |
TestClass f = new TestClass(); | |
int n; | |
Scanner sc = new Scanner(System.in); | |
n = sc.nextLine(); | |
System.out.print(f.fact(n)); | |
} | |
public int fact(int n){ | |
int a=1; | |
if(n==0){ | |
return 1; | |
}else{ | |
a = n* fact(n-1); | |
} | |
return a; | |
} | |
} |
0 Comments