How to make a C program restart on recursion?

1

Hello this is a simple program with which I explained the recursion, 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, I was told that with do while I do not know how to accommodate them in this code.

Greetings:)

    
asked by CarlosDayan 19.04.2017 в 06:09
source

1 answer

1

In your case, better a do ... while( ) . This structure guarantees you that you will enter the code at least once , which you can take advantage of to avoid duplicating code.

int main(void) {
  int a=0;

  printf("Ingresa un numero: ");

  do {
    scanf("%d",&a);

    if( a < 3 )
      printf( "Valor no valido. Ha de ser mayor de 3\n\n" );

  } while( a < 3 );

  funcion1(a);

  return 33;
}

As you can see, the comparison is made after of entering it; If the condition is not met , it returns to start the loop.

    
answered by 19.04.2017 / 06:15
source