-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
created java program to find factorial of number
- Loading branch information
1 parent
8f8344c
commit 5260c3c
Showing
1 changed file
with
27 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
//importing Scanner class | ||
import java.util.Scanner; | ||
|
||
public class FactorialUsingWhileLoop { | ||
public static void main(String[] args) { | ||
|
||
//declaring and intializing variables | ||
int fact = 1; | ||
int i = 1; | ||
|
||
//creating object of Scanner class | ||
Scanner sc = new Scanner(System.in); | ||
|
||
//accepting a number from the user | ||
System.out.println("Enter a number whose factorial is to be found: "); | ||
int num = sc.nextInt(); | ||
|
||
//counting the factorial using while loop | ||
while( i <= num ){ | ||
fact = fact * i; | ||
i++; //increment i by 1 | ||
} | ||
|
||
//printing the result | ||
System.out.println("\nFactorial of " + num + " is: " + fact); | ||
} | ||
} |