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

Thread: Creating number Pyramids with For Loops

  1. #1
    Member Staticity's Avatar
    Join Date
    Jul 2011
    Location
    Texas
    Posts
    105
    My Mood
    Inspired
    Thanks
    3
    Thanked 5 Times in 5 Posts

    Default Creating number Pyramids with For Loops

    So, I just had a random urge (after reading a previous post) to make an infinite loop that creates this output:

    1
    12
    123
    1234
    12345
    1234
    123
    12
    1
    12
    123
    .... etc.

    So far I have this:

    import java.util.Scanner;
     
    public class ForTriangle {
    	public static void main(String args[]){
     
    		Scanner scan = new Scanner(System.in);
    		System.out.println("Enter length of numbers");
    		int number = scan.nextInt();
     
    		int a = 1;
    		int b = 1;
     
    	while(b < number){
    		for(a = 1; a <= number; a++){
    			for(b = 1; b <= a; b++){
    				if(b == number)
    					a = 0;
    				System.out.print(b);
    			}
    			System.out.println();
    		}
     
    	}
     
    	}//End MM
    }//End MC

    When the user enters 5, it produces:

    1
    12
    123
    1234
    12345
    1
    12
    123
    1234
    12345
    etc...

    Any tips?
    Simplicity calls for Complexity. Think about it.


  2. #2
    Super Moderator Sean4u's Avatar
    Join Date
    Jul 2011
    Location
    Tavistock, UK
    Posts
    637
    Thanks
    5
    Thanked 103 Times in 93 Posts

    Default Re: Creating number Pyramids with For Loops

    Any tips?
    You can look at it as two sequences, one like this:
    1
    12
    123
    1234
    and one like this
    12345
    1234
    123
    12
    A loop that does those two over and over could be about right

  3. #3
    Member Staticity's Avatar
    Join Date
    Jul 2011
    Location
    Texas
    Posts
    105
    My Mood
    Inspired
    Thanks
    3
    Thanked 5 Times in 5 Posts

    Default Re: Creating number Pyramids with For Loops

    If I try to do this, the first for loop will run on forever, and ever.. And the second for loop will never be used because the first for loop never ended. I already seemed to have lost interest in this.. Thanks for replying so quickly though Sean!
    Simplicity calls for Complexity. Think about it.

  4. #4
    Super Moderator Sean4u's Avatar
    Join Date
    Jul 2011
    Location
    Tavistock, UK
    Posts
    637
    Thanks
    5
    Thanked 103 Times in 93 Posts

    Default Re: Creating number Pyramids with For Loops

    You need an outer 'loop forever' with two inner loops. Inside the outer loop the first inner loop loops from min to max - 1, then it exits. The second inner loop loops from max to min + 1, then exits. After the second inner loop exits, the outer 'loop forever' makes the first loop run again...

Similar Threads

  1. Creating a new class and dont know how to generate a random number math code
    By beatlebaby70 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: September 15th, 2011, 03:03 PM
  2. Creating and implementing class for creating a calendar object
    By kumalh in forum Object Oriented Programming
    Replies: 3
    Last Post: July 29th, 2011, 08:40 AM
  3. How to returned random number to original number?
    By i4ba1 in forum Algorithms & Recursion
    Replies: 2
    Last Post: March 19th, 2011, 04:35 AM
  4. Creating pyramids with for loops
    By jericajam in forum What's Wrong With My Code?
    Replies: 3
    Last Post: March 4th, 2011, 05:56 AM
  5. [SOLVED] Sets and creating a new set containing a common number
    By mds1256 in forum Collections and Generics
    Replies: 2
    Last Post: February 26th, 2010, 05:00 PM