# Function for factorials

Page 1 of 1

## 5 Replies - 784 Views - Last Post: 22 October 2009 - 11:27 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=133727&amp;s=c094f303192a9ab99d216e49fe77dc69&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 stephanos

Reputation: 0
• Posts: 23
• Joined: 05-January 09

# Function for factorials

Posted 22 October 2009 - 11:06 AM

```#include <stdio.h>

float factorial = 1;
while( int i = 2; i <= number; i++ );
{
factorial *= i;
}
int main(){

int number;

printf( "Give the number: ");
scanf("%d",& number);

printf("Factorial of %d  is %f \n",number,factorial);

return 0;
}

```

When i compile with lets say : gcc exercise2.c -o exercise2 -lm

Quote

it shows this errors :
exersise2.c: In function ‘factorial’:
exersise2.c:12: error: ‘for’ loop initial declaration used outside C99 mode
exersise2.c: In function ‘main’:
exersise2.c:24: error: expected ‘;’ before ‘printf’
exersise2.c: In function ‘factorial’:
exersise2.c:28: error: expected declaration or statement at end of input
exersise2.c:28: error: expected declaration or statement at end of input

Please i need help to find fixing the errors...

Is This A Good Question/Topic? 0

## Replies To: Function for factorials

### #2 brds

Reputation: 76
• Posts: 515
• Joined: 22-October 08

## Re: Function for factorials

Posted 22 October 2009 - 11:11 AM

How about you move that code back into main, and remove the ; from the end of your while statement?

```while(whatever) {
/* Code */
}

do {
/* Code */
} while (whatever);

```

### #3 stephanos

Reputation: 0
• Posts: 23
• Joined: 05-January 09

## Re: Function for factorials

Posted 22 October 2009 - 11:15 AM

brds, on 22 Oct, 2009 - 10:11 AM, said:

How about you move that code back into main, and remove the ; from the end of your while statement?

```while(whatever) {
/* Code */
}

do {
/* Code */
} while (whatever);

```

brds thank you for the help but i need the factorial function to be outside the main,and then call it.

### #4 Master Jake

Reputation: 16
• Posts: 106
• Joined: 27-February 09

## Re: Function for factorials

Posted 22 October 2009 - 11:22 AM

I don't see any function in that code besides main. You need to create one and say pass in some values. Here's an example:

```#include <stdio.h>

// Function Declaration
float getFactorial(float number);

float getFactorial(float number)
{
float factorial = number;
int i;

for (i = 2; i <= number; i++ );
{
factorial *= i;
}

return factorial;
}

int main()
{
int number;

printf( "Give the number: ");
scanf("%d", &number);

printf("Factorial of %d is %f \n", number, getFactorial((float) number));

return 0;
}

```

This is hypothetical, of course. I actually didn't pay attention to the math you were doing so it's your job to make sure it's right. I just gave an example of how it could be setup.

### #5 stephanos

Reputation: 0
• Posts: 23
• Joined: 05-January 09

## Re: Function for factorials

Posted 22 October 2009 - 11:26 AM

Problem solved thanks to you,of course the math is wrong but as you correctly said,i'll fix it.....
THANK YOU VERY MUCH!!

### #6 Master Jake

Reputation: 16
• Posts: 106
• Joined: 27-February 09

## Re: Function for factorials

Posted 22 October 2009 - 11:27 AM

stephanos, on 22 Oct, 2009 - 10:26 AM, said:

Problem solved thanks to you,of course the math is wrong but as you correctly said,i'll fix it.....
THANK YOU VERY MUCH!!