Advertisement
Guest User

Factorial without multiplication shit!!!!

a guest
Jul 16th, 2018
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.48 KB | None | 0 0
  1. /*
  2.      *
  3.      *  Name: Sean Myko C. Baang
  4.      *  Section: CC11 - CCB
  5.      *  Date: 07/17/2018
  6.      *
  7. */
  8.  
  9. // Description...
  10.  
  11. /*  Factorial without using multiplication */
  12. /* I almost died */
  13.  
  14.  
  15. import java.util.Scanner;
  16.  
  17. class FactorialProgram
  18. {
  19.  
  20.     public static void main(String args[])
  21.     {
  22.        
  23.         Scanner reader = new Scanner(System.in); // Userinput shit...
  24.         System.out.println("[INPUT NUMBER]: ");
  25.         int n = reader.nextInt();
  26.         System.out.println("");
  27.        
  28.         if(n < 0)
  29.         {
  30.             System.out.println( "[ERROR]: userinput is less than 0." );     // Prints error if userinput is less than 0...
  31.         }
  32.        
  33.         if(n == 0)
  34.         {
  35.             n = 1;
  36.             System.out.println( "[FACTORIAL]: " + n );      // Prints the Factorial of n equals to 0...
  37.         }
  38.         else
  39.         {
  40.  
  41.             int x = 1; 
  42.             int y = n; 
  43.             int z = 0; 
  44.            
  45.             if (y == 1)
  46.             {
  47.                 System.out.println( "[FACTORIAL]: "  +  n ); // Prints if y is equal to 1...
  48.             }
  49.             else
  50.             {
  51.                 while(y != 1) // Loops y not equal to 1...
  52.                 {
  53.                     y = y - 1;
  54.                     x = y;
  55.                    
  56.                    
  57.                     while(y != 1) // While y is not equal to zero, it checks if x is either equal to 0 and vice versa...
  58.                     {
  59.                         if(x != 0)
  60.                         {
  61.                             z = z + n;
  62.                             x = x - 1;
  63.                         }
  64.                         else
  65.                         {
  66.                             y = y - 1;
  67.                             x = y - 1;
  68.                             n = z;
  69.                         }
  70.                     }
  71.                
  72.                 }
  73.                 System.out.println("[FACTORIAL]: " + n);
  74.                 System.out.println("");
  75.                 System.out.println("[Coded by Sean Baang]");
  76.                 System.out.println("[07/17/2018]");
  77.             }
  78.                
  79.         }
  80.        
  81.     }
  82.    
  83.  
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement