This Java program calculates the factorial of a given integer number entered by the user. The program uses a for loop to iterate through all the numbers from the input integer down to 1, multiplying each number together to get the factorial.
Overall, this program is a straightforward way to calculate the factorial of an integer in Java using a for loop. However, it is worth noting that the factorial of large numbers can become very large and may overflow the limits of the long data type. In such cases, it may be necessary to use a different approach, such as using the BigInteger class in Java.
import java.util.*; public class Factorial_Number { public static void main(String args[]) { int num; long fact; Scanner input = new Scanner(System.in); System.out.print("Enter the Integer Number : "); num = input.nextInt(); //find factorial fact = 1; for (int i = num; i >= 1; i--) { fact *= i; } System.out.println(num+" Factorial is : " + fact); } }
Enter the Integer Number : 6 6 Factorial is : 720
Learn All in Tamil © Designed & Developed By Tutor Joes | Privacy Policy | Terms & Conditions