top button
Flag Notify
    Connect to us
      Facebook Login
      Site Registration Why to Join

Facebook Login
Site Registration

Printing triangle in Java

+1 vote
56 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 Google+ Share Button LinkedIn Share Button Multiple Social 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?

Contact Us
+91 9880187415
sales@queryhome.net
support@queryhome.net
#280, 3rd floor, 5th Main
6th Sector, HSR Layout
Bangalore-560102
Karnataka INDIA.
QUERY HOME
...