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)); }
6 Respostas
+ 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 😩
+ 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);
+ 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 :)
0
thanks ya all
0
Adarsh K Please also check your return type. ;)
0
everything is fine. once again thanks buddy