alex Alex
alex Alex

Reputation: 373

triangle numbers in java

I am new to Java and now I want to learn better for loop. I made some examples , but I don't know how to do a triangle that look like this: for n=6:

111111 
22222
3333
444
55
6

My code until now:

class Pyramid
{
public static void main (String[] args)
{
   int i,n=9,j; 
   for(i=1;i<=n;i++)
   {
       for(j=1;j<=i;j++)  {          
System.out.print(i); }      
System.out.print("\n");        
}}}

But what I managed to do it looks like this:

1
22
333
4444
55555
666666

How to make it in reverse order ?

Upvotes: 3

Views: 8108

Answers (5)

Harshit Pant
Harshit Pant

Reputation: 1065

If you want to print the triangular numbers then use the following code

`public class Triangular{
 public static void main(String[] args) {
    int i = 0;
    int j =0;
    int count =0;
    for (i=1;i<=10;i++){
        count = 0;  // This is a program to print triangular numbers in JAVA
        for(j=1;j<=i;j++){
            count = count + j;
        }
            System.out.println(count);
        }
    }
}`

Upvotes: 0

Manjunath
Manjunath

Reputation: 189

Can be done using below method:

public class Main {

    public static void main(String[] args) {

        int n = 6;
        int m =n;

        for (int i = 1; i <= n; i++,m--) {
            for (int j = 1; j <= m; j++) {
                System.out.print(i);
            }
            System.out.println();
        }

    }
}

Upvotes: 2

HouseWife
HouseWife

Reputation: 21

public class Pyramid {

    public static void main (String[] args)
    {
        int i,n=9,j; 
        for(i=1;i<=n;i++)
        {
            //for(j=1;j<=i;j++)  { 
            for(j=n;j>=i;j--) {
                System.out.print(i);
            }      
            System.out.print("\n");        
        }
    }
}

This should help.

Upvotes: 2

Bucket
Bucket

Reputation: 7521

Your issue is that your outer for loop was going from 6 to 1, so you need to reverse that.

Change

for(i=n;i>=1;i--) {

To

for(i = 1; i <= n; i++) { 

Further explanation, so you understand what is happening inside a for loop:

A for loop operates on three clauses: where you start, the condition that the loop runs, and what to do after it runs.

------v
for(i = 1; i <= n; i++) {

This is the assignment. You set a variable to a number, which is where the loop starts. In this case, we start with i = 1, since we want to print only one 1 on the first line. In the third clause, we will increment it (read: add one to it), and run the loop again.

--------------v
for(i = 1; i <= n; i++) {

This is the condition. The loop will run whenever this condition evaluates to true. In other words, if n = 6, this loop will run when i <= 6.

--------------------v
for(i = 1; i <= n; i++) {

This is what will happen each time the loop is executed. After it runs through once when i = 1, we will increment i, so now i = 2. This will happen until the condition (i <= n) evaluates to false, i.e. when i = 7. If the condition is false, the loop will terminate.

Upvotes: 4

user2246674
user2246674

Reputation: 7719

We can use is a function int numberForRow(int row) that will perform a suitable transformation. Then the function can be used like r = numberForRow(i); print(r). It needs to map this:

row (i) -> display number (r)
6          1
5          2
4          3
3          4
2          5
1          6

I think you can write it :)

Look at the relationship between the input (i) and output (r) - it might be useful to note that they always add up to the same value so a little bit of math should do the trick.

(While a function isn't strictly required I find that such functions can help break down a problem and, especially in this case, illustrate a transformation well - it also works in case of a "more advanced" transformation, such as was in the original question ;-)

Upvotes: 4

Related Questions