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

Post by BenJAMMIN89 on Wed Sep 30, 2009 6:59 pm
([msg=30283]see Factorials in C[/msg])

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....
User avatar
BenJAMMIN89
New User
New User
 
Posts: 3
Joined: Tue Sep 15, 2009 3:19 pm
Blog: View Blog (0)


Re: Factorials in C

Post by faazshift on Wed Sep 30, 2009 8:00 pm
([msg=30285]see Re: Factorials in C[/msg])

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
Contributor
 
Posts: 516
Joined: Wed Jun 03, 2009 3:55 pm
Location: Riverton, Utah
Blog: View Blog (0)


Re: Factorials in C

Post by Tentra on Wed Sep 30, 2009 8:32 pm
([msg=30289]see Re: Factorials in C[/msg])

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.
User avatar
Tentra
Poster
Poster
 
Posts: 161
Joined: Wed Apr 30, 2008 4:52 pm
Blog: View Blog (0)


Re: Factorials in C

Post by faazshift on Wed Sep 30, 2009 8:54 pm
([msg=30290]see Re: Factorials in C[/msg])

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
Contributor
 
Posts: 516
Joined: Wed Jun 03, 2009 3:55 pm
Location: Riverton, Utah
Blog: View Blog (0)


Re: Factorials in C

Post by BenJAMMIN89 on Wed Sep 30, 2009 10:26 pm
([msg=30292]see Re: Factorials in C[/msg])

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.
User avatar
BenJAMMIN89
New User
New User
 
Posts: 3
Joined: Tue Sep 15, 2009 3:19 pm
Blog: View Blog (0)


Re: Factorials in C

Post by ReMiiX on Sun Nov 15, 2009 11:45 pm
([msg=30530]see Re: Factorials in C[/msg])

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
New User
 
Posts: 16
Joined: Sun Aug 09, 2009 3:36 pm
Blog: View Blog (0)


Re: Factorials in C

Post by Capitan Hook on Sun Dec 27, 2009 7:12 pm
([msg=32072]see Re: Factorials in C[/msg])

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
New User
 
Posts: 17
Joined: Sun Dec 27, 2009 2:20 pm
Blog: View Blog (0)


Re: Factorials in C

Post by faazshift on Sun Dec 27, 2009 7:33 pm
([msg=32078]see Re: Factorials in C[/msg])

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
Contributor
 
Posts: 516
Joined: Wed Jun 03, 2009 3:55 pm
Location: Riverton, Utah
Blog: View Blog (0)


Re: Factorials in C

Post by Capitan Hook on Sun Dec 27, 2009 7:38 pm
([msg=32079]see Re: Factorials in C[/msg])

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
New User
 
Posts: 17
Joined: Sun Dec 27, 2009 2:20 pm
Blog: View Blog (0)


Re: Factorials in C

Post by faazshift on Sun Dec 27, 2009 7:53 pm
([msg=32081]see Re: Factorials in C[/msg])

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
Contributor
 
Posts: 516
Joined: Wed Jun 03, 2009 3:55 pm
Location: Riverton, Utah
Blog: View Blog (0)


Next

Return to Programming

Who is online

Users browsing this forum: No registered users and 0 guests