c++ - Write a subprogram that calculates n!. While using this subprogram make a program that calculates (a+b)! -
my task
write subprogram calculates
n!
. while using subprogram make program calculates(a+b)!
.
i wrote following code:
using namespace std; int f(int n) { for(int i=1;i<=n;i++){ f=f*i; } return f; } int main() { int a,b; cout<<"a="; cin>>a; cout<<"b="; cin>>b; cout<<"factorial of a+b="<<(a+b)*f; return 0; }
and error when compile:
in function 'int f(int)': 7:27: error: invalid operands of types 'int(int)' , 'int' binary 'operator*' 8:8: error: invalid conversion 'int (*)(int)' 'int' [-fpermissive] in function 'int main()': 16:34: error: invalid operands of types 'int' , 'int(int)' binary 'operator*' 17:9: error: expected '}' @ end of input
here's error
return f;
returns function pointer of function f()
. you're using function pointer calculations.
in c++ need declare variable can used calculate , return:
int f(int n) { int result = 1; for(int i=1;i<=n;i++) { result=result*i; } return result; }
Comments
Post a Comment