C
coinjo
I need to write a program which takes a number as an input and prints a
diamond of # and $. The number of rows in the shape is equal to the
number entered by the user. Your program should display the shape for
both even and odd value of size. For example if user enters number 7
the program should print following shape.
#
#$#
#$#$#
#$#$#$#
#$#$#
#$#
#
If user enters a value 6, the program prints diamond in following
shape.
#
#$#
#$#$#
#$#$#
#$#
#
I have tried very hard and this is the best i could come up with
#include<iostream>
using namespace std;
int main()
{
int rows=0;
cout<<"Enter the number of rows of the diamond"<<endl;
cin>>rows;
int space=0;
int space1=0;
int space2=0;
int space3=0;
int space4=1;
int a=0;
int b=0;
space1=rows/2;
space2=space1;
space3=space1;
a=rows-2;
space=space1+a;
int count=0;
int count1=0;
int count2=0;
int count3=0;
int count4=0;
int count5=0;
int count6=0;
int char1=1;
int char2=0;
b=a-1;
char2=space1+b;
while(count1<space1)
{
count2=0;
count3=0;
while(count2<space2)
{
cout<<" ";
count2=count2+1;
}
while(count3<char1)
{
cout<<"#";
count3=count3+1;
if(count3<char1)
{
cout<<"$";
count3=count3+1;
}
}
cout<<endl;
count1=count1+1;
char1=char1+2;
space2=space2-1;
}
while(count<=space)
{
cout<<"#";
count=count+1;
if(count<space)
{
cout<<"$";
count=count+1;
}
}
cout<<endl;
while(count4<=space3)
{
count5=0;
count6=0;
while(count5<space4)
{
cout<<" ";
count5=count5+1;
}
while(count6<char2)
{
cout<<"#";
count6=count6+1;
if(count6<char2)
{
cout<<"$";
count6=count6+1;
}
}
space4=space4+1;
char2=char2-2;
count4=count4+1;
cout<<endl;
}
}
This above code works fine for 3 but not for other odd values like 5
and 7. Can anyone help?
Also how to make it work for even numbers?
diamond of # and $. The number of rows in the shape is equal to the
number entered by the user. Your program should display the shape for
both even and odd value of size. For example if user enters number 7
the program should print following shape.
#
#$#
#$#$#
#$#$#$#
#$#$#
#$#
#
If user enters a value 6, the program prints diamond in following
shape.
#
#$#
#$#$#
#$#$#
#$#
#
I have tried very hard and this is the best i could come up with
#include<iostream>
using namespace std;
int main()
{
int rows=0;
cout<<"Enter the number of rows of the diamond"<<endl;
cin>>rows;
int space=0;
int space1=0;
int space2=0;
int space3=0;
int space4=1;
int a=0;
int b=0;
space1=rows/2;
space2=space1;
space3=space1;
a=rows-2;
space=space1+a;
int count=0;
int count1=0;
int count2=0;
int count3=0;
int count4=0;
int count5=0;
int count6=0;
int char1=1;
int char2=0;
b=a-1;
char2=space1+b;
while(count1<space1)
{
count2=0;
count3=0;
while(count2<space2)
{
cout<<" ";
count2=count2+1;
}
while(count3<char1)
{
cout<<"#";
count3=count3+1;
if(count3<char1)
{
cout<<"$";
count3=count3+1;
}
}
cout<<endl;
count1=count1+1;
char1=char1+2;
space2=space2-1;
}
while(count<=space)
{
cout<<"#";
count=count+1;
if(count<space)
{
cout<<"$";
count=count+1;
}
}
cout<<endl;
while(count4<=space3)
{
count5=0;
count6=0;
while(count5<space4)
{
cout<<" ";
count5=count5+1;
}
while(count6<char2)
{
cout<<"#";
count6=count6+1;
if(count6<char2)
{
cout<<"$";
count6=count6+1;
}
}
space4=space4+1;
char2=char2-2;
count4=count4+1;
cout<<endl;
}
}
This above code works fine for 3 but not for other odd values like 5
and 7. Can anyone help?
Also how to make it work for even numbers?