5 Replies - 666 Views - Last Post: 22 October 2009 - 11:27 AM Rate Topic: -----

#1 stephanos  Icon User is offline

  • New D.I.C Head

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

Function for factorials

Post icon  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  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 76
  • View blog
  • 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);


Was This Post Helpful? 0
  • +
  • -

#3 stephanos  Icon User is offline

  • New D.I.C Head

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

Re: Function for factorials

Posted 22 October 2009 - 11:15 AM

View Postbrds, 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.
Was This Post Helpful? 0
  • +
  • -

#4 Master Jake  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 16
  • View blog
  • 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.
Was This Post Helpful? 1
  • +
  • -

#5 stephanos  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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 :P but as you correctly said,i'll fix it.....
THANK YOU VERY MUCH!!
Was This Post Helpful? 0
  • +
  • -

#6 Master Jake  Icon User is offline

  • D.I.C Head
  • member icon

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

Re: Function for factorials

Posted 22 October 2009 - 11:27 AM

View Poststephanos, on 22 Oct, 2009 - 10:26 AM, said:

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


No problem, glad I helped.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1