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

Thread: Triangle printing (for loop)

  1. #1
    Junior Member
    Join Date
    Nov 2010
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Smile Triangle printing (for loop)

    I'm trying to create this output using the for loop :
    aaaaa
    *aaaa
    o*aaa
    oo*aa
    ooo*a

    this is the code i wrote:

    public class Triangles
    {
    	public static void main(String args[])
    	{
    		int q=5;
     
    		for (int p=1; p<=q; p++ )
            {
    			for (int x=1; x<p; x++)
    	            System.out.print("o");
    	            System.out.print("*");
    	        for (int y=q; y>=p; y--)
    	            System.out.print("a");
    	        System.out.println("");
            }
    	}
    }

    but this is the output i get:

    *aaaaa
    o*aaaa
    oo*aaa
    ooo*aa
    oooo*a

    i tried changing the code a few times but all i get are weird outputs.
    Can anybody please tell me whats wrong with my code? and if its possible tell me which part should i fix.

    THANKS for any help


  2. #2
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Triangle printing (for loop)

    aaaaa
    *aaaa
    o*aaa
    oo*aa
    ooo*a

    Ok
    I take it you start with all a's
    then you change the first a to a *
    then you change that star to an o and move the * over to the second a
    and then you continue

  3. #3
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Triangle printing (for loop)

    public class Triangles
    {
        public static void main(String args[])
        {
         /*   int q=5;
     
            for (int p=1; p<=q; p++ )
            {
            	System.out.println("P is: " + p);
                for (int x=1; x<p; x++)
                	System.out.println("X is: " + x);
                    System.out.print("o");
                    System.out.print("*");
                for (int y=q; y>=p; y--)
                	System.out.println("Y is: " + y);
                    System.out.print("a");
                System.out.println("");
            }
            */
        	String str = "aaaaa";
        	String[] str2 = new String[5];
        	System.out.println(str);
        	for (int i =0; i < 5; i++)
        	{
     
        	char[] chars = new char[str.length()];
        	for (int x =0; x < chars.length; x++)
        	{
        	chars[x] = str.charAt(x);
        	}
        	if (str.charAt(i) == 'a')
        	{
        	chars[i] = '*';
        	str2[i] = new String(chars);
        	str = str2[i];
        	if ( i > 0 && str.charAt(i-1) == '*' )
        	{
        		chars[i-1] = 'o';
        		str2[i] = new String(chars);
        		str = str2[i];
     
        	}
        	System.out.println(str2[i]);
        	}
     
        	}
        }
    }

     

    aaaaa
    *aaaa
    o*aaa
    oo*aa
    ooo*a
    oooo*


    Last edited by javapenguin; December 6th, 2010 at 03:09 PM.

Similar Threads

  1. help with loop to print an equliateral asterisk triangle
    By everyone0 in forum Loops & Control Statements
    Replies: 13
    Last Post: October 11th, 2012, 12:37 PM
  2. Triangle Printing - Java Program
    By mparthiban in forum Java Programming Tutorials
    Replies: 1
    Last Post: January 5th, 2012, 09:00 AM
  3. [HELP] TRIANGLE!
    By kramista in forum Loops & Control Statements
    Replies: 10
    Last Post: July 29th, 2010, 12:58 PM
  4. [SOLVED] Printing Array without printing empty elements
    By CarlMartin10 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 12th, 2010, 02:41 AM
  5. Equilateral Triangle Using nested for loop
    By uchizenmaru in forum Loops & Control Statements
    Replies: 2
    Last Post: January 14th, 2010, 09:01 PM