Advertisement
jpHernandez

Factorial Code with GUI Prompt

Jul 23rd, 2015
597
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.99 KB | None | 0 0
  1. //program that shows the factors of the given number
  2. //author: jp hernandez
  3. //date: 7/23/2015
  4. //import statements
  5. import javax.swing.*;
  6.  
  7. public class factorial2 { //public class scope
  8.     public static void main(String args[]){ //start of main
  9.         //initialization of variables
  10.         String temp;
  11.         short number;
  12.         short limit;
  13.        
  14.         temp = JOptionPane.showInputDialog(null,"What are the factors of"); //ask for the number using GUI
  15.         number = Short.parseShort(temp); //then store it in variable number
  16.        
  17.         limit = number; //we copy the value entered in number to the limit variable for later use
  18.        
  19.         System.out.print("\nThe factors of " + number + " are: ");
  20.         //next, we will create a for loop for the factor running algorithm
  21.         for(int counter=1; counter<=limit ; counter++ ){//starting the loop up to the limit set
  22.             if(limit%counter==0){ //check if the current counter number is a factor
  23.                 System.out.print(counter + " "); //printing if true, otherwise, continue until the limit
  24.             }
  25.         }
  26.     }
  27. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement