Reputation: 69
So I'm just starting to learn to code and maybe it's because I don't fully understand the nature of loops.
So what I want the output to be is:
If the inputted value is 6 for example, the program should produce this:
But instead it is printing this:
Without the bullet points obviously. Clearly that second for loop is not printing out properly. I'm not sure as to why. If someone can help me out, it would be greatly appreciated!
EDIT: I really should have specified what I'm looking for. Thank you for all the helpful responses. I realize that all your solutions may be valid but I'm looking for a solution that doesn't involve creating new procedures and functions. For my assignment using additional functions/procedures other than main() is not valid. The problem in my code appears to be that that second for loop is not being run through at all and I have no idea why. The program should take an input value normally but I hardcoded the values into the variables thinking that would make a difference but it isn't. If there's any way to achieve this using just if, for, or while loops would be the optimal way for me.
#include <stdio.h>
int main( void )
{
int number1 = 4;
int number2;
int factorial = 1;
int factorialPrev = 1;
printf("0! = 1\n");
for (int i = 0; i < number1; i++)
{
factorial = factorial * (number1 - i);
}
for (int i = number1 - 1; i <= 0; i--)
{
number2 = number1 - i;
factorialPrev = factorialPrev * number2;
printf("%d! = %d\n", number2, factorialPrev);
}
return 0;
}
Upvotes: 0
Views: 1731
Reputation: 401
You can also solve this with repeated calls to a recursive function, just to get the factorial of a single number. Something like this:
int fact(int num) {
if(num > 1) {
return num * fact(num - 1);
}
else {
return 1;
}
}
So an initial call to fact(5) would go all the way to fact(1) which returns 1. Then control goes back to fact(2) where you have 2 * the number returned from fact(1), which is 1. And so on from there until it gets back to the initial call of fact(5) which should return the final value.
fact(1) = 1
fact(2) = 2 * 1 = 2
fact(3) = 3 * 2 = 6
fact(4) = 4 * 6 = 24
fact(5) = 5 * 24 = 120
Then in main() you'd have a loop that calls the recursive function from num to 0, decrementing. Or if you want you can start from 0 and increment up to num.
for(x = num; x >= 0; --x) {
factNum = fact(x);
printf("%d! = %d\n", x, factNum);
}
Upvotes: 1
Reputation: 68
This should be a right answer :
void showfacto(int max) {
if (max< 0) {
printf("the max cannot be less than 0");
return;
}
printf("0! = 1\n");
int x=1;
for (int i= 1; n <= number; n++) {
{ for(int j=1;j<i)
x=x*j;
}
printf("%d! = %d\n",i, x);
}
if you want to limit the max number , you only need to add a condition if the number is greater than 9 for exemple , return.
Upvotes: 2
Reputation: 27251
Your code is a little... confusing, so I'll just rewrite it. Here's the absolute minimum functionality:
printf("0! = 1\n");
int factorial = 1;
for (int n = 1; n <= number; n++) {
factorial *= n;
printf("%d! = %d\n", n, factorial);
}
Let's wrap this inside a function:
void printFactorials(int number) {
printf("0! = 1\n");
int factorial = 1;
for (int n = 1; n <= number; n++) {
factorial *= n;
printf("%d! = %d\n", n, factorial);
}
}
And now let's add some guards for invalid inputs:
void printFactorials(int number) {
if (number < 0) {
printf("Input cannot be less than 0");
return;
}
if (number > 9) {
printf("Input cannot be greater than 9");
return;
}
printf("0! = 1\n");
int factorial = 1;
for (int n = 1; n <= number; n++) {
factorial *= n;
printf("%d! = %d\n", n, factorial);
}
}
Finally, let's use it:
#include <stdio.h>
int main(void) {
printf("Enter a positive integer between 0 and 9: ");
int number;
if (scanf("%d", &number) == 1)
printFactorials(number);
return 0;
}
Upvotes: 2