factorial program

Started by
0 comments, last by tom76 22 years, 5 months ago
Here''s my code. How do I make it get the factorial of a? #include int main() /* This is a program which calculates the factorial of a number */ /* >This is what it says: >Write a program which calculates the factorial of a number, it should >1 declare 3 integers: the no we are calculating the factorial of, a >number to hold the answer and a counter for the while loop >2 use a while loop to multiply the answer the correct no of times >3 multiply the answer by the appropriate amount each time round the >while loop >4 print an answer in the format " the factorial of 4 is 24" > > */ { int a,b,c; printf("Input number"); scanf("%d",&a); c = a-1; while (c >= 1) { b = a*c; c--; } printf ("The factoral is %d\n", b ); return 0; } "I envy you, who has seen it all" "And I, young sir, envy you, who have yet to see it for the first time..." - Daniel Glenfield 1st October 2001
if (witisism == !witty) return to_hole
Advertisement
phew! got it! thanks to the guys on general programming! I got in just before you, but thanks!!!



#include

int main()

{
int a,b,c;

printf("Input number");

scanf("%d",&a); /* %d is for an int I think in C. &a assigns that value to a */

c = a-1;

b = 1;
while (c >= 1)

{


b = b*a--;
c--;



}
printf ("The factoral is %d\n", b );
return 0;
}




"I envy you, who has seen it all"
"And I, young sir, envy you, who have yet to see it for the first time..."
- Daniel Glenfield
1st October 2001
if (witisism == !witty) return to_hole

This topic is closed to new replies.

Advertisement