Monday, May 24, 2010

I want to design a program using C, 'the factorial of a +ve number n! is defined as n!=n(n-1)(n-2)...x 1 .

there are no factorials of -ve numbers. design a C program which ask the user to enter a number n and gives the factorial of n. kindly help.

I want to design a program using C, 'the factorial of a +ve number n! is defined as n!=n(n-1)(n-2)...x 1 .
int factorial(int n)


{


int tempAnswer = n;





for(int i=n; i%26gt;0;i--)


{


tempAnswer = tempAnswer*i;


}





return tempAnswer;


}





//Do some printing stuff somewhere else.





int main()


{


int input;


//cin something into input


cout %26lt;%26lt; "The factorial is " %26lt;%26lt; factorial(input) %26lt;%26lt; endl;


return 0;


}
Reply:Did you mean "I want YOU to design a program"?


Here you go, don't forget to add comments.


// factorial by %26lt;insert your name here%26gt;


#include "stdio.h"





int fact(int n)


{


return n?n*fact(n-1):1;


};





int main(int argc, char* argv[])


{


int val;


printf("Factorial! pls enter number\n");


scanf("%d",%26amp;val);


val=fact(val);


printf("Factorial is %d\n",val);


scanf("%d",%26amp;val);


return 0;


}
Reply:int temp = yourFractorialNumber;





for (int num=temp; num %26gt; 1; num--)


{


temp = temp*(num-1);


}





int answer = temp;

garden state

No comments:

Post a Comment