Problems with a while loop

So this code is suppose to print out the results of 0*1, 1*2, 2*3, 3*4, 4*5, 5*6, 6*7, 7*8, 8*9, and 9*10. It prints out the number 0 and thats it. Any help. Do I have the order wrong? I have tried x++ and y++ as well and same result.

1
2
3
4
5
6
7
8
9
10
11
12
13
  int x = 0;
  int y = 1;
  int z ;
  
    while (z<=90);
{   
    z= x*y;
    cout << z << "," ;     
    ++x;
    ++y;
             
}
cout << endl;
Remove the semi-colon from line 5.

Also, the while condition is not the best. In fact, once z is initialised, it will loop one more than you intended. You can do better.
Last edited on
(Please make sure you put entire compilable code in - including the main statement and the headers).

Problem 1 - you don't initialise z. So what is it supposed to be in line 5 when you compare it with 90?

Problem 2 - your while loop starts on line 5 ... and ENDS on line 5, because you have a semicolon at the end of it; remove this semicolon. The next block of code, enclosed in braces, won't loop until you do.
I knew it was gonna be something simply that I just wasn't seeing because I have been staring at it for hours.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include <iostream>
#include <stdio.h>
#include <stdlib.h>

using namespace std;

/**
 * 
 * 
 * @creator
 */

int main(int, char**) {

// part 1 of part 0

   int i;
    for(i=2; i<=20; i+=2)
    {
        cout << i << ",";
    }
cout<< endl ;

// part 2 of part 0

int n = 17;
  do
  {
   cout << n << ","  ;
   n = n-2 ;

  } while(n>=1);
cout << endl ;

// part 3 of part 0  
  
  int x = 0;
  int y = 1;
  int z = 0;
  
    while (z<90)
{   
    z= x*y;
    cout << z << "," ;     
    x++;
    y++;
             
}
cout << endl;
cout << endl;

//part 1 of 1

    int m;
    for(m=10; m<=95; m+=5)
{
    cout << m << " " ;
    if (m % 8 == 5) cout << endl;
}
cout << endl; 
Topic archived. No new replies allowed.