Hello, this is a simple program with which recursion was explained to me, this is the code:
#include <stdio.h>
void funcion1(int a)
{
if(a>3)
{
printf("\nNumero %d",a);
funcion1(a-1);
}
}
int main(void) {
int a=0;
printf("Ingresa un numero: ");
scanf("%d",&a);
funcion1(a);
return 33;
}
As you can see the condition is a>3 and what I want to know is how to make it restart if a value of 3 or less is entered, they told me that with do while but I don't know how to accommodate them in this code.
Cheers :)
In your case, better a
do ... while( )
. This structure guarantees that you will enter the code at least once , which you can take advantage of to avoid duplicating code.As you can see, the comparison is made after entering it; if the condition is not met , the loop starts over.