1
I am stubbing the book 6th edition of C as programming of the Deitel brothers and I am doubtful in an exercise, the exercise is the 4.14 of chapter 4 which is about factorization, it asks a program that the user type a number and that number is factored, for example if the user type the number 5, the program will do 5*4*3*2*1 resulting in 120. If anyone can give me some tip and not reply I’ll be very grateful thank you:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int i, num, aux1=0, fat=0, aux=0;
printf("digite um numero para ser fatorado:\n");
scanf("%i", &num);
printf("\nvamos fatorar entao o numero %i:\n\n", num);
for(i=num;i>=1;i--)
{
aux=i;
fat=aux*i;
printf("%i! ", fat);
}
return 0;
}
Run your algorithm on paper and see what you’re doing wrong.
– Beterraba
1a. tip: the way it’s being used, the variable
aux
is not doing any good. But you don’t even need it. The algorithm may be simpler than this. In the end, there will have to be an answer.– Maniero
A quick comment. In mathematics the term "factoring" is used to decompose an integer into prime numbers. For example, 12 = 322. What you are asking for is the factorial.
– hugomg
Here’s what @Beet says is the best way to help you with small algorithms.
– Jorge B.