PDA

View Full Version : Computer programming in factorial.Need help


Rubentheran
Jul 18, 2011, 11:52 AM
Question is in attachment because I can't post the image here.The question is in the attachment.Currently, I don't know how to start because its very difficult.I even studied the textbook and references but I'm still unable to solve it.Can someone help me this out by giving the guide to start.I really desperately need help because next week my exam is coming.So I need solution with working or source code from you guys for this question so that I able to refer it as guide and also able to use this solution to solve other problems .Currently I'm using Dev C++ program as compiler.

slapshot_oi
Jul 18, 2011, 02:29 PM
Are you in college, or high school? This is like every programmers first problem. Did you even try to Google the answer?

I didn't compile and check this, but I'm pretty sure this will work.



#include <stdio.h>

int factorial ( int n);

int main ( int argc, char ** argv) {
int n = 0;
int i = 0;
int product = 0;

/* Scan std input and store the value into the address of n */
scanf ("%d", &n);

printf ( " n Factorial\n");
printf ( " - ---------\n");

product = n;

for ( i = 1; i != n; i ++ {
product *= ( product - i );

printf ( " %d", i );
printf ( " " );
printf ( "%d", product );
printf ( "\n" );
}

return 0;
}

/**
*
* This is the recursive version of the for-loop above. I'm not calling it in
* this program. This is much faster, but I didn't use this because a) I
* was taught only to call printf() in main() only, and 2), I couldn't figure
* out how to use it in this context.
*
*/
int factorial ( int n ) {

printf (" n");

if ( n <= 1)
return 0;

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


EDIT:
And if you have any other questions, go to Stack Overflow (http://http://stackoverflow.com/). The programming boards on this website are largely ignored.