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
is it possible to change the ‘if’ condition and create an oval shape?
***
* *
* *
* *
* *
***
dear this is not an oval shape ,please rectify your question.
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??
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.
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
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;
}
#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…….
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?
Nope
It won’t !
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?
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.
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)
Ya! there are other ways. But they will make the code longer. ‘abs’ tells the compiler to take only the absolute value of “i” form the for loop .
You can use array to make this Diamond shape. Take a look - Here
Great solution! I just ran across this problem in Deitel’s & Deitel’s C++ book.
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?
#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;
}
i want to print like this using C++ simple tools please help me as fast as u can…..
****** ******
***** *****
**** ****
*** ***
** **
* *
what’s the use of “abs”?
abs is used for the absolute values of “i” and “j”
Hai..i want to print like this using C..please help me as fast as u can…..
*********
* *
* *
* *
*********
thanx..
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.
hi,,,em sanav… can any 1 help me in file handling…
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:):)
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………:):)
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.
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
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
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!!!!
Reaally very simple and best solutions. Thanks buddy
thanks alot..gr8 help 4 begginners!