Home >Java >javaTutorial >How to use while loop to find the factorial of n in java
Implementation ideas:
1. Define two variables to store the number representing the factorial and the factorial result respectively;
2. Use while Calculate the factorial in a loop;
3. Just print the factorial result.
(Recommended video tutorial: java video tutorial)
The specific code is as follows:
package hello; import java.util.Scanner; public class Hello { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n; n=in.nextInt(); int i=1,sum=1; while(i<=n) { sum=sum*i; i++; } System.out.print(sum); } }
Recommended tutorial: java entry program
The above is the detailed content of How to use while loop to find the factorial of n in java. For more information, please follow other related articles on the PHP Chinese website!