Print the numbers in Triangle or Pyramid Shape.
Here is the task, want to print the numbers in triangle or pyramid shape.
Like this,
1
2 2
3 3 3
4 4 4 4
Solution:
import java.util.Scanner;
public class PyramidShape {
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
System.out.println("Enter the Number of rows : ");
int rows = in.nextInt();
int count = 1;
for (int i = rows; i > 0; i--){
for (int j = 1; j <= i; j++){
System.out.print(" ");
}
for (int j = 1; j <= count; j++){
System.out.print(count+" ");
}
System.out.println();
count++;
}
}
}
Like this,
1
2 2
3 3 3
4 4 4 4
Solution:
import java.util.Scanner;
public class PyramidShape {
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
System.out.println("Enter the Number of rows : ");
int rows = in.nextInt();
int count = 1;
for (int i = rows; i > 0; i--){
for (int j = 1; j <= i; j++){
System.out.print(" ");
}
for (int j = 1; j <= count; j++){
System.out.print(count+" ");
}
System.out.println();
count++;
}
}
}
Comments