Half Rhombus Shape Number Triangle in Java





package demo;
import java.util.Scanner;
public class HalfRhombus
{
 public static void main(String[] args) 
 {
  int num, n;
  Scanner sc = new Scanner(System.in);
  System.out.print("Enter Any Number : ");
  num = sc.nextInt();
  n = num;
  for (int r = 1; r <= num; r++, n--) 
  {
   for (int c = n; c <= num; c++) {
    System.out.print(c);
   }
   for (int c = r, j = num + 1; c > 1; c--, j++) {
    System.out.print(j);
   }
   System.out.println();
  }
  for (int r = 2; r <= num; r++) 
  {
   for (int c = r; c <= num; c++) {
    System.out.print(c);
   }
   for (int c = num - r, j = num + 1; c >= 1; c--, j++) {
    System.out.print(j);
   }
   System.out.println();
  }
 }
}

Output:

Enter Any Number : 4
4
345
23456
1234567
23456
345
4

BUILD SUCCESSFUL (total time: 6 seconds)


No comments:

Post a Comment