HOW TO PRINT DIAMOND / RHOMBUS SHAPE OF '*' IN JAVA LANGUAGE





Hello readers, today I will tell you the logic to print the diamond or rhombus shape of asterisk symbol ('*'). We will use Java language to implement this logic but you can easily modify the code to any other language such as C & C++.

The output of program will be:
HOW TO PRINT DIAMOND / RHOMBUS SHAPE OF '*' IN JAVA


We will follow the time tested method of 'divide and conquer' to solve this problem. We will divide the shape into two parts as shown below:

 Part 1:
HOW TO PRINT DIAMOND / RHOMBUS SHAPE OF '*' IN JAVA LANGUAGE | PART 1

Part 2:
HOW TO PRINT DIAMOND / RHOMBUS SHAPE OF '*' IN JAVA LANGUAGE | PART 2

In implementation we will first print the upper part (part 1) and then the lower part (part 2) of shape.

Program to print diamond / rhombus shape


/*Java program to print diamond or rhombus shape
2014 © EzyHow.com*/
class Diamond
{
 public static void main(String args[])
 {
  int spaces,j,i;
  for(i=1;i<=4;i++)                    // To print part 1 of shape
  {
   for(spaces=4-i;spaces>0;spaces--)   // To print white spaces
    System.out.print(' ');
   for(j=2*i-1;j>0;j--)                // To print '*'
    System.out.print('*');
   System.out.print('\n');
  }
  for(i=3;i>=0;i--)                    // To print part 2 of shape
  {
   for(spaces=0;spaces<4-i;spaces++)   // To print white spaces
    System.out.print(' ');
   for(j=0;j<2*i-1;j++)                // To print '*'
    System.out.print('*');
   System.out.print('\n');
  }
 }
}

Note: If you are writing your code in notepad (or any other text editor), then you must save it as Diamond.java file.
To execute it follow the instruction below:
1. First open command prompt (cmd).
2. Change directory to directory where you saved the file (Diamond.java) . For this you can use cd command.
If you are on windows 7 or above then you can directly go to directory where you saved file and press & hold shift key and then right click. This will open a context menu, from which choose 'open command window here'.
3. Then type javac Diamond.java command and press enter key to compile the program.
If there is no syntax error in code and environment variables are set correctly, then above command will a create a new file in directory named Diamond.class.
4. Now type java Diamond command and press enter key.
If everything is alright then you will get the output as shown below:
OUTPUT | HOW TO PRINT DIAMOND / RHOMBUS SHAPE OF '*' IN JAVA LANGUAGE

Explanation Of Code

In the above code we have created a class Diamond having the main method. First line of main method declares the three variables of integer type. We will use these variables as loop control variables in later code.

The first outer for loop for(i=1;i<=4;i++){...} is used to print the upper part of shape. It further has two inner for loops. First one to print white spaces and second one to print the asterisk symbol ('*').  First inner for loop for(spaces=4-i;spaces>0;spaces--){...} (used to print white spaces), prints three white spaces in first run of outer for loop and decrement by one, in each next run of outer loop till the number of white spaces becomes zero. Opposite to it, second inner loop for(j=2*i-1;j>0;j--){...} prints one asterisk symbol ('*') in first run of outer loop and increments by two in each next run of outer loop till '*' becomes seven in number.

The second outer for loop for(i=3;i>=0;i--){...} is used to print the lower part of shape. It also have two inner for loops, again one for printing white spaces and other one to print the asterisk symbol ('*'). First inner loop for(spaces=0;spaces<4-i;spaces++){...} prints the one white space in first run of outer loop and in each next run of it increments the number of white spaces by one. Second inner loop prints the five asterisk symbol ('*') in first run of outer loop and in each next run  of it, decrements the number of asterisk symbol by two.

You can easily modify the number of rows of this shape by changing the value of i but remember that second outer for loop must have value of i initialized by the number less by one from the highest possible value of i in the execution of first outer for loop. You can also have fun by testing the different values for loop control variables i, j and spaces.

You may also want to read How to browse internet privately using firefox & chrome

Bonus Part

Just as bonus to my readers, I had converted the above code to C and C++ language.

C Program To Print Diamond / Rhombus Shape

/*C program to print diamond or rhombus shape of '*'
2014 © EzyHow.com */
#include<stdio.h>
#include<conio.h>
void main()
{
 int spaces,j,i;
 clrscr();
 for(i=1;i<=4;i++)                            // To print part 1 of shape
 {
  for(spaces=4-i;spaces>0;spaces--)    // To print white spaces
   printf(" ");
  for(j=2*i-1;j>0;j--)                 // To print '*'
   printf("*");
  printf("\n");
 }
 for(i=3;i>=0;i--)                            // To print part 2 of shape
 {
  for(spaces=0;spaces<4-i;spaces++)    // To print white spaces
   printf(" ");
  for(j=0;j<2*i-1;j++)                 // To print '*'
   printf("*");
  printf("\n");
 }
 getch();
}

C++ Program To Print Diamond / Rhombus Shape

/*C++ program to print diamond or rhombus shape of '*'
2014 © EzyHow.com */
#include<iostream.h>
#include<conio.h>
void main()
{
 int spaces,j,i;
 clrscr();
 for(i=1;i<=4;i++)                           // To print part 1 of shape
 {
  for(spaces=4-i;spaces>0;spaces--)   // To print white spaces
   cout<<' ';
  for(j=2*i-1;j>0;j--)                // To print '*'
   cout<<'*';
  cout<<'\n';
 }
 for(i=3;i>=0;i--)                           // To print part 2 of shape
 {
  for(spaces=0;spaces<4-i;spaces++)   // To print white spaces
   cout<<' ';
  for(j=0;j<2*i-1;j++)
   cout<<'*';                  // To print '*'
  cout<<'\n';
 }
 getch();
}

You can replace asterisk symbol ('*') with any other symbol (such as @ # $ % ^ & etc.) as your wish.

 Also read: How to make embedded videos responsive.

If you have any doubt or suggestion for above written code then don't hesitate to comment below.





Sharing is a good habit

SHARE

Harinder Singh

  • Follow Author on Twitter
  • Like Author on Facebook
  • dribbble
  • pinterest
  • behance
  • instagram
    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment