Write a C++ program that performs the given diamond images of output 1 and 2. Th
ID: 3911034 • Letter: W
Question
Write a C++ program that performs the given diamond images of output 1 and 2.
Thanks.
Explanation / Answer
#include<iostream>
using namespace std;
int main()
{
//variable declaration
int n;
char c;
cout<<"Do you want use the default values? (Y/N):";
cin>>c;
if(c=='y')
{
n=5;
c='*';
}
else
{
cout<<"Enter an integer for the side of the diamond: ";
cin>>n;//reading input
cout<<"Enter a char value for the character printed: ";
cin>>c;
}
int k=1,i=0,j=n,ii;
//printing diamond
while(i<n)
{
//printing space
ii=0;
while(ii<j)
{
cout<<" ";
ii++;
}
ii=0;
while(ii<k)
{
cout<<c;//printing character
ii++;
}
k+=2;
j--;
i++;
cout<<" ";
}
i--;k-=4;j+=2;
while(i>0)
{
ii=0;
while(ii<j)
{
cout<<" ";
ii++;
}
ii=0;
while(ii<k)
{
cout<<c;//printing character
ii++;
}
i--;
k-=2;
j++;
cout<<" ";
}
return 0;
}
output:
Do you want use the default values? (Y/N):n
Enter an integer for the side of the diamond: 3
Enter a char value for the character printed: #
#
###
#####
###
#
Process exited normally.
Press any key to continue . . .
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.