Jump to content

First N factorial numbers using recursion C

Right I need to write a program that finds the first N of factorial numbers, much like this but i need to write a function using recursion to do so.

 

So far I've got it working to find the factorial of one number, but i can't seem to get it too display all the first factorial numbers up to N.

 

Here is the code:

#include <stdio.h>

int fact (int x)
{	
	if (x == 1) return 1;
	else return x * fact(x-1);   
}

int main()
{
    int n, i;
    printf("Enter an intger number: ");
    scanf("%i", &n);
    
    printf("\n");
    
	for (i = 0; i<n; i++)
	{
	printf("Factorial of %i is %i\n", i+1, fact(n));
				
	}
       
}

And here is the output: As you can see it displays the factorial of the N only , not all the factorials up to N, as well.

 

 

97445d7b43e8d5437b02ff69916b3656.png

   
   
Link to comment
Share on other sites

Link to post
Share on other sites

On 2/24/2016 at 6:56 AM, rEaGeNeReary said:

put a printf before the return on fact() function.

A new line printf?

 

On 2/24/2016 at 8:47 AM, fizzlesticks said:

You're always calculating fact(n) inside your loop instead of using the counter.

That doesn't help really. An example bit of code would be most helpful.

   
   
Link to comment
Share on other sites

Link to post
Share on other sites

1 hour ago, GR412 said:

That doesn't help really. An example bit of code would be most helpful.

Look at your loop. Look at the variable you're passing to fact(). "n" never changes, it's "i" that changes.

Link to comment
Share on other sites

Link to post
Share on other sites

Mmmm...dynamic programming would help your implementation a little bit , but factorials will end up very quickly overflowing integers so I don't see much of a point for it.

 

Anyway , this could also work :

int n , temp = 1;
void fact(int i)
{
    std::cout << (temp*=i) << ' ';
    if(i<n)fact(i+1);
}
int main()
{
    std::cin >> n;
    fact(2);
}

:D

i5 4670k @ 4.2GHz (Coolermaster Hyper 212 Evo); ASrock Z87 EXTREME4; 8GB Kingston HyperX Beast DDR3 RAM @ 2133MHz; Asus DirectCU GTX 560; Super Flower Golden King 550 Platinum PSU;1TB Seagate Barracuda;Corsair 200r case. 

Link to comment
Share on other sites

Link to post
Share on other sites

1 hour ago, Nineshadow said:

Mmmm...dynamic programming would help your implementation a little bit , but factorials will end up very quickly overflowing integers so I don't see much of a point for it.

 

Anyway , this could also work :


int n , temp = 1;
void fact(int i)
{
    std::cout << (temp*=i) << ' ';
    if(i<n)fact(i+1);
}
int main()
{
    std::cin >> n;
    fact(2);
}

:D

I'm sorry but your soloutions are always far too complicated for me to even comprehend. I need a simple soloution that I can trace through easily. Thanks for the attempt though.

 

2 hours ago, madknight3 said:

Look at your loop. Look at the variable you're passing to fact(). "n" never changes, it's "i" that changes.

Well I can see that, but it still doesn't really help. I simply need someone to tell me how to do it. I already know the issue I jsut can't fix it.

   
   
Link to comment
Share on other sites

Link to post
Share on other sites

13 minutes ago, GR412 said:

I'm sorry but your soloutions are always far too complicated for me to even comprehend. I need a simple soloution that I can trace through easily. Thanks for the attempt though.

 

Well I can see that, but it still doesn't really help. I simply need someone to tell me how to do it. I already know the issue I jsut can't fix it.

What is so hard to understand? You generate 1! , print it , then generate 2! , print that one as well , and so on until you reach n!. All recursively.

 

You still don't know how to fix it? Come on dude. We told you fact(n) always remains constant since n never changes , and that it should have been "i" instead of "n". What more than that do you want ? D:

i5 4670k @ 4.2GHz (Coolermaster Hyper 212 Evo); ASrock Z87 EXTREME4; 8GB Kingston HyperX Beast DDR3 RAM @ 2133MHz; Asus DirectCU GTX 560; Super Flower Golden King 550 Platinum PSU;1TB Seagate Barracuda;Corsair 200r case. 

Link to comment
Share on other sites

Link to post
Share on other sites

Just now, Nineshadow said:

What is so hard to understand? You generate 1! , print it , then generate 2! , print that one as well , and so on until you reach n!. All recursively.

 

You still don't know how to fix it? Come on dude. We told you fact(n) always remains constant since n never changes , and that it should have been "i" instead of "n". What more than that do you want ? D:

Your doing it in a format i'm not familaur with, using all complicated shifts and temp varaibles and that. It isn't straight foward to me. And as for knowing the problem I am trying, i've been trying for about a week.

   
   
Link to comment
Share on other sites

Link to post
Share on other sites

37 minutes ago, GR412 said:

Your doing it in a format i'm not familaur with, using all complicated shifts and temp varaibles and that. It isn't straight foward to me. And as for knowing the problem I am trying, i've been trying for about a week.

You literally have one character wrong. To reiterate what everyone else has already said, you are printing fact(n) over and over in your for loop. n never changes, i changes. You want to print fact(i). 

Link to comment
Share on other sites

Link to post
Share on other sites

1 hour ago, GR412 said:

Your doing it in a format i'm not familaur with, using all complicated shifts and temp varaibles and that. It isn't straight foward to me.

That's fair. He gave you an example in C++, not C, so if you're not familiar with the C++ syntax I can understand the confusion.

 

1 hour ago, GR412 said:

Well I can see that, but it still doesn't really help. I simply need someone to tell me how to do it. I already know the issue I jsut can't fix it.

We are telling you how to do it.

 

20 minutes ago, PlutoNZL said:

You literally have one character wrong. To reiterate what everyone else has already said, you are printing fact(n) over and over in your for loop. n never changes, i changes. You want to print fact(i). 

I hope that is clear enough for you. You literally only have to change 1 letter.

Link to comment
Share on other sites

Link to post
Share on other sites

1 hour ago, GR412 said:

Your doing it in a format i'm not familaur with, using all complicated shifts and temp varaibles and that. It isn't straight foward to me. And as for knowing the problem I am trying, i've been trying for about a week.

 

Better now?

int n , factorial = 1;
void fact(int i)
{
    factorial*=i;
    printf("%i ",factorial);
    if(i<n)fact(i+1);
}
int main()
{
    scanf("%i",&n);
    fact(2);
}

i5 4670k @ 4.2GHz (Coolermaster Hyper 212 Evo); ASrock Z87 EXTREME4; 8GB Kingston HyperX Beast DDR3 RAM @ 2133MHz; Asus DirectCU GTX 560; Super Flower Golden King 550 Platinum PSU;1TB Seagate Barracuda;Corsair 200r case. 

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×