Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Printing isoceles triangle based on user input

I can make my code print out the proper amount of asterisks, but I need the asterisks in the "row" plane to increment only once each line. Here is the current code:

package isoceles;

import java.util.Scanner;

public class Isoceles {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner stdIn = new Scanner(System.in);
        
        int userSpecified;//this will be the userSpecified triangle side size
        int triangleSide;//printed triangle side size
        int width, height;
        
        System.out.print("Enter the size of equal sides for isoceles triangle: ");
        userSpecified = stdIn.nextInt();
        
        for (int col=1; col<=userSpecified; col++) 
        {
           for (int row=1; row<=userSpecified; row++) 
           {
               System.out.print("*");
           }
           System.out.println("");
       }
   }
}

With an input of four, the output will look like this:

^^^^
^^^^
^^^^
^^^^

The desired output is:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

^
^^
^^^
^^^^

>Solution :

Your inner-loop is wrong, instead of row<=userSpecified, it should be row<=col. Try this:

import java.util.Scanner;

public class Isoceles {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner stdIn = new Scanner(System.in);
        
        int userSpecified;//this will be the userSpecified triangle side size
        int triangleSide;//printed triangle side size
        int width, height;
        
        System.out.print("Enter the size of equal sides for isoceles triangle: ");
        userSpecified = stdIn.nextInt();
        
        for (int col=1; col<=userSpecified; col++) 
        {
           for (int row=1; row<=col; row++) 
           {
               System.out.print("*");
           }
           System.out.println();
       }
   }
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading