X Tutup
Skip to content
Merged
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
1 change: 1 addition & 0 deletions src/navs/program.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,6 @@ export const programsNav = {
pages['java-program-to-check-divisbility'],
pages['find-quotient-and-reminder'],
pages['calculate-power-of-a-number'],
pages['factorial-in-java'],
],
}
50 changes: 50 additions & 0 deletions src/pages/programs/factorial-in-java.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
---
title: Java Program to Calculate Factorial of a number
shotTitle: Calculate Simple Factorial
description: In this program, you'll learn to calculate the factorial and display final value on screen/terminal
---
import {TipInfo} from '@/components/Tip'

To understand this example, you should have the knowledge of the following Java programming topics:

- [Java Loops](/docs/for-loop)
- [Java Basic Input and Output](/docs/basic-input-output)

## Calculate Factorial

A java program that calculate the factorial of a number by getting user input of the number.

```java
import java.util.Scanner;
class FactorialExample{
public static void main(String args[]){
Scanner in = new Scanner(System.in);
int i,fact=1;
int number = in.nextInt();
for(i=1;i<=number;i++){
fact=fact*i;
}
System.out.println("Factorial of "+number+" is: "+fact);
}
}
```

#### Output

```text
Enter the number : 5
The factorial of 5 is : 120.
```

To Calculate Factorial ,
```Factorial(5) = (5*4*3*2*1)```



<TipInfo>

Don't know how to take input from the user ? Look at [this examples](/docs/basic-input-output#java-input)

Here two input numbers are taken from user one after another with space in between them which distinguish between two different inputs, this useful behavior is because of the default settings of Scanner called as Delimiter, [learn more here](https://www.javatpoint.com/post/java-scanner-delimiter-method).

</TipInfo>
X Tutup