printing Alphabets in a shape like diamond inside a square using C++
Hello From IQBAL! guys, after tiresome work i have produced the following code to print alphabets like below:
Below is the source code, feel free to comment your suggestions and corrections.
#include <iostream>
using namespace std;
int main(void){
int a= 65;
char height = 'G'; // can get this as input via cin>>
int endchar = (int) height;
int rows, c, k;
rows =endchar-a-1;
int steps = rows;
for (k=1; k<=rows+1; k++)
{
for(c=0; c<=steps; c++)
{
cout<<((char)(65+c));
}
for(c=1; c<=(2*k-2); c++)
{
cout<<" ";
}
if(k==1){
for(c=1; c<=rows; c++) {
cout<<((char)(endchar-c-1));
}
} else{
for(c=steps+1; c>0; c--)
{
cout<<((char)(a+c-1));
}
}
steps--;
cout<<"\n";
} //upper part ends here
//starts lower part
steps=2;
for(k=1; k<=rows; k++)
{
for(c=1; c<=steps; c++)
{
cout<<((char)(a+c-1));
}
for(c=1 ; c<=(2*(rows-k)-1); c++)
{
cout<<" ";
}
if(k==rows){
for(c=1; c<=rows; c++) {
cout<<((char)(endchar-c-1));
}
}else{
for(c=steps; c>=1; c--)
{
cout<<((char)(a+c-1));
}
}
steps++;
cout<<"\n";
}
}
please let us know your thoughts in comments below
Below is the source code, feel free to comment your suggestions and corrections.
#include <iostream>
using namespace std;
int main(void){
int a= 65;
char height = 'G'; // can get this as input via cin>>
int endchar = (int) height;
int rows, c, k;
rows =endchar-a-1;
int steps = rows;
for (k=1; k<=rows+1; k++)
{
for(c=0; c<=steps; c++)
{
cout<<((char)(65+c));
}
for(c=1; c<=(2*k-2); c++)
{
cout<<" ";
}
if(k==1){
for(c=1; c<=rows; c++) {
cout<<((char)(endchar-c-1));
}
} else{
for(c=steps+1; c>0; c--)
{
cout<<((char)(a+c-1));
}
}
steps--;
cout<<"\n";
} //upper part ends here
//starts lower part
steps=2;
for(k=1; k<=rows; k++)
{
for(c=1; c<=steps; c++)
{
cout<<((char)(a+c-1));
}
for(c=1 ; c<=(2*(rows-k)-1); c++)
{
cout<<" ";
}
if(k==rows){
for(c=1; c<=rows; c++) {
cout<<((char)(endchar-c-1));
}
}else{
for(c=steps; c>=1; c--)
{
cout<<((char)(a+c-1));
}
}
steps++;
cout<<"\n";
}
}
please let us know your thoughts in comments below
Comments
Post a Comment