2
I have to make a program that calculates the factorial of a number using recursiveness within the main.
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
int main (int argc, char *argv[]){
float numero;
int valorA = atoi(argv[1]);
char var1[10];
itoa(valorA-1, var1, 10);
if (valorA <= 1)
return(1);
else{
numero = valorA * main(argc, var1);
return numero;
}
printf("Fatorial de %d = %.2f", atoi(argv[1]), numero);
}
When I pass the number as argument at the command prompt an error occurs, saying that the program has stopped working. What’s wrong with my code?
Taking advantage, take a look at [tour] just to know the model of the site, and in [Ask] have some tips on how to better elaborate the next questions you ask, to increase the chance of solution. Here you go some tips to format the code if you need to.
– Bacco
Why recursion needs to be done with main?
– eightShirt