Chapter 6:
Looping Structures
Programming Exercise
Problem # 45:
Write a program that displays a diamond of asterisks using loop.
*
***
*****
*******
*********
*******
*****
***
*
Solution:
#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
int i, k, l;
for(i=4;i>=1;i--)
{
for(k=1;k<=i;k++)
{
cout<<" ";
}
for(l=i;l<=8-i;l++)
cout<<"*";
cout<<"\n";
}
int i1, k1, l1;
for(i1=0;i1<=4;i1++)
{
for(k1=i1;k1>0;k1--)
{
cout<<" ";
}
for(l1=i1;l1<9-i1;l1++)
{
cout<<"*";
}
cout<<"\n";
}
getch();
}
#include<conio.h>
using namespace std;
int main()
{
int i, k, l;
for(i=4;i>=1;i--)
{
for(k=1;k<=i;k++)
{
cout<<" ";
}
for(l=i;l<=8-i;l++)
cout<<"*";
cout<<"\n";
}
int i1, k1, l1;
for(i1=0;i1<=4;i1++)
{
for(k1=i1;k1>0;k1--)
{
cout<<" ";
}
for(l1=i1;l1<9-i1;l1++)
{
cout<<"*";
}
cout<<"\n";
}
getch();
}
Output:
Let me know in the comment section if you have any question.
Previous Post:
44. Program that inputs the height of a triangle and displays it using loop.
Comments
Post a Comment