Print a diamond shape using asterisks in C/C++

It is a very common question for both C and C++ programmers that printing a diamond shape using asterisks (*) and this question is asked in almost every freaking semester ;) .There are a lot of different ways to do that. But among them I found this one easier. Those who likes to use C code for printing diamond just change the header #include<iostream> to #include<stdio.h>, cout to printf and cin to scanf.Lets start the fun :)

#include<iostream>

#include<cstdlib>

using namespace std;

int main()

{

int i=0, j=0, NUM=3;

for(i=-NUM; i<=NUM; i++)

{

for(j=-NUM; j<=NUM; j++)

{

if( abs(i)+abs(j)<=NUM) // Change this condition

 { cout<<"*"; }

else { cout<<" ";}

}

cout<<endl;

}
return 0;
}

The above code will prints-

*
***
*****
*******
*****
***
*

Wait I haven’t done yet. You can have fun with it. Just change the ‘ if ‘ condition. You will get new design :) . Wanna see???. Lets take a look-

if( abs(i)*abs(j)<= NUM)

prints-

***
***
*******
*******
*******
***
***

if( abs(i)+abs(j)==NUM)

prints-

*
*  *
*      *
*          *
*      *
*  *
*

if( abs(i)==abs(j))

prints-

*          *
*      *
*  *
*
*  *
*      *
*          *

if( abs(i)==0||abs(j)==0)

prints-

*
*
*
*******
*
*
*

if( abs(i)>=abs(j))

prints-

*******
*****
***
*
***
*****
*******

if( abs(i)<=abs(j))

prints-

*          *
**      **
***  ***
*******
***  ***
**      **
*          *


That’s it. Just change the condition and have fun with it. ENJOY :)
For other method you may check Print a Diamond shape using array

Advertisement
This entry was posted in C/C++, programming and tagged , , . Bookmark the permalink.

30 Responses to Print a diamond shape using asterisks in C/C++

  1. Kyriacos says:

    is it possible to change the ‘if’ condition and create an oval shape?
    ***
    * *
    * *
    * *
    * *
    ***

  2. dayeen says:

    Sorry for late reply. :) I had exm.
    Yes it is possible to change the if condition and create an oval shape.

    Look-In my code the for loop – “for(i=-NUM; i<=NUM; i++)" creates an 6×6 matrix and my "if" condition tells the compilers where to put the STARS and where to put the BLANK SPACE. You need to calculate where do you want to put your stars and change the "if" condition according to it :) Simple isn't it?? :D

  3. sheela says:

    Hai Can you tell me how to print the stats in right angled triangle shape?

    *
    **
    ***
    ****
    *****

    i want to print the stars in the above shape, is it possible, please help me.

    • dayeen says:

      Yes! it is possible. You need to change the for loops-

      for(i=-NUM; i<=0; i++)
      and
      for(j=0; j<=NUM; j++)
      This will do it :)

    • firstguest says:

      with out using the for loops
      you can print the triangle using a simple “cout” statements
      for example
      int main()
      {
      cout<<"*\n";
      cout<<"**\n";
      cout<<"***\n";
      cout<<"****\n";
      cout<<"*****\n";
      return 0;
      }

    • subhani says:

      #include
      int main()
      {
      for(int i=1;i<6;i++)
      {
      for(int j=1;j<=i;j++)
      {
      printf("*");
      }
      printf(" \n");
      }
      return 0;
      }

      I think this is the way…….

  4. eman ali says:

    in the above code if v not use stdlib and simply give the condition in if as if(i+j<=num).then this program remains correct?

  5. eman ali says:

    ok thx.i hve did this n foud how it is incorrect.bt i hve also a question that without using the if statement in nested for how will the diamond print?

    • dayeen says:

      Upsss! Sorry But I didn’t get it. What did u wanna do?? did u want to print the diamond shape without using loop? or you wanna use another method to print diamond?? If you wanna use another method you may use array. :)

  6. someone says:

    Is there a simpler way of doing it without using the “abs(i)”? I don’t understand how “abs(i)” works in the first place. (I’m a beginner)

  7. Chris C. says:

    Great solution! I just ran across this problem in Deitel’s & Deitel’s C++ book.

  8. jony says:

    if i want it to not print but stick it into an 2d array… how do i do it?
    ive tried changing the cout<<… to screen[i][j]='*'….
    in short i need to get the diamond into a 2d array instead of printing… any solutions?

    • Array Man says:

      #include
      #include
      using namespace std;

      int main()
      {
      int i=0, j=0, NUM=3;
      char array [7][7];
      for(i=-NUM; i<=NUM; i++)
      {
      for(j=-NUM; j<=NUM;)
      {
      if(abs(i)+abs(j)<=NUM)
      {
      array [3 + i][3 + j] = '*';
      j++;
      }
      else
      {
      array [3 + i][3 + j] = ' ';
      j++;
      }
      }
      }
      Print the array
      for(i=0; i<=6; i++)
      {
      for(j=0; j<=6;)
      {
      if(abs(i)+abs(j)<=NUM)
      {
      cout << array [i][j];
      j++;
      }
      else
      {
      cout << array [i][j];
      j++;
      }
      }
      cout << endl;
      }
      return 0;
      }

  9. bscs2010gcu says:

    i want to print like this using C++ simple tools please help me as fast as u can…..
    ****** ******
    ***** *****
    **** ****
    *** ***
    ** **
    * *

  10. beaiya14 says:

    what’s the use of “abs”?

  11. nurul says:

    Hai..i want to print like this using C..please help me as fast as u can…..

    *********
    * *
    * *
    * *
    *********

    thanx..

  12. Anisa Cochran says:

    I program with c++ on Microsoft Visual Studio and I need to know a program that asks the user to enter two numbers, obtains the two numbers from the user and prints the sum, product, difference, and quotient of the two numbers.

  13. satnav vishwakarma says:

    hi,,,em sanav… can any 1 help me in file handling…

  14. vikram says:

    Dear Dayeen,
    Reely thanx for your above codes…..It helped me a lot…:)
    can u generate a triangle lik dis??? kindly rply…:)

    *
    * *
    * * *
    * * * *
    * * * * *
    Thanx in advance:):)

  15. vikram says:

    Sorry…the above pattern is nt d one i needed….

    first i wanna print space followed by single asterisk, then d astrerisk space n one more asterisk….hope u got ma query………:):)

  16. aerastello says:

    Can anyone please help me on how to explain these codes? Next week is our examination, and this is what I am going to present. Haha

    Btw, thank you for this code. :)

  17. Vibhor says:

    program to print a diamond shape using a string.

    like if i give ram then out put should be
    a
    r a m
    a

    if i give 12345
    3
    234
    12345
    234
    3

  18. khalaf says:

    i need a code to print (the outline of diamond only) without {abs}funcion
    in{if( abs(i)+abs(j)==NUM)}
    (without using #include )
    please quickly reply I need it before 9/4/2011

  19. sanav says:

    hey!
    this stuff d’nt run in TURBO C++
    unusual figure is seen while running above procedure …
    How can i do this in TURBO c++ using abs()
    plz
    Help me!!!!

  20. info says:

    Reaally very simple and best solutions. Thanks buddy

  21. Me says:

    thanks alot..gr8 help 4 begginners!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s