In this tutorial, we will discuss Java code to find factorial using method
There are many ways to calculate a factorial using Java programming language.
In this tutorial, we are going to learn about how to calculate factorial of a given number using Java method.
Factorial is a product of all positive descending integer begins with a specified number (n) and calculates up to one
Example
factorial of 5 is
5!=5*4*3*2*1=120
factorial of 4 is
4!=4*3*2*1=24
The factorial of a positive number of “n” is as follows
factorial of n is
n!=n*(n-1)*....2*1
the user can provide numbers as they wish and get the factorial according to their input
Program 1
import java.util.Scanner;//import the scanner class class Find_Factorial4{ public static void main(String args[]){ Scanner scan=new Scanner(System.in); //create a scanner object System.out.println("Enter a number for find factorial"); int num=scan.nextInt();//get input from user factorial(num);//call the method } static void factorial(int num)//user defined method for calculate factorial { int i,f=1; for(i=1; i<=num; i++){ f=f*i;//calculate te factorial using for loop } System.out.print("Factorial of the "+num+"is "+f); //display the factorial } }
When the above code is executed, it produces the following results
Enter a number for find factorial 5 Factorial of the 6 is: 120
Program 2
import java.util.Scanner;//import the scanner class class Find_Factorial{ public static void main(String args[]){ Scanner scan=new Scanner(System.in);//create a scanner object System.out.println("Enter a number for find factorial"); int num=scan.nextInt();//get input from user System.out.print("Factorial of the "+num+"is "+factorial(num)); //call the function to find factorial /*int result=factorial(num); //assum the output for result variable System.out.print("Factorial of the "+num+"is: "+result);*/ //another way to find factorial } static int factorial(int num) { int i,fact=1; for(i=1; i<=num; i++){ fact=fact*i;//calculate the factorial using for loop } return fact; } }
When the above code is executed, it produces the following results
Enter a number for find factorial 6 Factorial of the 6 is: 720
import java.util.Scanner;//import the scanner class class Find_Factorial2{ public static void main(String args[]){ Scanner scan=new Scanner(System.in);//create a scanner object System.out.println("Enter a number for find factorial"); int num=scan.nextInt();//get input from user System.out.print("Factorial of the "+num+"is "+factorial(num)); //call the function to find factorial } static int factorial(int num)//user defined method { return (num==1 || num==0)? 1: num*factorial(num-1); //calculate factorial using ternary operator } }
Enter a number for find factorial 5 factorial of the 5 is 120
Suggested for you
Java code to find factorial of a number
Find factorial of a number in C
C Program to find factorial of a number using Function
How to find reverse number using method In this article, we will discuss the concept…
C# inverted full pyramid star pattern In this article, we will discuss the concept of…
C# Full Pyramid star pattern program In this article, we will discuss the concept of…
Program to count vowels, consonants, words, characters and space in Java In this article, we…
How to print multiplication table using Array in C++ language In this post, we will…
C Program to multiplication table using Array In this tutorial , we will discuss about…