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

How to find reverse number using method in Java

How to find reverse number using method In this article, we will discuss the concept…

2 days ago

C# inverted full pyramid star pattern

C# inverted full pyramid star pattern In this article, we will discuss the concept of…

3 weeks ago

C# Full Pyramid star pattern program

C# Full Pyramid star pattern program In this article, we will discuss the concept of…

1 month ago

Program to count vowels,consonants,words, characters and space in Java

Program to count vowels, consonants, words, characters and space in Java In this article, we…

1 month ago

How to print multiplication table using Array in C++ language

How to print multiplication table using Array in C++ language In this post, we will…

1 month ago

C Program to multiplication table using Array

C Program to multiplication table using Array In this tutorial , we will discuss about…

2 months ago