-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path036-Factorial-of-a-Number.java
More file actions
70 lines (54 loc) · 2.13 KB
/
Copy path036-Factorial-of-a-Number.java
File metadata and controls
70 lines (54 loc) · 2.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
/*
* ============================================================================
* Program Name : Factorial of a Number
* File Name : 036-Factorial-of-a-Number.java
* Class Name : FactorialOfANumber
*
* Description:
* This program accepts a non-negative integer from the user and
* calculates its factorial using the for loop.
*
* Objective:
* - Understand user input using the Scanner class.
* - Learn how to use the for loop.
* - Calculate the factorial of a given number.
*
* Author : Shaik Mahaboob Basha
* Repository : 01-Core-Java
* Folder : 08-Java-Programs
* ============================================================================
*/
import java.util.Scanner;
public class FactorialOfANumber {
// The main() method is the entry point of every Java application.
public static void main(String[] args) {
// Create a Scanner object to read input from the keyboard.
Scanner scanner = new Scanner(System.in);
// Ask the user to enter a number.
System.out.print("Enter a Non-Negative Integer: ");
// Read the number entered by the user.
int number = scanner.nextInt();
// Declare and initialize a variable to store the factorial.
long factorial = 1;
// Check whether the entered number is valid.
if (number < 0) {
// Display an error message for negative numbers.
System.out.println("Factorial is not defined for negative numbers.");
// Example Output:
// Factorial is not defined for negative numbers.
} else {
// Iterate from 1 to the given number.
for (int i = 1; i <= number; i++) {
// Multiply the current value with the factorial.
factorial = factorial * i;
}
// Display the calculated factorial.
System.out.println("Factorial of " + number + " = " + factorial);
// Example Output:
// Enter a Non-Negative Integer: 5
// Factorial of 5 = 120
}
// Close the Scanner object to release system resources.
scanner.close();
}
}