Why while loop is not working instead of for loop?
I am creating a pyramid in C++, and for loop is working fine but when I came to while loop, the same code is not working. Here
for Loop:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
|
#include <iostream>
using namespace std;
int main()
{
int i,j;
for(i=1; i<=5; i++)
{
for(j=1; j<=i; j++)
{
cout << "* ";
}
cout << endl;
}
system("pause");
return 0;
}
| |
While Loop:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
|
#include <iostream>
using namespace std;
int main()
{
int i = 1, j = 1;
while (i <= 5)
{
while (j <= i)
{
cout << "* ";
j++;
}
cout <<endl;
i++;
}
system("pause");
return 0;
}
| |
Can you tell where I am making mistake in my while Loop?
Last edited on
You forgot to reset j to its original value before entering the inner loop.
Topic archived. No new replies allowed.