We're a community of 1.1M IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,080,470 Members — Technology Publication meets Social Media

# Listing

Hi, how do i output a list,1-5 of the following factorial?

``````#include <stdio.h>
#include <stdlib.h>
#define fact 5

int main(void)
{
int i,result=1;
for (i=fact;i>=1;i--)
result*=i;

printf("factorial of (%d) = %d\n", fact, result);

return(EXIT_SUCCESS);
}
``````
3
Contributors
4
Replies
1 Day
Discussion Span
1 Year Ago
Last Updated
5
Views
Question
Valiantangel
Junior Poster in Training
89 posts since Jul 2010
Reputation Points: 10
Skill Endorsements: 0
``````printf("factorial of (%d) = %d\n", fact, result);
printf("press Enter to exit\n";
getchar();

return (EXIT_SUCCESS);

or do you mean:

#include <stdio.h>
#include <stdlib.h>

#define fact 5

int main(void)
{
int i, j, result;
int _fact = fact;

for (j = 0; j < fact; j++, _fact--)
{
result = 1;

for (i = _fact ; i >= 1; i--)
{
result *= i;
}
printf("factorial of (%d) = %d\n", _fact, result);
}

printf("\npress Enter to exit\n");
getchar();

return (EXIT_SUCCESS);
}
``````
nullptr
Junior Poster
158 posts since Mar 2012
Reputation Points: 46
Skill Endorsements: 0

yes your send ans is good.But do u need to use the two loops?Can it be achieved using 1?

for (j = 0; j < fact; j++, _fact--)

for (i = _fact ; i >= 1; i--)

Also i cant seem understand what does j do?

Valiantangel
Junior Poster in Training
89 posts since Jul 2010
Reputation Points: 10
Skill Endorsements: 0

Never mind i got the ans.

Valiantangel
Junior Poster in Training
89 posts since Jul 2010
Reputation Points: 10
Skill Endorsements: 0
Question Answered as of 1 Year Ago by nullptr

For me this is better and simpler:

``````#include <stdio.h>
#include <stdlib.h>
#define fact 5

int main(void)
{
int i,result=1;
for (i=1;i<=fact;i++) {
result*=i;
printf("factorial of (%d) = %d\n", i, result);
}

return(EXIT_SUCCESS);
}
``````
pyTony
pyMod
Moderator
6,330 posts since Apr 2010
Reputation Points: 879