output a Triangle in java

Govinda2020
Govinda2020 used Ask the Experts™
on
I have written a program on a pyramid in java and now struggling with Triangle.
where the output should be
         1
       2  3
   4   5   6

so on till my input is.
now the program I wrote is :

public class PyramidB
 {
  public static void main(String args[])
  {
   int r,c,n,m;
   n = Integer.parseInt(args[0]);
   for(r=1;r<=n;r+=2)
   {
     for(m=r;m>0;m-=2)
       System.out.print(" ");
       for(c=1;c<r;c++)
         System.out.print(" "+c);
       System.out.print(" "+r);
     System.out.println(" ");
   }
  }
 }

 'r' - indicates the rows,'c' - indicates the column and 'm' for the third loop to be run

Can somebody help me out in the third loop which is actually wrong.

when I input 7 ,my output was
 
 1
   1 2 3
    1 2 3 4 5
     1 2 3 4 5 6 7
I have compiled..and there aren't any errors.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
check this one

public class PyramidB
{
 public static void main(String args[])
 {
  int r,c,n,m;
  n = Integer.parseInt("8");
  for(r=1;r<=n;r+=2)
  {
        //System.out.println("r = " + r);
    for(m=(n-r);m>=0;m-=2)
    {
        System.out.print(" ");
    }
    for(c=1;c<=r;c++)
    {
        System.out.print(""+c);
    }
    //System.out.print(" "+r);
    System.out.println("");
  }
 }
}

Author

Commented:
Gurinder -your program has an output

     1
   1 2 3
  1 2 3 4 5
 1 2 3 4 5 6 7
the inclination in the output is odd numbers...and always starts with 1
not completely..
it would be better if my output is

            1
          1  2
       1   2   3
    1   2   3   4
   
or

          1
        2  3
     4   5  6
  7   8   9   10
check this

public class PyramidC
{
 public static void main(String args[])
 {
  int r,c,n,m;
  n = Integer.parseInt("8");
  for(r=1;r<=n;r+=2)
  {
        //System.out.println("r = " + r);
    for(m=(n-r);m>=0;m-=2)
    {
        System.out.print(" ");
    }
    for(c=1;c<=((r/2)+1);c+=1)
    {
        System.out.print(" "+c);
    }
    //System.out.print(" "+r);
    System.out.println("");
  }
 }
}
Fundamentals of JavaScript

Learn the fundamentals of the popular programming language JavaScript so that you can explore the realm of web development.

Got the output for
            1
          1  3
       1   3   5
    1   3   5   7

public class PyramidB
 {
  public static void main(String args[])
  {
   int r,c,n,m;
   n = Integer.parseInt(args[0]);
   for(r=1;r<=n;r+=2)
   {
    for(m=(n-r);m>0;m-=2)
       {
       System.out.print(" ");
       }
      for(c=1;c<=r;c+=2)
         {
         System.out.print(" "+c);
         }
       //System.out.print(" "+r);
     System.out.println(" ");
   }
  }
 }

My increment was +2 so all odd numbers are printing but what if I want my output to be

          1
        2  3
     4   5  6
  7   8   9   10

My increment would be +1 but what else is supposed to be changed..

Author

Commented:
Can anyone help me to get a triangle as below.where each row has a new number.
Is it possible

          1
        2  3
     4   5  6
  7   8   9   10

Author

Commented:
Any other way of writing the program

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial