Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions Java/Program-22/Program-22.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Program-22: Factorial of a Number

import java.util.Scanner;

public class Program22 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number: ");
int num = sc.nextInt();
long fact = 1;

if (num < 0) {
System.out.println("Please enter a non-negative number.");
} else {
for (int i = 1; i <= num; i++) {
fact *= i;
}
System.out.println("Factorial of " + num + " is: " + fact);
}

sc.close();
}
}
19 changes: 19 additions & 0 deletions Java/Program-22/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Factorial of a Number

## Description
This program calculates the factorial of a non-negative integer using iteration.

## Input
- A non-negative integer
Example: `5`

## Output
- Factorial of the number
Example:
`Factorial of 5 is 120`

## How to Run
1. Compile using: `javac Program-22.java`
2. Run using: `java Program22`
3. Enter a number when prompted.
4. View the output in the console.