diamond help c++

The problem i need to solve with my code is to make a diamond that when an odd number like 5 is entered will look like this:

Please specify the width: 5
#
###
#####
###
#
and when width is even like 6:

Please specify the width: 6
##
####
######
####
##

i commented in where my code starts the odd or even loops, what i need to know is a way to skip to the odd loop if the input width is odd, thank you.



#include <iostream>
using namespace std;
int main()
{
int i,width;
cout << "enter width" <<endl;
cin>>width;

// if even
{
for( i=0;i<width;i=i+2)
{
for(int j=width;j>i;j--)
cout<<" ";
for(int k=0;k<i;k++)
cout<<" #";
cout<<endl;
}
for(i=width;i>0;i=i-2)
{
for(int j=width;j>i;j--)
cout<<" ";
for(int k=0;k<i;k++)
cout<<" #";
cout<<endl;

}
for(int i=0;i<width;i=i+2)
{
for(int j=width;j>i;j--)
cout<<" ";
for(int k=0;k<i;k++)
cout<<" *";
cout<<endl;


return 0;
} }

//if odd
for( i=0;i<width;i=i+2)
{
for(int j=width;j>=i;j--)
cout<<" ";
for(int k=0;k<i-1;k++)
cout<<" *";
cout<<endl;
}
for(i=width;i>=0;i=i-2)
{
for(int j=width;j>i;j--)
cout<<" ";
for(int k=0;k<i;k++)
cout<<" *";
cout<<endl;

}
for(int i=0;i<width;i=i+2)
{
for(int j=width;j>i;j--)
cout<<" ";
for(int k=0;k<i;k++)
cout<<" *";
cout<<endl;

return 0;
} }
diamonds should look like

Please specify the width: 6
##
####
######
####
##

Please specify the width: 5
#
###
#####
###
#
for some reason the diamonds won't show up the correct way but just imagine the lines line up to make a diamond lol
If you used code tags they would show up properly, it also makes your actual code a lot more readable. Edit your post, highlight the code and select the [<>] box under Format:
Last edited on
just

1
2
3
4
5
6
if( width % 2 == 0 ){
    //..do if it's even..
}
else{
    //..do if it's odd..
}
Last edited on
Topic archived. No new replies allowed.