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 9 of 9

Thread: Need Help on Looping Program!! Beginner!!

  1. #1
    Junior Member
    Join Date
    Feb 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Need Help on Looping Program!! Beginner!!

    I'm having trouble on a program where I am supposed prompt the user for a positive integer and then displays those integers.

    My teacher told me to use this:
    Initialize a counter to 2
    while the counter is less than or equal to the number
    if the counter divides the number evenly
    display the counter
    divide the number by the counter to get a new number
    else increment counter by 1

    This is what I tried:

    import java.util.Scanner;
    public class PrimeFactors {
     
        public static void main(String []args) {
     
    		Scanner input = new Scanner(System.in);
     
        	int n;
        	int i;
     
        	System.out.println("What is your number?");
        	n = input.nextInt();
     
    		for(i = 2;i<=n;i++)
        	while (i <= n)
        	{
    		if(i%n == 0)
    			System.out.println(i);
    		else
    			i++;
        	}
     
     
        }
     
     
    }

    It has an infinite loop. Help?????


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,141
    Thanks
    48
    Thanked 2,156 Times in 2,129 Posts

    Default Re: Need Help on Looping Program!! Beginner!!

    To see what the code is doing, add some printlns that print out the values of i and n as it executes.

  3. #3
    Member
    Join Date
    Feb 2012
    Posts
    173
    Thanks
    6
    Thanked 10 Times in 10 Posts

    Default Re: Need Help on Looping Program!! Beginner!!

    Once the
    if(i%n == 0)
    where i%n does equal zero, it won't ever add 1 to i again. Also, when is the program exactly finished?

  4. #4
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: Need Help on Looping Program!! Beginner!!

    while (i <= n)
     
        	{
    		if(i%n == 0)
    			System.out.println(i);
    		else
    			i++;
        	}
    Look carefully. When your program is going to else part? What is the value of i? Why doesn't it change itself?

  5. #5
    Junior Member
    Join Date
    Feb 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need Help on Looping Program!! Beginner!!

    import java.util.Scanner;
    public class PrimeFactors {

    public static void main(String []args) {

    Scanner input = new Scanner(System.in);


    int n;

    System.out.println("What is your number?");
    n = input.nextInt();

    for(int i = 2;i<=n;i++)
    while (i <= n)
    {
    if(i%n == 0){
    System.out.println(i);
    n=n/i;
    }
    else
    i++;

    }


    }


    }

    Okay, I did this. However it just displays the number entered.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,141
    Thanks
    48
    Thanked 2,156 Times in 2,129 Posts

    Default Re: Need Help on Looping Program!! Beginner!!

    Did you try this:
    To see what the code is doing, add some printlns that print out the values of i and n and (i%n) as it executes.

    The print out will show you what the code is doing. You need to understand what the code is doing if you want to be able to change it to do what you want it to do.
    Last edited by Norm; March 1st, 2012 at 05:47 PM.

  7. #7
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: Need Help on Looping Program!! Beginner!!

    And wrap your code in code tags for better readability.

  8. #8
    Junior Member
    Join Date
    Feb 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Smile Re: Need Help on Looping Program!! Beginner!!

    once try this
    import java.util.*;
    ...edited by moderator

    or else according to your code

    ...
    Last edited by copeg; March 3rd, 2012 at 11:04 PM.

  9. #9
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,264
    Thanks
    176
    Thanked 821 Times in 764 Posts
    Blog Entries
    5

    Default Re: Need Help on Looping Program!! Beginner!!

    @rajivsomayaji, please read the forum rules and the following:
    The Problem with Spoon-feeding

Similar Threads

  1. Beginner needs help with a program
    By MartinC in forum What's Wrong With My Code?
    Replies: 4
    Last Post: February 8th, 2012, 08:51 AM
  2. Need Beginner Calculator Program help!
    By theJastro in forum What's Wrong With My Code?
    Replies: 18
    Last Post: December 17th, 2011, 06:30 PM
  3. For-looping, if-else statements, charAt(), etc. Beginner programming problem
    By ayelleeeecks in forum Loops & Control Statements
    Replies: 11
    Last Post: October 3rd, 2011, 12:54 PM
  4. Issue with beginner program
    By suxen in forum What's Wrong With My Code?
    Replies: 3
    Last Post: April 5th, 2011, 08:55 AM
  5. Replies: 24
    Last Post: April 14th, 2009, 03:43 PM