2
I’m learning recursion in C, and I was able to do some programming using recursion. However, I am getting to make a simple program that the user sending two numbers, for example: 6x2, I multiply them in recursion. I can even show the code below, but I’m sure it totally escapes the reality of the real code.
Code:
#include <stdio.h>
int poligono(int n,int v){
int res=0;
if(v==0)
return 0;
else if(n==0)
return n;
else
return n + poligono(n,v);
}
int main(){
int n,v,res=0;
scanf("%d%d",&n,&v);
res=poligono(n,v);
printf("Res = %d\n",res);
return 0;
}
how to use main as it should be used to see if compiles? Use: int main() { /your code.../ Return 0; }
– Bruno Bermann
To figure out what’s wrong, take any two numbers and make the hand recursion on a piece of paper. You’ll see your problem.
– Jorge B.
Besides, your code generates infinite recursion, so your code generates an error called (I think you’ve heard of it, rsrs) Stack Overflow. Because it exceeds the limit of "stacked" calls in the stack (stack).
– Bruno Bermann
Bruno, I apologize, because the code that did not compile was another. This compiles. Soon I will edit my question.
– Fiodor
@Fiodor does what I told you, takes 2 numbers and runs the function recursively, but keep writing on paper and you’ll understand where your problem is.
– Jorge B.
I’ll take your advice, @Jorgeb.
– Fiodor
@Fiodor I reversed his question, so that the answers I already had are not invalidated. I warned Math also to edit his answer.
– Jorge B.