top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

Printing triangle in Java

+1 vote
280 views

Can someone help me to print this pattern in Java?

         *
      +  *  +
  *   +  *  +   *
posted Oct 14, 2013 by Nagarajuk

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

+1 vote

You need to use a for loop nested in another for loop. The syntax would look something like:

for(int i = 0; i < n; ++i)
{
    for(int j = 0; j < i; ++j)
    {
        for (int k=n-i; k>0;k++)
        {
              System.out.print(" ");
        }
        if ((i+j) % 2 == 0)
              System.out.print("*");
        else
              System.out.print("+");
    }
    System.out.println();
}
answer Oct 14, 2013 by Deepankar Dubey
Similar Questions
–1 vote

Floyd's triangle is a right-angled triangular array of natural numbers, used in computer science education. It is named after Robert Floyd. It is defined by filling the rows of the triangle with consecutive numbers, starting with a 1 in the top left corner.
Please help me how should i print the following pattern using c language.

1
2 3
4 5 6
7 8 9 10
11 12 13 14 15

+2 votes

Problem
Given a table of size n * m, with each cell having a color value from the set {r, g, b}, find the area of the largest triangle that has one side parallel with the y – axis (vertical) and has no two vertices that have the same color value.

Input
First line contains the size of the matrix i.e. n * m where n is the number of rows and m is the number of columns.
n lines follow each having a string of size m.

Output
A single value for the area of the triangle.

Provide the C Code for the above problem?

...