Basically recursion is another way of performing loops with sometimes complex attributes to it. Since not all programming languages have the same syntax and logical sequencing and identifiers and not least keywords.
Recursion is used in programming languages where you can't use for loops and so on.
Recursion simply solves the loop problem in these languages.
In using recursion you first need to identify the base case as this will be your break in essence of stopping the recursion when it has achieved its purpose.
The recursive case is where you simply divide the more complex arguments for the repetitive section of code itself and this part needs to to do some critical thinking in solving such problems.
#include <stdio.h>
#include <conio.h>
int recursive(int,int); //Function Prototype and parameters
int main()
{
int num,num2,ans;
printf("Please enter the number:");
scanf("%d",&num);
printf("Please enter the multiplier:");
scanf("%d",&num2);
ans=recursive(num,num2);
printf("The answer is:%d",ans);
getch();
return 0;
}
int recursive(int a,int b)
{
if(b==0)//base case for recursion to stop
return 0;
else
return a + recursive(a,b-1);//recursive case
}


Sign In
Create Account


Back to top









