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

Thread: help with loop to print an equliateral asterisk triangle

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

    Default help with loop to print an equliateral asterisk triangle

    here is my work so far :
    # include <stdio.h>
    main()
    {
    int  N,row, column;
    printf(“Enter the triangle-drawing value n:”);
    scanf(“%d”,&N);
    for (row=1; row<=N, row++)
    {
    	for ( column = 1 ;  column <= row ; column++ )
    	{
    		   printf(  “*”  );
    	}
                   for (space=0; space<=N-1; space--)
                     {
                           printf(" ");
                     }
    	printf(“\n”);
    }
    }

    the output is supposed to look like this when u enter n=5

    ----*
    ---***
    --*****
    -*******
    *********
    of course just the triangle ignoring the "-".but when i run the program it doesn't display that way and i still dont know why.


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: help with loop to print an equliateral asterisk triangle

    This is a forum dedicated to Java programming, not C/C++

  3. #3
    Junior Member
    Join Date
    Apr 2010
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: help with loop to print an equliateral asterisk triangle

    comeone im sure u know the answer tell me plz , and i promise i will learn java after i finish c learning

  4. #4
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: help with loop to print an equliateral asterisk triangle

    I'll give you some hints:

    1. The number of spaces to draw out is N - 1 - row (starting with row 0)
    2. The number of * to draw out starts at 1 and increases by 2 each time through, so 2 * row + 1 (starting with row 0)

    Fyi, learning Java before learning C/C++ is much easier (simply because then all you have to do is pick up some small syntax changes and memory/pointer management)

  5. #5
    Junior Member
    Join Date
    Apr 2010
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: help with loop to print an equliateral asterisk triangle

    thanks for the help , im done and i cant start java without finishing c im almost done only need to learn functions.

  6. #6
    Junior Member
    Join Date
    Apr 2010
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: help with loop to print an equliateral asterisk triangle

    hi helloworld922 , how have u been? , i have one more question what about the same triangle but inverted ?!

  7. #7
    Junior Member
    Join Date
    Apr 2010
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: help with loop to print an equliateral asterisk triangle

    nvm already done it , the equation of space = N-5+row and equation for stars = -2*row+9

  8. #8
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: help with loop to print an equliateral asterisk triangle

    Almost right on the stars: what if you wanted to print an inverted triangle with 6 rows?

  9. #9
    Junior Member
    Join Date
    Apr 2010
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: help with loop to print an equliateral asterisk triangle

    my guess is that it will be like this : space=6-6+row , stars = -2*row+11
    so what is the correct answer ?

  10. #10
    Junior Member
    Join Date
    Apr 2010
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: help with loop to print an equliateral asterisk triangle

    well i changed the space equation so it becomes like this : space=row

  11. #11
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: help with loop to print an equliateral asterisk triangle

    the space equation is right. Again, stars is correct, but the thing I was hinting towards is a general formula for stars with N rows.

    stars = 2 * (N - row) - 1
    space = row

  12. The Following User Says Thank You to helloworld922 For This Useful Post:

    everyone0 (April 19th, 2010)

  13. #12
    Junior Member
    Join Date
    Apr 2010
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: help with loop to print an equliateral asterisk triangle

    thank you very much

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

    Lightbulb Re: help with loop to print an equliateral asterisk triangle

    Quote Originally Posted by everyone0 View Post
    here is my work so far :
    # include <stdio.h>
    main()
    {
    int  N,row, column;
    printf(“Enter the triangle-drawing value n:”);
    scanf(“%d”,&N);
    for (row=1; row<=N, row++)
    {
    	for ( column = 1 ;  column <= row ; column++ )
    	{
    		   printf(  “*”  );
    	}
                   for (space=0; space<=N-1; space--)
                     {
                           printf(" ");
                     }
    	printf(“\n”);
    }
    }

    the output is supposed to look like this when u enter n=5

    ----*
    ---***
    --*****
    -*******
    *********
    of course just the triangle ignoring the "-".but when i run the program it doesn't display that way and i still dont know why.
    This will work fine try it. its a java code
    ...removed by moderator
    Last edited by copeg; October 11th, 2012 at 12:38 PM.

  15. #14
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,308
    Thanks
    181
    Thanked 824 Times in 767 Posts
    Blog Entries
    5

    Default Re: help with loop to print an equliateral asterisk triangle

    Quote Originally Posted by c.palihakkara View Post
    This will work fine try it. its a java code
    Thanks for the contribution, but you should recognize that a) you are resurrecting a post that is over 2 years old b) you are spoonfeeding code. Read the forum rules and The Problem with Spoon-feeding Your code has been removed as a result.

Similar Threads

  1. Triangle Printing - Java Program
    By mparthiban in forum Java Programming Tutorials
    Replies: 1
    Last Post: January 5th, 2012, 09:00 AM
  2. ASCII Triangle
    By physics in forum Loops & Control Statements
    Replies: 1
    Last Post: March 27th, 2010, 06:39 AM
  3. Triangle issues
    By FrEaK in forum What's Wrong With My Code?
    Replies: 0
    Last Post: February 24th, 2010, 07:49 AM
  4. Equilateral Triangle Using nested for loop
    By uchizenmaru in forum Loops & Control Statements
    Replies: 2
    Last Post: January 14th, 2010, 09:01 PM
  5. Triangle Question
    By Leeds_Champion in forum What's Wrong With My Code?
    Replies: 0
    Last Post: October 29th, 2009, 11:33 PM