You need to reset your values of x and y during each of your loops :). What you have is roughly this:
for(z = 0; z <=n; z++)
for(; y < z; y++)
for(; x < y; x++)
Instead you need to reset x and y each time their loops are reached, so:
while(z <= n) {
y = 0;
while(y < z) {
x = 0;
//...