Results 1 to 2 of 2
  1. #1
    miraclefruit is offline Member
    Join Date
    Nov 2016
    Posts
    10
    Rep Power
    0

    Question Triangle Printing Program

    Write an application that displays the following patterns separately, one below the other. The use should be asked to enter an integer that will determine how many lines of the pattern are printed out. Use for loops to generate the patterns. All asterisks (*) should be printed by a single statement of the form System.out .print('*'); which causes the asterisks to print side by side. A statement of the form System.out .println(); can be used to move to the next line. A statement of the form System.out .print(' '); can be used to display a space for the last two patterns. There should be no other output statements in the program . [Hint: The last two patterns require that each line begin with an appropriate number of blank spaces.]

    This is what I have so far:
    Java Code:
    public class TrianglePrinting
    {
     public static void main(String args[])
     {
      System.out.println("Pattern A");
      loopa();
      System.out.println("Pattern B");
      loopb();
      System.out.println("Pattern C");
      loopc();
      System.out.println("Pattern D");
      loopd();
     }
     static void loopa()
     {
      for(int i=0;i<10;i++)
      {
       for(int j=0;j<=i;j++)
       {
        System.out.print("*");
       }
       System.out.println();
      }
     }
     static void loopb()
     {
      for(int i=10;i>=0;i--)
      {
       for(int j=0;j<=i;j++)
       {
        System.out.print("*");
       }
       System.out.println();
      }
     }
     static void loopc()
     {
      for(int i=0;i<=10;i++)
      {
       for(int j=0;j<=i;j++)
       {
        System.out.print(" ");
       }
       for(int k=10;k>=i;k--)
       {
        System.out.print("*");
       }
       System.out.println();
      }
      
     }
     static void loopd()
     {
      for(int i=0;i<10;i++)
      {
       for(int j=10;j>=i;j--)
       {
        System.out.print(" ");
       }
       for(int k=0;k<=i;k++)
       {
        System.out.print("*");
       }
       System.out.println();
      }
     }
     
    }
    I attached the output and expected output with a user input of 4. My result almost doubles the triangle, Im not sure how to fix this. Any help would be appreciated.
    Thank you
    Attached Thumbnails Attached Thumbnails Triangle Printing Program-snip.jpg  

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Triangle Printing Program

    You are supposed to enter an integer which drives the size of the triangles. I don't see where you did that.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. Triangle program for beginner
    By shawnx715 in forum New To Java
    Replies: 18
    Last Post: 11-15-2011, 12:40 PM
  2. triangle program
    By computerbum in forum New To Java
    Replies: 8
    Last Post: 03-02-2010, 04:55 AM
  3. Help with calculating triangle program
    By ALH813 in forum New To Java
    Replies: 5
    Last Post: 10-19-2009, 05:08 AM
  4. Errors in Program (Right Triangle)
    By SupaStudy in forum New To Java
    Replies: 3
    Last Post: 03-26-2009, 10:42 AM
  5. Triangle Sides program
    By jamesov89 in forum New To Java
    Replies: 6
    Last Post: 10-06-2008, 03:36 AM

Posting Permissions

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