Factorial Program In Java Using While Loop
Last Updated : Mar 11, 2024
In this article we will show you the solution of factorial program in java using while loop, the condition is verified at the start of each iteration in the while loop, as well as the loop incrementation happens within the loop body.
The steps for writing a Java program to find the factorial of the a number using a while loop are as follows:
- Declare a variable (int in this case) and set its value to 1.
- Read a number that contains a factorial. Put it into a variable (int num).
- Set of that while loop to a condition I = num) where I is initially set to 1.
Multiply variable fact and variable I within the while loop and save the result in variable reality. Increase loop variable I by one (i++).
Let us first discuss recursion briefly prior to actually moving to the main problem. Recursion is a problem-solving technique in which a method calls itself.
Recursion divides an issue into smaller subproblems and solves them with successive method calls.
It eventually combines this same solution with such subproblems to arrive at the final solution.
The product of all of the numbers from 1 to N seems to be the factorial of a number N.
Factorial of the a number n is defined as the sum of all positive descending integers, denoted by n!.
Factorial can be calculated using the recursive formula below, in which the recursive calls are made to a multiplicity of all of the numbers below the number which the factorial is computed.
Step By Step Guide On Factorial Program In Java Using While Loop :-
import java.util.Scanner; public class TalkersCodeExample{ public static void main(String[] args) { int fact = 1; int i = 1; Scanner sc = new Scanner(System.in); System.out.println("TalkersCode Enter a number of factorial"); int num = sc.nextInt(); while( i <= num ){ fact = fact * i; i++; } System.out.println("\nTalkersCode Factorial of " + num + " is: " + fact); } }
- Give the class the public class name TalkersCodeExample.
- In the following step, we will establish a public static void main to string arguments.
- Next, we use num1 to count an integer number.
- To calculate the Factorial Series, we will use system.out.println for "+count+".
- The while loop is then defined as just a counting loop.
- We define previous numbers by using system.out.println.
Conclusion :-
Factorial of the a number n is defined as the sum of all positive descending integers, denoted by n!.
Factorial can be calculated using the recursive formula below, in which the recursive calls are made to a multiplicity of all of the numbers below the number which the factorial is computed.
I hope this article on factorial program in java using while loop helps you and the steps and method mentioned above are easy to follow and implement.