I am having problems trying to figure out exactly what is going wrong with my code. I am trying to create a factorial program using an array. I have tried, unsuccessfully, various ways to make it work but I can not figure it out. Any assistance would be appreciated.

#include <stdio.h>

int main(void)
{
	int iFac[10] = {0};
	int iTotal = 0;
	int iCount = 0;
	for (iCount = 0; iCount < 10; iCount++)
			iFac[iCount] = iCount;
			iTotal = (iCount * (iCount - 1)) * iCount;
	for (iCount = 0; iCount <= 10; iCount++)
			printf("\nThe value of the factorial is %d\n", iTotal);
	return 0;
}

Recommended Answers

All 15 Replies

You're storing the factorials in an array, but that doesn't change how you calculate the value. Do you know how to calculate a factorial?

Well, the math formula reads, for example:

5! = 1 x 2 x 3 x 4 x 5 = 120

>Well, the math formula reads, for example:
Actually, it's more like: n! = \displaystyle\prod_{k=1}^nk, but yours is close enough. However, knowing the formula doesn't mean you can write the code to implement it. Can you write a function that when given a value of n, produces the factorial of n? My point is that calculating n! is independent of what you do with the result. If you have a function that gives you n!, you can store it in an array just as easily as printing it:

int fac[10];
int i;

for ( i = 0; i < 10; i++ )
  fac[i] = factorial ( i );

for ( i = 0; i < 10; i++ )
  printf ( "%d\n", fac[i] );

So the factorial equation is what I should enter in the code where you have written the word factorial?

fac = factorial ( i );

I am unsure of how to make the factorial n! into a math equation when using it for code.

>what I should enter in the code where you have written the word factorial?
That's a function. You don't have to write a function, but you do have to loop from 1 to n and store the running product to calculate a factorial. Something like this is a translation of the formula into code:

int prod = 1;
int k = 1;

while ( k <= n )
  prod *= k;

OK, so the reference to factorial (i) is a declared function outside of main correct? In that declared factorial function is the loop you referenced to calculate the factorial? What does the operator *= mean? I am new at C so that's why I keep asking these questions.

>What does the operator *= mean?
It means you should check your C reference. Not many people like to explain things that are easily discovered by looking in a book or searching google. In this case, *= means multiply and assign using the left hand operand as the left operand of the multiplication. It's functionally equivalent to this:

prod = prod * k;
Member Avatar for iamthwee

Draw a flow chart or write some pseudo code on paper first to help you visualise what is going on.

I do understand arrays, somewhat. I created a Fibonacci Sequence array and tried to apply the same thinking to the factorial program but could not figure it out.

>I created a Fibonacci Sequence array and tried to
>apply the same thinking to the factorial program
That's a beautiful approach. Take a problem you've already solved and morph it into something similar that solves your current problem:

#include <stdio.h>

int fibonacci ( int n )
{
  int last = 0;
  int curr = 1;
  int next;

  if ( n < 2 )
    return n;

  while ( --n >= 1 ) {
    next = last + curr;
    last = curr;
    curr = next;
  }

  return next;
}

#define LIMIT 10

int main ( void )
{
  int save[LIMIT];
  int i;

  for ( i = 0; i < LIMIT; i++ )
    save[i] = fibonacci ( i );

  for ( i = 0; i < LIMIT; i++ )
    printf ( "%d ", save[i] );

  printf ( "\n" );

  return 0;
}

Just because you couldn't figure it out doesn't mean that you don't have a promising thought process. I think you'll be a pretty good programmer if you keep at it. :)

Here is my updated code, still working on it.

#include <stdio.h>


int iFactorial(int iCount)
{
	int iProduct;
	int iNumber = 1;
	while (iNumber <= iCount)
			iProduct *= iNumber;
			iNumber++;
	return iProduct;
}

int main(void)
{
	int iFac[10] = {0};
	int iCount = 0;
	for (iCount = 0; iCount < 10; iCount++)
			iFac[iCount] = iFactorial(iCount);
	for (iCount = 0; iCount <= 10; iCount++)
			printf("\nThe value of the factorial is %d\n", iFac[iCount]);
	return 0;
}

>int iProduct;
You're using iProduct without initializing it.

while (iNumber <= iCount)
	iProduct *= iNumber;
	iNumber++;

C++ isn't Python. 99% of the time, whitespace means bupkis, absolutely nothing. If you have a loop or conditional with more than one statement, you must wrap those statements in parentheses:

while (iNumber <= iCount) {
	iProduct *= iNumber;
	iNumber++;
}

That made all the difference! I did have iProduct initialized in my code earlier, I must have changed it to try something else and then not changed it back. Ok, here is the new code, I believe I have it down now.

#include <stdio.h>


int iFactorial(int iCount)
{
	int iProduct = 1;
	int iNumber = 1;
	while (iNumber <= iCount)
	{
			iProduct *= iNumber;
			iNumber++;
	}
	return iProduct;
}

int main(void)
{
	int iFac[10] = {0};
	int iCount = 0;
	for (iCount = 0; iCount < 9; iCount++)
			iFac[iCount] = iFactorial(iCount);
	for (iCount = 0; iCount < 9; iCount++)
			printf("\nThe value of the factorial is %d\n", iFac[iCount]);
	return 0;
}

yes i have seen the thread but fding factorial is such a simple program and you are making it harder to undersatnd. also its not user friendly to coz there are no input or any thing from user

>but fding factorial is such a simple program
Yes, it is.

>and you are making it harder to undersatnd
Who is? It would be nice if you quoted what you're replying to so that we know whom you're addressing.

>also its not user friendly to coz there are no input or any thing from user
Who said it's suppposed to be user friendly? Not all programs interact with a user, you know.

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.