Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    Regular Coder
    Join Date
    Jul 2007
    Location
    FL, USA
    Posts
    153
    Thanks
    13
    Thanked 0 Times in 0 Posts

    displaying factors of a number...

    guys, I'm in the middle of a program which ask for user input and gives back all factors of that number.... I'm doing it with "for loop"....

    Code:
    package factors;
    
    /**
     *
     * @author jorge
     */
    import java.util.Scanner;
    public class Main {
    
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
            // TODO code application logic here
         Scanner myscan = new Scanner(System.in);
         System.out.print("Please enter a number: ");
         int number = myscan.nextInt();
         for (int i = 0; i <= (number / i); i++){
             
             System.out.println(number % == 0);
         }
        }
    
    }
    any help appreciated
    thanks
    ____________________
    www.pctechtips.org

  • #2
    Regular Coder
    Join Date
    Jul 2007
    Location
    FL, USA
    Posts
    153
    Thanks
    13
    Thanked 0 Times in 0 Posts
    I'm bit slow today...

    public static void main(String[] args) {
    // TODO code application logic here
    int factor = 0;
    Scanner myscan = new Scanner(System.in);
    System.out.print("Please enter a number: ");
    int number = myscan.nextInt();
    for (int i = 1; i <= number; i++)
    {
    if(number % i == 0){
    factor = number / i;
    System.out.println(factor);
    }

    }
    }
    thanks
    ____________________
    www.pctechtips.org


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •