Thursday, July 9, 2009

How can I use c programming to find the factorial of a number,n?

It should be written in the c programming language. The program should use a while loop. It should prompt the user for the value of n and display its corresponding factorial value.

How can I use c programming to find the factorial of a number,n?
//USING WHILE LOOPS FOR FINDING FACTORIAL IS NOT RECOMMENDED..


//STANDARD METHOD IS BY RECURSION


// THIS PROGRAM FINDS FACTORIAL BY 1)RECURSION 2)WHILE LOOP





#include%26lt;stdio.h%26gt;


long int factorial(int n)


{


if(n==1) return 1;


else return(n*factorial(n-1));


}


void main()


{


int n,i=1;


long int fact=1;


printf("\nEnter a Number:"); scanf("%d",%26amp;n);


printf("\n\nFactorial of %d by recursion algorithm: %ld",n,factorial(n));





//using while loop





while(i%26lt;=n)


{


fact=fact*i;


i++;


}


printf("\n\nFactorial of %d by while loop : %ld",n,fact);





}
Reply:Take a c programming course to find out. It's a lot of work, but you can get a credit for it if you pass.
Reply:#include %26lt;stdio.h%26gt;


#include %26lt;stdlib.h%26gt;


#include %26lt;ctype.h%26gt;








#include %26lt;iostream%26gt;


using namespace std;











int fact(int n)


{





int i;


long int x=1;


for (i=1;i%26lt;=n;i++){


x=x*i;





}


return x; }











int main() {





int a;





while (1) {





printf("\n Enter a number ");


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





printf("\n %10d! = %5ld \n",a,fact(a));





//to wait and see the answer


system("pause");





}


return 0;





}

silk flowers

No comments:

Post a Comment