#include <iostream>
#pragma hdrstop
using namespace std;
int fun(int x){
return x*x;
}
int main ()
{
int n , i , x, t;
x = 0;
t = 0;
n = 75;
for (i= 1 ; fun (i) < n; i++){
cout << fun(i) << endl;
}
n = n - fun(i);
cout << n << endl;
}
Hey guys. I've got a problem with while or for loop and actually I dont know exactly where it is. When for loop is stoped the value of fun(i) is 64, but the next value is 81, why it's happening?
This is because of a misunderstanding in how for loops work. What it actually does is increments i, and then checks to see if the compare is true. What this means is that it is showing you 1 past the value you want. The easiest way to solve this is simply to pass n = n - fun(i - 1); instead.