You might want to look carefully at the scanf() statement. In particular, check that values of x and nmax are what you expect immediately after reading them. I doubt my concerns about your approach will go away, unless you restrain yourself to small values of x and nmax, though.