Printing a damru/dumbbell pattern – Computer Sir Ki Class
  

Login
[lwa]



Code Learning #CPP#3128 siteicon   siteicon   siteicon  

Printing a damru/dumbbell pattern

A program that prints a damru or dumb bell pattern of stars where a size unit is given in form of half of the damru height.

Learning Objectives

  • Printing a filled damru/dumb bell pattern using adjustment of pre print spaces first in increasing order and then in decreasing order.

Source Code

TC++ #3128

#include <iostream.h>
#include <conio.h>
int main()
{
clrscr();
int n=5;
int i,j;
int sp=0;
for(int i=0;i<=n-1;++i)
{
for (j=1;j<=sp;j++) cout<<" ";
sp++;
for (j=1;j<=2*(n-i)-1;j++) cout<<"*";
cout<<endl;
}
sp=n-1;
for(int i=1;i<=n;++i)
{
for (j=1;j<=sp; j++) cout<<" ";
sp--;
for (j=1;j<=2*i-1;j++) cout<<"*";
cout<<endl;
}
getch();
return 0;
}

Source Code

#include <iostream>
using namespace std;
int main()
{
int n=5;
int i,j;
int sp=0;
for(int i=0;i<=n-1;++i)
{
for (j=1;j<=sp;j++) cout<<" ";
sp++;
for (j=1;j<=2*(n-i)-1;j++) cout<<"*";
cout<<endl;
}
sp=n-1;
for(int i=1;i<=n;++i)
{
for (j=1;j<=sp; j++) cout<<" ";
sp--;
for (j=1;j<=2*i-1;j++) cout<<"*";
cout<<endl;
}
return 0;
}

Test it !
  Related (?) :

Run Output

*********
*******
*****
***
*
*
***
*****
*******
*********


Suggested Filename(s): damru.cpp



Share

sunmitra| Created: 28-Jan-2018 | Updated: 28-Jan-2018|






Back