Java code to find factorial using method
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
Find factorial using Java method
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
Find factorial using Java method with return
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
Find factorial using Ternary operator in Java
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