Thread: Easy Question
View Single Post
Old 2009-10-01, 13:03   #4
R.D. Silverman
 
R.D. Silverman's Avatar
 
Nov 2003

26·113 Posts
Default

Quote:
Originally Posted by Unregistered View Post
I'm a complete noob when it comes to programming in c, but you have to start somewhere. I ran into a question about factorials. Can you use "n!"? It didnt work for me, I instead went about it the long way,

# include <stdio.h>

int main (void)
{

int n, n1 = 1, n2 = 2, n3 = 3, n4 = 4, n5 = 5, n6 = 6, n7 = 7, n8 = 8, n9 = 9, n10 = 10, nResult;

printf("-n- -n!\n\n");


printf( "%i %2i\n" , n1 , nResult = n1);
printf( "%i %2i\n", n2, nResult = n1 * n2);
printf( "%i %2i\n", n3, nResult = n1 * n2 * n3);
printf( "%i %2i\n", n4, nResult = n1 * n2 * n3 * n4);
printf( "%i %2i\n", n5, nResult = n1 * n2 * n3 *n4 * n5);
and so on....
}


Whats an easier way?
Let me start by asking: Are you a novice programmer in general?

You do realize that the multiplication of two integers can overflow?

Do you know how to code recursive functions in any language (i.e. not
specifically C?). If not, may I assume that you at least know how to
code a simple loop?

Do you have any understanding of multi-precision arithmetic?

Aside from the issue of the choice of programming language, may
I suggest that you read about coding arithmetic algorithms? Start
with Knuth "The Art of Computer Programming", vol 2.

There are many issues involved in coding n! that are separate from the
choice of coding language.
R.D. Silverman is offline   Reply With Quote