Find elements

Java code to find factorial using method

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

 

Factorial of number of n

 

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

Find factorial of a number in CPP

Find factorial of a number in Python

Python program to find factorial of a number using while loop
Cpp program to find factorial using function
Karmehavannan

I am Mr S.Karmehavannan. Founder and CEO of this website. This website specially designed for the programming learners and very especially programming beginners, this website will gradually lead the learners to develop their programming skill.

Recent Posts

PHP Star Triangle pattern program

PHP Star Triangle pattern program In this tutorial, we will discuss about PHP Star Triangle…

2 months ago

PHP Full Pyramid pattern program

PHP Full Pyramid pattern program In this tutorial, we will discuss about PHP Full Pyramid…

2 months ago

5 methods to add two numbers in Java

5 methods to add two numbers in Java In this tutorial, we will discuss the…

2 months ago

Python Full Pyramid star pattern program

Python full Pyramid star pattern program In this tutorial, we will discuss  the concept of…

5 months ago

Write a function or method to convert C into F -Entered by user

Write a function or method to convert C into F -Entered by the user In…

10 months ago

How to write a function or method to convert Celsius into Fahrenheit

How to write a function or method to convert Celsius into Fahrenheit In this tutorial,…

10 months ago