0

what is the error in this program? it always shows no output, but no compilation error

#include <iostream> #define pi 3.14159 using namespace std; float power(float, int); int factorial(int); int main() { int b, i; float a, callOF1, callOF2; float sum = 0; cin >> a; cin >> b; cout << a; a *=(pi/180); cout << a; for (i = 0; i <= b; i++) { callOF1 = power(a, b); callOF2 = factorial(b); sum = sum + (callOF1 / callOF2); } cout << sum; return 0; } float power(float x, int n) { if (n == 0) return 1; else return (x * power(x, (n - 1))); } int factorial(int n) { if (n == 1 && n == 0) return 1; else return (n * factorial(n - 1)); }

17th Dec 2018, 4:30 AM
Adarsh K
6 Réponses
+ 3
The problem might be in the line if (n == 1 && n == 0). n can't be 1 and 0 at the same time so that condition will never be true and you're creating an infinite loop. Try to change it from && to ||. /Kirk Schafer Did you try to make him find the error by himself? If so, I'm sorry 😩
17th Dec 2018, 5:49 AM
Anna
Anna - avatar
+ 2
These are often caused by some kind of segmentation fault or hang. To diagnose a problem like this, you could try commenting code to check for problems. For example, why does the code show output again if I comment line 19: callOF2 = factorial(b);
17th Dec 2018, 4:54 AM
Kirk Schafer
Kirk Schafer - avatar
+ 2
Anna I regularly drop hints for "the old one-two", d.h. follow-up combos. It increases the learning pool + my timezone's different + what if someone's still stuck? All's well :)
17th Dec 2018, 5:11 PM
Kirk Schafer
Kirk Schafer - avatar
0
thanks ya all
17th Dec 2018, 3:19 PM
Adarsh K
0
Adarsh K Please also check your return type. ;)
17th Dec 2018, 5:12 PM
Kirk Schafer
Kirk Schafer - avatar
0
everything is fine. once again thanks buddy
17th Dec 2018, 5:14 PM
Adarsh K