454,483 Members | 1,612 Online
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,483 IT Pros & Developers. It's quick & easy.

# [ques]use recursive function to print out n!

 P: n/a this is my C Programming homework, It wants me to input 10! and output the follow result 10! ¡õ 10 * 9! ¡õ 9 * 8! .................. 2* 1! ¡õ 1 I take it for decreasing by degress, so using two for() loop to execute.. but I spend two days to try and can not figure out what the problem is. Could anybody show me a hint with this ? thank you so much. I use the compiler with dec C++ to finish the C programming follow ---------------------------------------------------------------------------- #include #include long fact(int num); int main(void) { int i,j; /* counter */ int ans = 1; printf("Input the n!:"); scanf("%d", &i); for(i=10;i>=1;i--) { for(j=9;j>=0;j--) { ans *= j; printf("%d * %2d! = %d\n",i, j, i*j ); } printf("\n"); } system("pause"); return 0; } /* Recursive fonction */ long fact(int num) { if(num<=1){ return 1; } else{ return (num * fact(num-1)); } } -- [1;34m¢~¢w¢w¢w¢w¢¡ ùá [35mweiyu.csie.net [34mùá [30m¡¸¡¸¡¸¡¸¡¸¡¸¡¸¡¸¡¸¡¸¡¸¡¸¡¸[m [1;34m¢x[31m¤ý[32mªÌ[33m¤§[34m®a¢x ùá[36m keith [33m¡¸¡¸¡¸¡¸¡¸¡¸¡¸¡¸¡¸¡¸¡¸¡¸¡¸¡¸¡¸¡¸¡¸¡¸¡¸[m [1;34m¢¢¢w¢w¢w¢w¢£ ùá [37mµoªí©ó [36m140.120.238.173 [30m¡¸¡¸¡¸¡¸¡¸¡¸¡¸¡¸¡¸¡¸[m Nov 15 '05 #1
10 Replies

 P: n/a Hi This is quite easy. You don't need 2 for-loops for that. action! wrote: this is my C Programming homework, It wants me to input 10! and output the follow result 10! â†“ 10 * 9! â†“ 9 * 8! .................. 2* 1! â†“ 1 I take it for decreasing by degress, so using two for() loop to execute.. but I spend two days to try and can not figure out what the problem is. Could anybody show me a hint with this ? thank you so much. When you get questions like this, keep in mind that all the elements need not be put in a single loop. Some of them maybe formaing a series while others have to be done seperately. In your case, the first 10! can be done seperately. The rest forms a series, as 10!, (10-1)! 9!, (9-1)! ..... ...... ......... 1!,(1-1)! Since you already have a recursive function to computer the factorial of a number called fact(), you could use a single for loop to do the series. Hope you get the idea. If not, please feel free to contact me. Regards AK Owner Programmer's HQ http://groups.google.com/group/programhq Nov 15 '05 #2

 P: n/a ke*******@weiyu.csie.net (action!) writes: this is my C Programming homework, It wants me to input 10! and output the follow result 10! â†“ 10 * 9! â†“ 9 * 8! .................. 2* 1! â†“ 1 I take it for decreasing by degress, so using two for() loop to execute.. but I spend two days to try and can not figure out what the problem is. Could anybody show me a hint with this ? thank you so much. [snippet] Basically, you don't need a "for" loop if you employ a recursive function like the "fact()" defined in your code. Therefore, what you want is simply as follows: --- begin here #include #include long fact(int num); int main(void) { int i; /* counter */ printf("Input the n!:"); scanf("%d", &i); fact(i); } /* Recursive fonction */ long fact(int num) { if(num<=1){ return 1; } else{ printf("%d!\n|\n%d x (%d-1)!\n", num, num, num); return (num * fact(num-1)); } } --- end here -- Denis H. G. Nov 15 '05 #3

 P: n/a Denis H. G. wrote: ke*******@weiyu.csie.net (action!) writes: this is my C Programming homework, It wants me to input 10! and output the follow result 10! â†“ 10 * 9! â†“ 9 * 8! .................. 2* 1! â†“ 1 I take it for decreasing by degress, so using two for() loop to execute... but I spend two days to try and can not figure out what the problem is. Could anybody show me a hint with this ? thank you so much. [snippet] Basically, you don't need a "for" loop if you employ a recursive functionlike the "fact()" defined in your code. Therefore, what you want is simply as follows: --- begin here #include #include long fact(int num); int main(void) { int i; /* counter */ printf("Input the n!:"); scanf("%d", &i); fact(i); } /* Recursive fonction */ long fact(int num) { if(num<=1){ return 1; } else{ printf("%d!\n|\n%d x (%d-1)!\n", num, num, num); return (num * fact(num-1)); } } --- end here -- Denis H. G. I was also thinking the same thing. I did the following. HOWEVER, scanf() still has to checked for 'funky input chars'. I had no idea how to do this. I suppose I could have thought about it, but I was too lazy. -------------------ans.c-------------------------------------------------- #include #include int fact(int); int fact(int num) { if(num(<=1){ return 1; } else { printf("%d! * %d! \n",num,num-1); } } int main(void) { int i = 0; printf("Input the n! \n"); scanf("%d! \n", &i); fact(i); return 0; } ----------------------------------------------------------------------------- Chad Nov 15 '05 #4

 P: n/a Chad wrote: I was also thinking the same thing. I did the following. HOWEVER, scanf() still has to checked for 'funky input chars'. I had no idea how to do this. I suppose I could have thought about it, but I was too lazy. #include #include int main(void) { int i = 0; printf("Input the n! \n"); scanf("%d! \n", &i); fact(i); return 0; } \$ man scanf: `scanf' returns the number of input fields successfully scanned, con- verted and stored; the return value does not include scanned fields which were not stored. => Ans: check the return value is equal to one. Check the return values of functions. Check the return values of functions. Note that if scanf stops scanning then the remaining characters on input are still waiting to be read. So if scanf stops there will still be input waiting and you have to worry about that if you want to read anything else in. E.g. your format string will read the number entered, but if the next character is not a '!' it will be left waiting on the input buffer. Similarly for ' ' and '\n' after it. OTOH "%d%*s" will pick up the number and eat the rest of the string until the end of the input buffer. Check the return values of functions. -- imalone Nov 15 '05 #5

 P: n/a Hi action! wrote: this is my C Programming homework, It wants me to input 10! and output the follow result 10! â†“ 10 * 9! â†“ 9 * 8! .................. 2* 1! â†“ 1 So, do you want to display this excactly the way it is written here?? If so, I misunderstood the program as I thought you need to compute the value of each factorial. I take it for decreasing by degress, so using two for() loop to execute.. but I spend two days to try and can not figure out what the problem is. Could anybody show me a hint with this ? thank you so much. I use the compiler with dec C++ to finish the C programming follow ---------------------------------------------------------------------------- I suppose you meant dev C++ /* Recursive fonction */ long fact(int num) { if(num<=1){ return 1; } else{ return (num * fact(num-1)); } } Why do you need this fact()?? This is a function to computer factorial of a number. Actually, this is what made me misunderstand your question. What I see now, is that your assignment doesn't need recursion at all. It can be simply done like this : //////////////////////////////////////////// printf ( " \n 10! " ) ; for ( i = 10; i > 1; i -- ) { printf ( "\n %d * %d ! ", i, i-1 ) ; /* I don't know how to print that arrow you have shown. So, replace this comment by the printf() for the arrow */ printf ( " \n 1 " ) ; ////////////////////////////////////////////// If recursion is compulsory, here it goes : //////////////////////////////////// printf ( " \n 10! " ) ; fact ( ) ; printf ( " \n 1 " ) ; ////////////////////////////////////// void fact ( ) { static int i = 10; if ( i > 1 ) { printf ( "\n %d * %d ! ", i, i-1 ) ; /* I don't know how to print that arrow you have shown. So, replace this comment by the printf() for the arrow */ fact ( ) ; } } /////////////////////////////////////// Regards AK Owner Programmer's HQ http://groups.google.com/group/programhq Nov 15 '05 #6

 P: n/a Chad wrote: Denis H. G. wrote: ke*******@weiyu.csie.net (action!) writes: this is my C Programming homework, It wants me to input 10! and output the follow result 10! â†“ 10 * 9! â†“ 9 * 8! .................. 2* 1! â†“ 1 I take it for decreasing by degress, so using two for() loop to execute.. but I spend two days to try and can not figure out what the problem is. Could anybody show me a hint with this ? thank you so much. [snippet] Basically, you don't need a "for" loop if you employ a recursive function like the "fact()" defined in your code. Therefore, what you want is simply as follows: --- begin here #include #include long fact(int num); int main(void) { int i; /* counter */ printf("Input the n!:"); scanf("%d", &i); fact(i); } /* Recursive fonction */ long fact(int num) { if(num<=1){ return 1; } else{ printf("%d!\n|\n%d x (%d-1)!\n", num, num, num); return (num * fact(num-1)); } } --- end here -- Denis H. G. I was also thinking the same thing. I did the following. HOWEVER, scanf() still has to checked for 'funky input chars'. I had no idea how to do this. I suppose I could have thought about it, but I was too lazy. In case you still have no idea how to do it, please visit http://groups.google.com/group/programhq and check out "More I/O Stream" thread. I have posted the code for checking 'funky input chars' in case of inputting integers. -------------------ans.c-------------------------------------------------- #include #include int fact(int); int fact(int num) { if(num(<=1){ return 1; } else { printf("%d! * %d! \n",num,num-1); } } int main(void) { int i = 0; printf("Input the n! \n"); scanf("%d! \n", &i); fact(i); return 0; } ----------------------------------------------------------------------------- Chad Nov 15 '05 #7