factorial program
can somebody help me? I''m trying to get the factor of the number inputted but I don''t know how to do it!
#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
it looks like you are re-assigning b every time through the while-loop... thus you are not actually storing the entire factorial calculated so far...
first off, you should set "b" to equal "a" before the loop...
then the line "b=a*c" should be "b=b*c" so it multiplies the result so far by the next "c"...
with your program:
b = 4 * 3 = 12
b = 4 * 2 = 8
b = 4 * 1 = 4
so, b would be "4" at the end of the loop...
with the adjustments i gave you:
b = 4 (before the loop starts)
with b = b*c:
b = 4 * 3 = 12
b = 12 * 2 = 24
b = 24 * 1 = 24
which would output "24", the correct factorial.
--- krez (krezisback@aol.com)
first off, you should set "b" to equal "a" before the loop...
then the line "b=a*c" should be "b=b*c" so it multiplies the result so far by the next "c"...
with your program:
b = 4 * 3 = 12
b = 4 * 2 = 8
b = 4 * 1 = 4
so, b would be "4" at the end of the loop...
with the adjustments i gave you:
b = 4 (before the loop starts)
with b = b*c:
b = 4 * 3 = 12
b = 12 * 2 = 24
b = 24 * 1 = 24
which would output "24", the correct factorial.
--- krez (krezisback@aol.com)
Thank you. I got in just before you though, here''s what I did.
#include
int main()
{
int a,b,c;
printf("Input number");
scanf("%d",&a);
c = a-1;
b = a*c;
c--;
while (c >= 1)
{
b = b*c--;
}
printf ("The factoral of %d is %d\n", a, b );
return 0;
}
Thank you for your speed in responding, much appreciated
"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
#include
int main()
{
int a,b,c;
printf("Input number");
scanf("%d",&a);
c = a-1;
b = a*c;
c--;
while (c >= 1)
{
b = b*c--;
}
printf ("The factoral of %d is %d\n", a, b );
return 0;
}
Thank you for your speed in responding, much appreciated
"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
WHOOPS for 1 it gives 0 - not good! Here''s the fix:
#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
#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
int factorial(int n){ int f = n, r = 1; while(f > 1) r *= f--; return r;}
Two differences:
1.) You really don''t need a in your loop; assign a to c and decrement it as well (like f above).
2.) You don''t need the >= comparison. > works fine, since n * 1 = n for all n.
To you it''s a Bently, to me it''s a blue car...
"Diddy"
P.Diddy
quote:Original post by Oluseyi
1.) You really don''t need a in your loop; assign a to c and decrement it as well (like f above).
you would need "a" to store the original input for the end of the program where it outputs: "the factorial of ''a'' is ''whatever''"
but you are right with the second one.
--- krez (krezisback@aol.com)
I only meant within the factorial loop. If I used the function above, I''d write:
The value of a is assigned to c (the parameter n is not manipulated other than copying to f).
To you it''s a Bently, to me it''s a blue car...
"Diddy"
P.Diddy
...cout << "The factorial of " << a << " is " << factorial(a) << endl;
The value of a is assigned to c (the parameter n is not manipulated other than copying to f).
To you it''s a Bently, to me it''s a blue car...
"Diddy"
P.Diddy
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement