Tuesday 16 February 2016

Java: Draw Rhombus java program.

Problem: Rhombus

Draw a rhombus of given length.
Sample Input:
5
Sample Output:
        1
      123
    12345
  1234567
123456789
  1234567
    12345
     123
       1

Solve:
Lets see the following Code:
// ADLabs

import java.util.Scanner;

public class NastedForLoop {

 public static void main(String[] args) {

  Scanner sc = new Scanner(System.in);
  System.out.print("Enter any Int Num:");
  int n = sc.nextInt();
  System.out.println();

  // Rhombus

  for (int i = 0; i < n; i++) {

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

    System.out.print(" ");
   }
   for (int k = 1; k <= 2 * i + 1; k++) {

    System.out.print(k);
   }
   System.out.println();
  }
  for (int i = n - 2; i >= 0; i--) {

   for (int j = i; j <= n - 2; j++) {

    System.out.print(" ");
   }
   for (int k = 1; k <= 2 * i + 1; k++) {

    System.out.print(k);
   }
   System.out.println();
  }

  sc.close();
 }

}



Console:

Enter any Int Num:5

        1
      123
    12345
  1234567
123456789
  1234567
    12345
     123
       1

Happy Coding :)
-@D