+ 1
Unable to form right triangle of star..just getting output like***** vertically...kindly do help me
#include <iostream> using namespace std; int main() { int i,j; i=1; j=1; //outer loop for lines while (i<=5) { // inner looo for star while (j<=i) { cout<<"*"; j++; } cout<<"\n"; i++; } return 0; }
2 ответов
+ 3
after the inner loop you have to reset j = 1 otherwise when execute again it will be always equal to the i variable:
first time
j=1
j=i (so exit loop)
j++ -> j=2
after that i++ -> i=2
so second time yiu have:
j=2
i=2
so it will execute only once again!
You should do this:
#include <iostream>
using namespace std;
int main()
{
int i,j;
i=1;
j=1;
//outer loop for lines
while (i<=5)
{
// inner looo for star
while (j<=i)
{
cout<<"*";
j++;
}
j = 1;
cout<<"\n";
i++;
}
return 0;
}
+ 1
Thanks alot dear