This Java program uses a while loop to calculate the factorial of a user-input number. The user is prompted to enter a number, which is then stored as an integer. A for loop iterates from 1 to the input number, multiplying the running factorial by each integer. After the loop finishes, the final factorial is printed along with the input number.
This Java program uses a while loop to calculate the factorial of a user-input number. The user is prompted to enter a number, which is then stored as an integer. A for loop iterates from 1 to the input number, multiplying the running factorial by each integer. After the loop finishes, the final factorial is printed along with the input number.
This Java program uses a while loop to calculate the factorial of a user-input number. The user is prompted to enter a number, which is then stored as an integer. A for loop iterates from 1 to the input number, multiplying the running factorial by each integer. After the loop finishes, the final factorial is printed along with the input number.
package assesment1; import java.util.Scanner; public class factorial {
public static void main(String[] args)
{ int number; System.out.println("Enter the number: "); Scanner scanner = new Scanner(System.in); number = scanner.nextInt(); scanner.close(); long fact = 1; int i = 1; while(i<=number) { fact = fact * i; i++; } System.out.println("Factorial of "+number+" is: "+fact); } }
Output : Enter the number: 6 Factorial of 6 is: 720