473,486 Members | 2,353 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

[ques]use recursive function to print out n!

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 <stdio.h>
#include <stdlib.h>

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));
}
}

--
¢~¢w¢w¢w¢w¢¡ ùá weiyu.csie.net ùá ¡¸¡¸¡¸¡¸¡¸¡¸¡¸¡¸¡¸¡¸¡¸¡¸¡¸
¢x¤ýªÌ¤§®a¢x ùá keith ¡¸¡¸¡¸¡¸¡¸¡¸¡¸¡¸¡¸¡¸¡¸¡¸¡¸¡¸¡¸¡¸¡¸¡¸¡¸
¢¢¢w¢w¢w¢w¢£ ùá µoªí©ó 140.120.238.173 ¡¸¡¸¡¸¡¸¡¸¡¸¡¸¡¸¡¸¡¸
Nov 15 '05 #1
10 1548
AK
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
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 <stdio.h>
#include <stdlib.h>

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
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 <stdio.h>
#include <stdlib.h>

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 <stdio.h>
#include <stdib.h>

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
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 <stdio.h>
#include <stdib.h>

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
AK
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++

<snipped>
/* 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
AK

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 <stdio.h>
#include <stdlib.h>

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 <stdio.h>
#include <stdib.h>

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
AK wrote:
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 problemis.
>
> 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 <stdio.h>
#include <stdlib.h>

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 <stdio.h>
#include <stdib.h>

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

Hmmm.... I noticed I forget a line on the fact() function. I guess that
is what I get for typing it in vs copying and pasting actual working
code. The fact() I meant to use use was

int fact(int num) {
if(num(<=1){
return 1;
}
else {
printf("%d! * %d! \n",num,num-1);
fact(num-1); /*line I forget in the original code -( */
}

}
With that out of my system, I will just wander over to the link you
posted and read it. If I become too dense when I read it, I'll post my
confusion in the proper forum.

Chad

Nov 15 '05 #8
AK
Hi Chad

Chad wrote:
AK wrote:
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 <stdio.h>
> #include <stdlib.h>
>
> 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 <stdio.h>
#include <stdib.h>

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

Hmmm.... I noticed I forget a line on the fact() function. I guess that
is what I get for typing it in vs copying and pasting actual working
code. The fact() I meant to use use was

int fact(int num) {
if(num(<=1){
return 1;
}
else {
printf("%d! * %d! \n",num,num-1);
fact(num-1); /*line I forget in the original code -( */
}

}
With that out of my system, I will just wander over to the link you
posted and read it. If I become too dense when I read it, I'll post my
confusion in the proper forum.

proper forum?????????

Regards
AK

Nov 15 '05 #9
Hmmm.... I noticed I forget a line on the fact() function. I guess that
is what I get for typing it in vs copying and pasting actual working
code. The fact() I meant to use use was

int fact(int num) {
if(num(<=1){
return 1;
}
else {
printf("%d! * %d! \n",num,num-1);
fact(num-1); /*line I forget in the original code -( */
}

}
With that out of my system, I will just wander over to the link you
posted and read it. If I become too dense when I read it, I'll post my
confusion in the proper forum.

proper forum?????????

Regards
AK


Well, I was thinking that I wouldn't post a question about scanf() in
say alt.fetish.tongue. Okay, I have managed to wander totally off
topic.

Chad

Nov 15 '05 #10
On Mon, 07 Nov 2005 13:58:11 +0000, Ian Malone <ib***@cam.ac.uk>
wrote:
> scanf("%d! \n", &i);
=> Ans: check the return value is equal to one. Check the return values
of functions.

Check the return values of functions.
Right. (Twice.)
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
Right.
input buffer. Similarly for ' ' and '\n' after it.
Wrong. Any whitespace in a *scanf format matches not just that
specific character but _all_ consecutive whitespace in the input.
Thus the ' ' can match and "use up" say several newlines and several
leading spaces, stopping only at the next "printing" character (or EOF
including end-of-string or error). FAQ 12.17 at the usual places and
http://www.eskimo.com/~scs/C-faq/top.html .
OTOH "%d%*s" will
pick up the number and eat the rest of the string until the end of the
input buffer.
%s matches, and %*s discards, only upto whitespace (or end).

%*[^\n] discards up to newline, which will often probably usually be
the same as an "input buffer" but not always.
Check the return values of functions.


Still right. :-)
- David.Thompson1 at worldnet.att.net
Nov 15 '05 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
1738
by: Hal Halloway | last post by:
I'm trying to go from PHP4 to PHP5, all seemed OK until I tried to connect to a MYSQl DB. Then I get the error message: Fatal error: Call to undefined function mysql_pconnect() in C:\Program...
1
2043
by: ryan | last post by:
the win32 python extentions don't seem to have "SysParametersInfo()" and i need it to set the wallpaper in windows. cyptes has a "SysParametersInfoA()" function but wont let me use the...
1
14123
by: G Kannan | last post by:
Hey all! I have written a perl script to retrieve information from a HTML Form and insert the data into an Oracle database table. I am gettting the the following error message: "Use of...
4
1306
by: news.microsoft.com | last post by:
Hi again, I am pretty new to ASP.net (I use to be a Visual Basic 6 addict) I want to do the following but I have been unable to find good info/ or sample code on it; please help; - retrieve a...
2
1033
by: TS | last post by:
Hi, I had a microsoft guru help my team build a project. I noticed that in 2 situations where a value needed to be formatted in a certain way when viewed in browser, he would create a new class...
2
1989
by: G. Dean Blake | last post by:
We currently distribute a web application to serveral servers for a customer. We have been putting the connection string in our web.config file under <appSettings> <add...
6
4059
by: GrispernMix | last post by:
//ques and and level order traversal file name: lab6_build_leaf_up.cpp Instructions:
43
7534
by: dev_cool | last post by:
Hello friends, I'm a beginner in C programming. One of my friends asked me to write a program in C.The purpose of the program is print 1 to n without any conditional statement, loop or jump. ...
2
1211
by: muktagarg | last post by:
Wrie a function which returns the most frequent number in a list of integers. Handle the case of more than one number which meets this criterion. public static int GetFrequency(int list)
0
7105
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7132
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7180
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6846
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
1
4870
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4564
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3076
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3071
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1381
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.