A Blog IS About Education and It related Knowledge....

Sunday, 9 November 2014

Print Triangle by using for loop in c++

08:29:00 Posted by Unknown No comments
write a function named draw_Asterisks that displays asterisks according to the pattern as shown below
*********
*******
*****
***
*

‪#‎include‬<iostream>
#include<conio.h>
main ()
{
void Draw_Asterisks (void);
clrscr ();
Draw_Asterisks ();
}
// Definition of Draw_Asterisks function
void Draw_Asterisks (void)
{
int u,i;
for(u=9; u>=1; u-=2)
{
for (i=1; i<=u i++)
cout<<" * ";
cout<<endl;

}


Explanation:


for(u=9; u>=1; u-=2) // outer loop
{
for (i=1; i<=u i++) cout<<" * "; // inner loop and their body statement ....

cout<<endl;

/////////////////////////////////////////////////////////////////////////////////////////////////////////////
Outer Loop Iteration#1: 'u' ki value ha 9
control aa gaya inner loop py 'i' ki value 1 ha or ye 9 times run ho g
output:
********* after that inner loop terminates
cout<<endl; // ye statement execute ho g control new line py transfer ho jaye ga
usky bad u-=2 statement execute ho g yani u ki value ma 2 minus ho ga.
//////////////////////////////////////////////////////////////////////////////////////////////////////////
Outer Loop Iteration#2: 'u' ki value ha 7
control aa gaya inner loop py 'i' ki value 1 ha or ye 9 times run ho g
output:
*********
******* after that inner loop terminates
cout<<endl; // ye statement execute ho g control new line py transfer ho jaye ga
usky bad u-=2 statement execute ho g yani u ki value ma 2 minus ho ga.
//////////////////////////////////////////////////////////////////////////////////////////////////////////
Outer Loop Iteration#3: 'u' ki value ha 5
control aa gaya inner loop py 'i' ki value 1 ha or ye 9 times run ho g
output:
*********
*******
***** after that inner loop terminates
cout<<endl; // ye statement execute ho g control new line py transfer ho jaye ga
usky bad u-=2 statement execute ho g yani u ki value ma 2 minus ho ga.
//////////////////////////////////////////////////////////////////////////////////////////////////////////
Outer Loop Iteration#4: 'u' ki value ha 3
control aa gaya inner loop py 'i' ki value 1 ha or ye 9 times run ho g
output:
*********
*******
*****
*** after that inner loop terminates
cout<<endl; // ye statement execute ho g control new line py transfer ho jaye ga
usky bad u-=2 statement execute ho g yani u ki value ma 2 minus ho ga.
///////////////////////////////////////////////////////////////////////////////////////////////////////////
Outer Loop Iteration#5: 'u' ki value ha 2
control aa gaya inner loop py 'i' ki value 1 ha or ye 9 times run ho g
output:
*********
*******
*****
***
** after that inner loop terminates
cout<<endl; // ye statement execute ho g control new line py transfer ho jaye ga
usky bad u-=2 statement execute ho g yani u ki value ma 2 minus ho ga.
/////////////////////////////////////////////////////////////////////////////////////////////////////////
Outer Loop Iteration#5: 'u' ki value ha 0 loop terminate kr jaye g ku k condition ha u greater than equal to 1 or value ho gai ha 0 so body execute ni ho g...

0 comments:

Post a Comment