| | |
Need help turn this into a recursive function
Thread Solved |
•
•
Join Date: Sep 2008
Posts: 35
Reputation:
Solved Threads: 0
I created this function and I need help turning this into a recursive function
Thanks for the guidance
c Syntax (Toggle Plain Text)
#include "stdafx.h" #include "stdio.h" int fact (int n); // function prototype// int main (void) { int n,result; printf("please enter a positive integer"); scanf("%d", &n); result = fact(n); printf("The answer is %d\n", result); return 0; } int fact(int n) { int i, product=1; for(i=1; i<=n; i++) //algorithm { product *= i; } return (product); }
Last edited by Narue; Sep 27th, 2008 at 9:04 am. Reason: added code tags
Perhaps you should make an effort to search on the form. There might be like tons of results on this issues. But here is the pesudo code.
NB: Learn to start using code tags!
ssharish
C Syntax (Toggle Plain Text)
<return type of int> function name FACT ( taking int as an argument ) { if n equals to zero return one; else return n multiple FACT ( n minus one ); }
NB: Learn to start using code tags!
ssharish
•
•
Join Date: Sep 2008
Posts: 35
Reputation:
Solved Threads: 0
Is this correct for a recursive function I still have the following error (23) : error C2447: '{' : missing function header (old-style formal list?)
c Syntax (Toggle Plain Text)
#include "stdafx.h" #include "stdio.h" int fact (int n); // function prototype// int main (void) { int n,result; printf("please enter a positive integer"); scanf("%d", &n); result = fact(n); printf("The answer is %d\n", result); return 0; } int return1 (int n); { if (n<=1) return1; else return(n*fact(n-1)); }
Last edited by cscgal; Sep 27th, 2008 at 8:04 pm. Reason: Added code tags
int return1 (int n);Why is that semicolon there??? Delete that! Syntax error
ssharish
Last edited by ssharish2005; Sep 27th, 2008 at 4:41 pm.
•
•
Join Date: Sep 2008
Posts: 35
Reputation:
Solved Threads: 0
•
•
•
•
int return1 (int n);
Why is that semicolon there??? Delete that! Syntax error
ssharish
C:\Documents and Settings\Owner\My Documents\Visual Studio 2008\Projects\lab 8.1\Debug\lab 8.1.exe : fatal error LNK1120: 1 unresolved externals
•
•
•
•
int return1 (int n);
Why is that semicolon there??? Delete that! Syntax error
ssharish
Last edited by Sci@phy; Sep 27th, 2008 at 4:46 pm.
Well there are few thing which you need to consider. First of all the function name is not write. In you code you have declared a function prototype clearly, but why dosn;t that reflect in your function definiation.
Your function name is return1 which should be return1. And then within the function your should check for
ssharish
Your function name is return1 which should be return1. And then within the function your should check for
n <= 0 not 1 or n == 0 . And also if thats true you should return 1 not return1 . Give a space between return and 1.ssharish
"Any fool can know, point is to understand"
Here it is...
C Syntax (Toggle Plain Text)
#include "stdio.h" #include "conio.h" int fact (int n); // function prototype// int main (void) { int n,result; printf("please enter a positive integer"); scanf("%d", &n); result = fact(n); printf("The answer is %d\n", result); getch(); return 0; } int fact (int n) { if (n<=1) return 1; else return(n*fact(n-1)); }
regards,
Ahamed.
Ahamed.
![]() |
Similar Threads
- comparing file in c (C)
- Help with Fibonacci Series (C++)
- pyramid function (Python)
- powers of two, recursion. (C)
Other Threads in the C Forum
- Previous Thread: Real COM port vs. USB-to-Serial cable
- Next Thread: Generating a Binary tree from inorder and preorder
| Thread Tools | Search this Thread |
* ansi api array arrays bash binarysearch calculate centimeter changingto char character convert copyanyfile copypdffile createcopyoffile createprocess() csyntax directory dynamic fflush file floatingpointvalidation fork forloop frequency function getlasterror getlogicaldrivestrin givemetehcodez graphics gtkgcurlcompiling gtkwinlinux hardware highest histogram homework i/o ide inches initialization intmain() iso km license linked linkedlist linux linuxsegmentationfault list logical_drives looping loopinsideloop. lowest match matrix microsoft motherboard mqqueue mysql oddnumber odf open opendocumentformat openwebfoundation pdf pointer pointers posix power program programming pyramidusingturboccodes read recursion recv recvblocked repetition reversing scanf scheduling segmentationfault send shape single socketprogramming stack standard strchr string suggestions test unix urboc user variable whythiscodecausesegmentationfault win32api windows.h windowsapi





