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

Recommended Answers

All 4 Replies

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

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?

Never mind i got the ans.

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);
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.