Reputation: 51
The code is working just when r
is smaller than 1
(ex: 0.99
), but if r = 1
nothing appears:
#include <stdio.h>
#include <stdlib.h>
int main() {
int nr = 2;
float x1 = 0, x2 = 0, nextTerm = 0, r;
scanf("%f", &r);
while (nextTerm < r) {
nextTerm = (1 + x2 + x1 * x1) / 3;
x1 = x2;
x2 = nextTerm;
nr++;
}
printf("%d", nr);
return 0;
}
The code is working just when r
is smaller than 1
(ex: 0.99
), but if r = 1
nothing appears
Upvotes: 0
Views: 171
Reputation: 23
When you set a number greater than or equals to 1 while you are looping validated by a division of numbers smaller than 1, you are basically dividing a number smaller than 1 by a greater one (3, in the (1 + x2 + x1 * x1) / 3;
), so you'll never get a number greater than 1 to stop the loop. Because no matter what you put in the prompt, if the values of x1 and x2 are smaller than 3 you'll never get a number greater than 1.
Upvotes: 1