First Factorial Solution In Java-Coderbyte

0

What is Factorial: Factorial of a whole number 'n' is defined as the product of that number with every whole number till 1. For example, the factorial of 4 is 4×3×2×1, which is equal to 24. 

 


 

Title: First Factorial

Difficulty: Easy

Maximum Score: 10

 

Task: Have the function FirstFactorial(num) take the num parameter being passed and return the factorial of it. For example: if num = 4, then your program should return (4 * 3 * 2 * 1) = 24. For the test cases, the range will be between 1 and 18 and the input will always be an integer.

 

Examples

Input: 4

Output: 24


Input: 8

Output: 40320

 


Source code


import java.util.*;
import java.io.*;

class Main {

public static int FirstFactorial(int num) {
// code goes here
int fact=1;
for(int i=1;i<=num;i++){
fact*=i;
}
return fact;
}

public static void main (String[] args) {
// keep this function call here
Scanner s = new Scanner(System.in);
System.out.print(FirstFactorial(s.nextLine()));
}

}

 

Explanation

 

FirstFactorial is a method which accept one int parameter called num that is the value we need to find the factorial. We initialize one more variable fact where we store the product of the entire numbers which comes under the num. After the completion of the loop, the final fact value will be the factorial of that number. We return that fact as method output and print it on the console.

Post a Comment

0 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.
Post a Comment (0)
Our website uses cookies to enhance your experience. Learn More
Accept !