Monday, December 3, 2012

Printing Patterns Using Asterisks in c++

It is a very common question for both C and C++ programmers that printing a diamond shape using asterisks (*) and this question is going to be ask in almost every freaking semester ;) .There are a lot of different ways to do that. But among them I found this one easier. Those who likes to use C code for printing diamond just change the header #include<iostream> to #include<stdio.h>, cout to printf and cin to scanf.Lets start the fun :)

#include<iostream>

#include<cstdlib>

using namespace std;

int main()

{

int i=0, j=0, NUM=3;

for(i=-NUM; i<=NUM; i++)

{

for(j=-NUM; j<=NUM; j++)

{

if( abs(i)+abs(j)<=NUM) // Change this condition

 { cout<<"*"; }

else { cout<<" ";}

}

cout<<endl;

}
return 0;
}

The above code will prints-
 *
***
*****
*******
*****
***
*
 Wait I haven’t done yet. You can have fun with it. Just change the ‘ if ‘ condition. You will get new design :) . Wanna see???. Lets take a look-



if( abs(i)*abs(j)<= NUM)
prints-
***
***
*******
*******
*******
***
***


if( abs(i)*abs(j)<= NUM)
prints-
*
*  *
*      *
*          *
*      *
*  *
*


if(abs(i)==abs(j)
prints-
*          *
*      *
*  *
*
*  *
*      *
*          *


if( abs(i)==0||abs(j)==0)
prints-
*
*
*
*******
*
*
*


if(abs(i)>=abs(j))
prints-
*******
*****
***
*
***
*****
*******


if(abs(i)<=abs(j))
prints-
*          *
**      **
***  ***
*******
***  ***
**      **
*          *

That’s it. Just change the condition and have fun with it. ENJOY !


2 comments:

  1. what do i need to print 5 rows of 10 astericks

    ReplyDelete
  2. Play Real Money Slots in NJ | JammyHub
    Free Slots Sites — 고양 출장마사지 Play real money 여주 출장마사지 online slots in NJ. 아산 출장샵 Check 과천 출장안마 our list of top sites to play Real Money slots and get a top bonus in December 광주광역 출장샵 2021.

    ReplyDelete