Code:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main()
float fact;
/* what number do we want to calculate the factorial of? */
num = 10;
/* Initialise the factorial */
fact = 1.0;
/* Count from 2 to num, multiply fact by counter each time */
i = 2;
while ( i <= num) {
fact = fact * i;
i = i + 1;
}
/* Print the result */
printf (“The factorial of %d is %f\n”, num, fact);
}
The point of that was to calculate the factorial of 10, using a while loop, which worked fine, but I have this question that says Alter the program so that the counter counts from num down to 1, rather than the other way .
I tried altering the code in the following way but it didn’t work:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main()
float fact;
/* what number do we want to calculate the factorial of? */
num = 10;
/* Initialise the factorial */
fact = 1.0;
/* Count from 10 to num, multiply fact by counter each time */
i = 10;
while ( 0< i <= num) {
fact = fact * i;
i = i - 1;
}
/* Print the result */
printf (“The factorial of %d is %f\n”, num, fact);
}
Which should give me the same answer as the first code written but it doesn’t. Where have I gone wrong?
Thanks!