Factorial Program in Java Using while Loop8 May 2025 | 3 min read The factorial of a number N is the product of all positive descending integers (integers less than or equal to N). N! = N * (N - 1) ... * 3 * 2 * 1 In this section, we will create Java programs to find the factorial of a number using a while loop and do-while loop. How to find factorial?If we say N = 5, Then the factorial of 5 is represented as 5! And, 5! = 5 * 4 * 3 * 2 * 1 = 120 Note: The value of 0! is 1, according to the convention for an empty product. We can find the factorial of a number in Java using the following ways: Factorial Program Using while LoopIn the while loop, the condition is checked at the beginning of the iteration, and the loop incrementation occurs inside the loop body. Following are the steps to write a Java program to find factorial of a number using while loop:
Example 1:FactorialUsingWhileLoop.java Output: ![]() Factorial Using while loop and user-defined functionLet's use the while loop in user defined function. Example 2: FactorialUsingWhileLoop2.java Output: ![]() Factorial Program Using do-while LoopThe factorial program using the do-while loop slightly differs from the while loop. Unlike the while loop, the condition (i <= num) is checked at the end of each iteration. Example 3: FactorialUsingDoWhileLoop.java Output: ![]() Factorial using do-while loop and user-defined functionLet's use the do-while loop in user defined function. Example 4: FactorialUsingDoWhileLoop2.java Output: ![]() |
In this article, we are going to study the Asynchronous call in the JAVA programming language. At the end of the article, we will get a clear picture of the Asynchronous call and how it differs from synchronous call in the JAVA programming language. Firstly, let...
8 min read
It is a problem frequently asked in interviews of top IT companies like Google, Amazon, TCS, Accenture, Flipkart etc. By solving the problem, one wants to check the logical ability, critical thinking, and problem-solving skill of the interviewee. So, in this section, we are going to...
6 min read
In Java, objects of a subclass can be referenced using either a subclass reference or a superclass reference. The difference lies in what methods or fields can be accessed and how the behavior of the program changes depending on the reference type. Referring to Subclass Objects in...
5 min read
A string is given to us. Our task is to convert that string into a palindromic string by inserting characters. The characters should be inserted only on the leftmost side of the input string. In the output, we have to mention the total number of characters...
8 min read
The FloatBuffer put() has mainly 2 methods that take two different parameters. put(float f) put(int index, float f) i. put(float f) The java.nio.FloatBuffer class has put(float f) function. The newly generated float buffer is written with the specified float at the current location, and the position is then incremented...
5 min read
A Java operator is a special symbol that performs a certain operation on multiple operands and gives the result as an output. Java has a large number of operators that are divided into two categories. First, an operator's performance is based on the number of operands it...
3 min read
Java ranks as one of the most well-liked and often used languages in the field of object-oriented programming. In past couple of years, Java has been a mainstay in software development thanks to its powerful and adaptable capabilities. In Java, inheritance and interfaces are two fundamental ideas...
4 min read
Two positive integers, n, and k, are given. In the case where x is a palindrome, the number is said to be k-palindromic. By k, x is divisible. Return, as a string, the greatest k-palindromic integer with n digits. Example 1: Input: int N = 2 int k = 3 Output: The...
24 min read
As a programming language, Java provides a lot of functionality when it comes to handling dates and times. One common task when working with dates is to calculate the difference in hours between two given dates. In this article, we will explore different methods to get...
4 min read
Java, a widely used object-oriented programming language, offers a variety of features to help build robust and flexible applications. Two important concepts in Java of the object model have static and non-static members. Understanding the difference between static and non-static members is important for effective Java...
5 min read
We request you to subscribe our newsletter for upcoming updates.
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India