I need a C++ code that ouputs the following: Enter an odd number for the a diamo
ID: 3763851 • Letter: I
Question
I need a C++ code that ouputs the following:
Enter an odd number for the a diamond (1-19): 22
Enter an odd number for the a diamond (1-19): 111
Enter an odd number for the a diamond (1-19): -1
Enter an odd number for the a diamond (1-19): 6
Enter an odd number for the a diamond (1-19): 17
*
***
*****
*******
*********
***********
*************
***************
*****************
***************
*************
***********
*********
*******
*****
***
*
Press any key to continue . . .
Explanation / Answer
working c++ code
#include<iostream>
using namespace std;
int main()
{
int n, c, k, gap = 1;
int flag; // to check if number is even or odd
cout<<"Enter an odd number for the diamond"<<endl;
cin>>n;
flag=n%2;
if(n>19 || n< 1 || (flag==0))
{
while(1)
{
cout<<"Enter an odd number for the diamond"<<endl;
cin>>n;
flag=n%2;
if(n<19 && n >=1 && (flag == 1))
break;
}
}
n=(n-1)/2;
gap = n - 1;
for (k = 1; k <= n; k++)
{
for (c = 1; c <= gap; c++)
cout<<" ";
gap--;
for (c = 1; c <= 2*k-1; c++)
cout<<"*";
cout<<endl;
}
gap = 1;
for (k = 1; k <= n - 1; k++)
{
for (c = 1; c <= gap; c++)
cout<<" ";
gap++;
for (c = 1 ; c <= 2*(n-k)-1; c++)
cout<<"*";
cout<<endl;
}
return 0;
}
compiled on ideone.
http://ideone.com/fE765V
output available at same ide.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.