Factorials in C

Discuss how to write good code, break bad code, your current pet projects, or the best way to approach novel problems

Factorials in C

What kind of loop can you use to calculate the first ten factorials?
Or just how to display factorials at all...

I just started going the long way

printf( "%i %2i\n" , 1 , nResult = 1);
printf( "%i %2i\n", 2, nResult = 1 * 2);
printf( "%i %2i\n", 3, nResult = 1 * 2 * 3);
and so on....

BenJAMMIN89
New User

Posts: 3
Joined: Tue Sep 15, 2009 3:19 pm
Blog: View Blog (0)

Re: Factorials in C

Heres a quick and dirty c program to print the first 10 factorials.

Code: Select all
`int factorial(int sum){   if(sum <= 1) return 1;   else return sum * factorial(sum - 1);}int main(){   int i;   for(i = 10; i > 0; i--)   {      printf("%i! = %i\n", i, factorial(i));   }}`
faazshift
Contributor

Posts: 516
Joined: Wed Jun 03, 2009 3:55 pm
Location: Riverton, Utah
Blog: View Blog (0)

Re: Factorials in C

BenJAMMIN89 wrote:What kind of loop can you use to calculate the first ten factorials?

You don't use a loop, you would use recursion like faazshift showed.

Tentra
Poster

Posts: 161
Joined: Wed Apr 30, 2008 4:52 pm
Blog: View Blog (0)

Re: Factorials in C

Well, I suppose if you just want to display the first 10 factorials, utilizing less cpu than the previous code, you can just do something like this:

Code: Select all
`int main(){   int i, sum;   for(i = 0, sum = 1; i < 10; i++)   {      sum *= i + 1;      printf("%i! = %i\n", i + 1, sum);   }}`
faazshift
Contributor

Posts: 516
Joined: Wed Jun 03, 2009 3:55 pm
Location: Riverton, Utah
Blog: View Blog (0)

Re: Factorials in C

Thanks faazshift. I'm reading "Programming in C" by Stephen Kochan. This was an exercise in one of the chapters about using the "for" statement. That was the answer I was looking for.

BenJAMMIN89
New User

Posts: 3
Joined: Tue Sep 15, 2009 3:19 pm
Blog: View Blog (0)

Re: Factorials in C

Use recursion its much easier.

Code: Select all
`public class rec {   int temp;   public static int factorial(int number)   {      if(number == 1)         return number;                  return number * factorial(number - 1);   }      public static void main(String[] args)   {      int num=5;      System.out.println(factorial(num));   }      }`

There's one in Java.
ReMiiX
New User

Posts: 16
Joined: Sun Aug 09, 2009 3:36 pm
Blog: View Blog (0)

Re: Factorials in C

faazshift wrote:Heres a quick and dirty c program to print the first 10 factorials.

Code: Select all
`int factorial(int sum){   if(sum <= 1) return 1;   else return sum * factorial(sum - 1);}int main(){   int i;   for(i = 10; i > 0; i--)   {      printf("%i! = %i\n", i, factorial(i));   }}`

that aint gona work...you have made tipical mistake, in argumen for function factorial you have made call by value, and that is bad...the real, correct code if following:

Code: Select all
`#include <stdio.h>#include <string.h>int factorial (int *sum){        int i;        for (i=0; i!=10; i++)        {                 *sum--;                i++;                printf ("%d. = %d\n", i, *sum);         }         return 0;}int main (){int limit;struct struktura{char naredba[10];int try;int close;}uradi;petlja: printf ("Write in biggest factorial\n");scanf ("%d", &limit);factorial (limit);printf ("Here it is...\nIf you want to try with difrent factorials, write in try, if not, write in close\nfactoriel>");scanf ("%s", &uradi.naredba);try = strcmp (uradi.naredba, "try");close = strcmp (uradi.naredba, "close");if (try == 0){goto petlja;}else if (close == 0){return 0;}else{printf ("I don't know what to do with you...go to start of the program...\n");goto petlja;}}`

here it is, i think it works, i didn't compiled it (written first time in this post...)...enjoy, test it, and then enjoy more...hh...
Last edited by Capitan Hook on Sun Dec 27, 2009 7:36 pm, edited 1 time in total.
Capitan Hook
New User

Posts: 17
Joined: Sun Dec 27, 2009 2:20 pm
Blog: View Blog (0)

Re: Factorials in C

Capitan Hook wrote:that aint gona work...you have made tipical mistake, in argumen for function factorial you have made call by value, and that is bad...the real, correct code if following:

Hehe... it actually does work. I tested it plenty before posting. Maybe yours works better though, I don't know.
faazshift
Contributor

Posts: 516
Joined: Wed Jun 03, 2009 3:55 pm
Location: Riverton, Utah
Blog: View Blog (0)

Re: Factorials in C

sorry, I'v made a mistake...i hope mine works, i didn't compile it, but it's to short to have mistake...
btw. wich programming languages you use?
Capitan Hook
New User

Posts: 17
Joined: Sun Dec 27, 2009 2:20 pm
Blog: View Blog (0)

Re: Factorials in C

Capitan Hook wrote:btw. wich programming languages you use?

What languages do I know? Python, PHP, C, C++, Bash Scripting, C#, some Java, ASP.NET, and the web stuffs ((x)html, css, javascript).
What are my favorites? Python, PHP, C++
faazshift
Contributor

Posts: 516
Joined: Wed Jun 03, 2009 3:55 pm
Location: Riverton, Utah
Blog: View Blog (0)

Next