Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Java Program Help

  1. #1
    Junior Member
    Join Date
    Oct 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Java Program Help

    Print out 5 biggest factors of a number that is entered.

    I need help writing a program that will print out the 5 biggest factors of a number that is entered.

    For example:
    Enter A Number: 100
    The 5 Biggest Factors Are: 100, 50, 25, 20, 20.

    But if the number has less than 5 factors print out those factors.
    For example:
    Enter A Number: 7
    The factors for 7 are : 1,7

    Thanks Alot..


  2. #2
    Member
    Join Date
    Apr 2012
    Posts
    42
    Thanks
    8
    Thanked 4 Times in 4 Posts

    Default Re: Java Program Help

    What exactly do you need help understanding? All you've posted is basically what your assignment is. How much code have you already done? We can't help you if you don't show us what you've already done.

  3. #3
    Junior Member
    Join Date
    Oct 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java Program Help

    This is what I have so far. But its only printing out the first 5 factors. I don't know how to print out the biggest 5 factors.

    import java.io.*;
    public class Factors
    {public static input in = new input(); // input library
    public static void main(String[] args) throws IOException
    {int factor;
    int counter = 0;
    System.out.print("Enter A Number: ");
    factor=in.getInt();
    System.out.print("The First Five Factors of " + factor + " are ");
    for (int i=1; i <= factor; i++) {
    if (factor % i == 0) {
    counter ++;
    System.out.print(i + ",");
    }
    else if (counter == 5){
    i = factor;
    }
    }
    }
    }

  4. #4
    Member
    Join Date
    Apr 2012
    Posts
    42
    Thanks
    8
    Thanked 4 Times in 4 Posts

    Default Re: Java Program Help

    How far have you gone in the class? If you know how to implement array lists, then I imagine it would be easier to return all of the factors to an array list, sort the array list, and then print the highest factors, up to five integers. I can't think of an easy way to do this without some form of sorting.

  5. #5
    Junior Member
    Join Date
    Oct 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java Program Help

    Quote Originally Posted by bankston13 View Post
    How far have you gone in the class? If you know how to implement array lists, then I imagine it would be easier to return all of the factors to an array list, sort the array list, and then print the highest factors, up to five integers. I can't think of an easy way to do this without some form of sorting.
    We haven't gone really far in class. We are finishing while loops. My teacher stated that he wants us to use loops to do this program. I don't see how I could do this with loops.

  6. #6
    Member
    Join Date
    Jun 2012
    Location
    Left Coast, USA
    Posts
    451
    My Mood
    Mellow
    Thanks
    1
    Thanked 97 Times in 88 Posts

    Default Re: Java Program Help

    Quote Originally Posted by silvagabriel123 View Post
    This is what I have so far. But its only printing out the first 5 factors. I don't know how to print out the biggest 5 factors.
    Instead of starting at 1 and working your way up in the loop, how about starting at the number and working your way down? Terminate the loop when you have printed five factors or when the factor that you print is equal to 1.

    Cheers!

    Z
    Last edited by Zaphod_b; October 21st, 2012 at 09:23 PM.

Similar Threads

  1. Replies: 1
    Last Post: July 8th, 2012, 10:23 AM
  2. how to run a java program..when the program using jar
    By miriyalasrihari in forum Java Theory & Questions
    Replies: 2
    Last Post: May 17th, 2012, 10:04 AM
  3. Java Error cannot be applied to (java.lang.String), phone book entry program.
    By iceyferrara in forum What's Wrong With My Code?
    Replies: 5
    Last Post: September 23rd, 2011, 06:32 AM
  4. Convert Java Program to Java ME code
    By rinchan11 in forum Java ME (Mobile Edition)
    Replies: 1
    Last Post: October 5th, 2009, 10:18 PM
  5. Java Program Help
    By javakid93 in forum Java Theory & Questions
    Replies: 6
    Last Post: July 27th, 2009, 11:03 AM