Java Factorial Program – Java Sorting Examples: a helpful blog for those learning about the factorial function.

Factorial of a Number Using Recursion

In this program, we will learn how to find the factorial of a number using recursion in Java.

Recursion is the process of defining something in terms of itself. A method that uses this technique is recursive.

The Coding:

import java.util.*;

public class Factorial {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

System.out.print(“Enter number : “);

This is a classic Java Factorial Program Example. It shows how to create a Java factorial program using recursion, iterative, and using the factorial methods provided by the Apache Commons Math library.

Java Factorial Program – Recursive Approach

The first approach to creating a Java factorial program is to use recursion. This involves calling a method from within itself until some condition is met. The recursive approach to writing this Java Factorial Program Example looks like this:

public class FactorialExample1 {

public static void main(String[] args){

int i = 5;

System.out.println(“Factorial of “+i+” is: “+factorial(i));

}

public static int factorial(int number){ //Recursive method

if(number == 0){

return 1;

}else{

return(number * factorial(number-1)); //Method calls itself: Recursive call

}

}

}

Factorial program in java with examples of fibonacci series, armstrong number, prime number, palindrome number, factorial number, bubble sort, selection sort, insertion sort, swapping numbers etc.

Java Program to Find Factorial of a Number using Recursion

import java.util.*;

public class Factorial {

public static void main(String[] args) {

int num = 10;

long factorial = 1;

for(int i = 1; i <= num; ++i) { // factorial = factorial * i; factorial *= i; } System.out.printf("Factorial of %d = %d", num, factorial); } } import java.io.*; import java.util.*; class Factorial { public static void main(String args[]) { int n,i,fact=1;//Factorial Function Scanner obj=new Scanner(System.in);//Input From User n=obj.nextInt();//Input From User for(i=1;i<=n;i++)//Factorial Logic { fact=fact*i;//Result Of Factorial } System.out.println("factorial="+fact);//Print Result To User Screen import java.io.*; class Factorial { public static void main(String args[])throws IOException { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); int n,fact=1; System.out.println("Enter the number whose factorial is to be calculated"); n=Integer.parseInt(br.readLine()); for(int i=1;i<=n;i++) fact*=i; System.out.println("The factorial of "+n+" is: "+fact);} } public class FactorialExample{ public static void main(String args[]){ int i,fact=1; int number=5;//It is the number to calculate factorial for(i=1;i<=number;i++){ fact=fact*i; } System.out.println("Factorial of "+number+" is: "+fact); } }

Tomy

Tomy is a contributor at AskMeCode. We are committed to providing well-researched, accurate, and valuable content to our readers.

You May Also Like

Top 5 Fighter Planes in The World

Top 5 Fighter Planes in The World

The best fighter planes in the world are those that are ranked at the top of the list. This is...

What is sketch2code? A quick description of our services and values.

What is sketch2code? A quick description of our services and values.

Sketch2code is a new way to deliver responsive websites to your customers. We are dedicated to providing high quality, reliable,...

Artistic representation for Quick Software Development Solutions Under 30 Minutes

Quick Software Development Solutions Under 30 Minutes

Quick Software Development Solutions Under 30 Minutes In today's fast-paced digital world, software development has become essential across various industries,...

8 Things That Make Someone Look Like a Master Programmer

8 Things That Make Someone Look Like a Master Programmer

I've been programming for a few years now and I've seen a lot of people come and go. Some were...

Leave a Reply

About | Contact | Privacy Policy | Terms of Service | Disclaimer | Cookie Policy
© 2026 AskMeCode. All rights reserved.