C++ program to draw the following pattern using loops : * *** ***** ******* ********* ********* ******* ***** *** * with algorithm
C++ program to draw the following pattern using loops :
*
***
*****
*******
*********
*********
*******
*****
***
*
Here is the C++ code to output like above . You all should have in mind how it works exactly. Here is the algorithm.
Steps
User side
1: When user run this code it simply output the result
Developer side
1: Declare variables that are going to be used in program .
2: Start a loop from i=1 to 10 (increase it by 2 after each iteration)
3: Inside the above loop initialize a variable equal 1 and iterate it until the following statement fails n<=i
4:output the "*" (this will output above triangle )
5: start a loop from i=10 to 1 (decrease it by 2)
6: Inside above loop start a loop from 1 to less than i
7 : Inside above loop output "*"
8: Program ends here
Code:
//start
#include<iostream>
using namespace std;
void
main()
{ //first triangle
for(int i=1;i<=10;i=i+2)
{ for(int
n=1;n<=i;n++)
{
cout<<"*";
}
cout<<endl;
}
//2nd triangle
for(int i=10;i>=1;i=i-2)
{ for(int
n=1;n<i;n++)
{
cout<<"*";
}
cout<<endl;
}
system("pause");
}
//End
Comments
Post a Comment