Skip to content

Commit

Permalink
created java program to find factorial of number
Browse files Browse the repository at this point in the history
  • Loading branch information
Vishakhasarode authored Oct 23, 2022
1 parent 8f8344c commit 5260c3c
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions java/factorial 0f number.java
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);
}
}

0 comments on commit 5260c3c

Please sign in to comment.