Q. Write a program in JAVA to print the following triangles (taking height
as command line input from user):
a.
1
2 2
3 3 3
4 4 4 4
b.
1 2 3 4
1 2 3
1 2
1
a:
import java.io.*;
class triangle1
{
public static void main(String args[])
{
int i,j,n;
n=Integer.parseInt(args[0])
for(i=n;i>=1;i--)
{
for(j=1;j<=i;j++)
{
System.out.print(+j);
}
System.out.println( );
}
}
}
Output:
at command line take input 4
1
2 2
3 3 3
4 4 4 4
b:
import java.io.*;
class triangle2
{
public static void main(String args[])
{
int i,j,n;
n=Integer.parseInt(args[0])
for(i=1;i<=1;i++)
{
for(j=1;j<=i;j++)
{
System.out.print(+j);
}
System.out.println( );
}
}
}
Output:
at command line take input 4
1 2 3 4
1 2 3
1 2
1
/*if you have any problem, comment below*/
a.
1
2 2
3 3 3
4 4 4 4
b.
1 2 3 4
1 2 3
1 2
1
a:
import java.io.*;
class triangle1
{
public static void main(String args[])
{
int i,j,n;
n=Integer.parseInt(args[0])
for(i=n;i>=1;i--)
{
for(j=1;j<=i;j++)
{
System.out.print(+j);
}
System.out.println( );
}
}
}
Output:
at command line take input 4
1
2 2
3 3 3
4 4 4 4
b:
import java.io.*;
class triangle2
{
public static void main(String args[])
{
int i,j,n;
n=Integer.parseInt(args[0])
for(i=1;i<=1;i++)
{
for(j=1;j<=i;j++)
{
System.out.print(+j);
}
System.out.println( );
}
}
}
Output:
at command line take input 4
1 2 3 4
1 2 3
1 2
1
/*if you have any problem, comment below*/
Comments
Post a Comment