m2skills

factorial java

May 31st, 2017
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.63 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class fact{
  4.     public static void main(String a[]){
  5.         Scanner sc = new Scanner(System.in);
  6.         boolean cont = true;
  7.         while(cont){
  8.             System.out.print("Enter the Number : ");
  9.             int number = sc.nextInt();
  10.             long answer = factorial(number);
  11.             System.out.println("The factorial of " + number + " is " + answer);
  12.             System.out.print("\nDo you want to continue (1/0) : ");
  13.             int n = sc.nextInt();
  14.             if(n==0){
  15.                 cont = false;
  16.             }
  17.         }
  18.     }
  19.    
  20.     static long factorial(long number){
  21.         long fact = 1;
  22.         while(number > 0){
  23.             fact = fact*number;
  24.             number = number - 1;
  25.         }
  26.         return fact;
  27.     }
  28. }
Add Comment
Please, Sign In to add comment