I'm making a program, that findes the cubic root of the given number.
I wan't it without using pow()
, or only in a way that I could also write here num * num * num
, but that doesn't matter. The method, I have to do this is like the method in the code below.
I don't know, where is the problem. It works with the cubic numbers(1,8,27,64), but with other numbers don't. Here's the code:
cout << "Cube root";
cout << "Enter a number: ";
int x; double num = 0;
cin >> x;
while (true) {
if (pow(num + 1, 3) > x) {
if (pow(num + 0.1, 3) > x) {
if (pow(num + 0.01, 3) > x) {
if (pow(num + 0.001, 3) > x) {
break;
}
}
else { num += 0.01; }
}
else { num += 0.1; }
}
else { num += 1; }
}
cout << num;
For example:
Input: 8 Output: 2
Input: 64 Output: 4
Input : 12 Output: (none)
Input : 12340 Output: (none)
"Output: (none)" means no crash, but I think there is an endless loop.