mathajax

Java program Factorial Number

A result of series of multiplication from 1 to factorial F is called factorial number F

Factorial Number - Example

  
   
    for example, a factorial number denoted by 5! 
               (postfix exclamation symbol with digits)    

     find equivalent decimal value for given a factorial number 5! 

                   5!  = 1 x 2 x 3 x 4 x 5
                   5!  = 120 
                                   
       

Factorial Number - Pseudocode

  • Read a positive integer F
  • set sf = 1
  • For n= 2 to F
  • sf = sf * n
  • End for
  • print sf

  • Factorial number - Java code

    The Java program finds factorial value for a number. The program reads a positive integer from user while running and returns factorial value as result of the it.

     
    
    import java.io.IOException;
    import java.util.Scanner;
    
    public class Factorial 
    {
    
    public static void main(String[] args) throws IOException
       {      
          System.out.println("Find a factorial value for given number );
          System.out.println("Enter a value  ");
          Scanner sc = new Scanner(System.in);         
          int F= sc.nextInt();
          sc.close();
         
         int sf=1;     
         for (int n=1; n<=F ; n++) {
          sf *= n;  
         } 
          
         System.out.println("Factorial "+F+"!=" + sf );         
     }
    
    }
     

    Factorial number - Java program output

    
    Find a factorial value for given number 
    
    Enter a value  
    5
    Factorial 5!=120
    
    
    

    Comments

    Popular posts from this blog

    Solving System of Linear Equations by Gauss Jordan Elimination

    Matrix Forward and Back Substitution

    Solve System of Linear Equations by LU Decompose

    Chebyshev distance between two points

    Binary 1's and 2's Complement