Write a program which draw isosceles Triangle Shape with asterisk(*) and using nested for loop
Program required output:
C++ Code:
Program Output:
More C++ Shapes Here
These kind of programs are very helpful in the understanding of nested for loop.
Program required output:
C++ Code:
#include<iostream>
|
Program Output:
More C++ Shapes Here
These kind of programs are very helpful in the understanding of nested for loop.
#include
ReplyDeleteusing namespace std;
int main()
{
for(int i=1;i<=5;i++)
{
for(int j=4;j>=i;j--)
cout<<" ";
for(int k=1;k<=2*i-1;K++)
cout<<"*";
cout<<endl;
}
return 0;
}
I use this code and the output is just a half triangle. Whatto do with that?
ReplyDeleteI use this code and the output is just a half triangle. Whatto do with that?
ReplyDeleteso what does it mean e=e+2; in this example above and why cstart 0 and e starts 1
ReplyDeletefor(int c=0;c<e;c++)
{
cout<<"*"; // Printing asterisk here
}
cout<<endl; // new line
e=e+2;
e increments by two (e=e+2) because if you notice, on line #1 there is one "*", then on line #2 there are 3 "*", then on line #3 there are 5 "*" and so on... (if you want to count the odds numbers, start at one and increment of 2 every time (1,3,5,7,9,...))
Deletec starts at 0 because there is step between 0 and 1 (e starts at one) and then one line #2, e = 3 and there is 3 steps between 0 and 3 and so on... So this is juste a way to always do the right amount of recursion loop.
plz reply to me i have an assigment coz ididnot understood nested loop for writng the shapes plzzzz clarify to me each and every thing and each for loop and its function
ReplyDeleteAt this point, I would just recommand to you that you copy this code and use the debugger to see what each line does...
DeleteBasically the loop using the int a and the int b are there to control the number of spaces and then the for(int c=0;c<e;c++) is there to control the amount of "*" that you need for each line
because there are 5 lines, you know the last line is 9 times "*" (the 5th odd number is 9). so if you what to have an isoscele triangle,
on line #1 : 4 spaces + 1 "*"
on line #2 : 3 spaces + 3 "*"
on line #3 : 2 spaces + 5 "*"
on line #4 : 1 space + 7 "*"
on line #5 : 0 space + 9 "*"
This comment has been removed by the author.
ReplyDelete/* This one ask the user how many lines he wants and print them in the console*/
ReplyDelete#include
using namespace std;
int main() {
int e=1;
int n;
cout<< "How many lines ?" << endl;
cin >> n;
for(int a=1;a<=n;a++) {
for(int b=(n-1);b>=a;b--) {
cout<<" ";
}
for(int c=0;c<e;c++) {
cout<<"*";
}
cout<<endl;
e=e+2;
}
}