Results 1 to 3 of 3
Like Tree1Likes
  • 1 Post By JosAH

Thread: Need some help with this Numbers triangle

  1. #1
    UmairBaloch is offline Member
    Join Date
    Nov 2012
    Posts
    5
    Rep Power
    0

    Post Need some help with this Numbers triangle

    Hi everyone,
    I'm assigned to make a triangle using numbers and I'm only allowed to use while loop
    the program should give output like this
    *****1
    ****121
    ***12221
    **1222221
    *122222221
    (Ignore the "asterisks,*")
    I've sorted out how to make a triangle but I can't figure out how to display the above written output.
    this is the code
    Java Code:
    class numtriangle{
        public static void main(String a[]){
    
        int trianglesize =10;
        int num = 1;
        int spaces = trianglesize / 2;
        int count = 0;
        while (num <= trianglesize)
        {
    
        while (count < spaces)
        {
        System.out.print(" ");
        count++;
        }
        count = 0;
        while (count < num)
        {
        System.out.print("1");
        count++;
        }
        count = 0;
        spaces--;
        num++;
        num++;
        System.out.println();
        }
        }
        }
    and this generates the below output
    *****1
    ****111
    ***11111
    **1111111
    *111111111

    any of you guys might want to help would be highly appreciated
    (Sorry about 2 previous threads, I was jst testing things, how to post etc.)

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,516
    Rep Power
    25

    Default Re: Need some help with this Numbers triangle

    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,560
    Blog Entries
    7
    Rep Power
    21

    Default Re: Need some help with this Numbers triangle

    Quote Originally Posted by UmairBaloch View Post
    Java Code:
        count = 0;
        while (count < num)
        {
        System.out.print("1");
        count++;
        }
    I quoted the part of the code that prints digits on a line; you want the first and the last digit to be a '1' and the rest of the digits should be a '2'. So, when count == 0 || count == num-1 the digit should be a '1', else it should be a '2'.

    kind regards,

    Jos
    gavrielk likes this.
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Triangle Pattern with numbers
    By JayP in forum New To Java
    Replies: 2
    Last Post: 06-05-2011, 07:27 PM
  2. Replies: 11
    Last Post: 01-14-2011, 06:36 PM
  3. Triangle using Numbers
    By Anandt88 in forum New To Java
    Replies: 16
    Last Post: 06-05-2010, 04:10 PM
  4. printing two smallest numbers from a series of numbers
    By trofyscarz in forum New To Java
    Replies: 2
    Last Post: 10-14-2008, 11:46 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •