Recursion to return numbers from 0 to n

6

I'm trying to do a recursive function that returns the n numbers from 0 to n , my code looks like this:

#include <stdio.h>

int imprimenumeros(int n){

    if (n==1)
        return 1;
    else 
        return  imprimenumeros(n-1);
}

void main (){
    int n;
    int y;
    printf("digite um numero\n");
    scanf("%d", &n);
    printf("%d\n", imprimenumeros (n) );
}

But it is not working ... where is the error?

    
asked by anonymous 28.10.2014 / 23:04

3 answers

9

A small correction, you want it to return from N to 0 and not from 0 to N, since you are decrementing recursively, if you really want it to go from 0 to N I change the code.

Another thing you forgot is that you are not printing the numbers, you are only giving return, ie it performs the entire calculation but does not print.

Another observation is that if you want to start from 0 then you have to change 1 by zero in your first if:

#include<stdio.h>
    int imprimenumeros(int n){
        printf("%d\n", n );
        if (n==0){
            return 0;
        }
        else {
            return  imprimenumeros(n-1);
        }
    } 
void main (){ 
    int n; 
    int y; 
    printf("digite um numero\n"); 
    scanf("%d", &n); 
    imprimenumeros (n);

} 

Now if you really want to incremental from 0 to a given N, simply pass your initial variable "in this case is 0" along with your terminal variable "which in this case is N" and increase recursively: / p>

#include<stdio.h>
    int imprimenumeros(int n, int y){

        printf("%d\n", y);
        if (n>y){
            return imprimenumeros(n, y+1);
        }
        else {
            return 0 ;
        }
    } 
void main (){ 
    int n; 
    int y = 0; 
    printf("digite um numero\n"); 
    scanf("%d", &n); 
    imprimenumeros (n, y);
    return 0;
} 
    
28.10.2014 / 23:15
8

You say return the numbers from 0 to N, but your function calls imprimenumeros , so I'm assuming that for you just print on the screen , Am I right? (otherwise it would be necessary to store the list of numbers in an array or similar, which is a bit trickier ...) After all, a function that returns a int returns only a single value, to return several would require one another data structure.

One way to do this differently from the other answers is by first doing the recursive call and after by printing the number:

#include <stdio.h>

void imprimenumeros(int n){
    if (n > 0)
        imprimenumeros(n-1);
    printf("%d\n", n);
}

void main (){
    int n;
    int y;
    printf("digite um numero\n");
    scanf("%d", &n);
    imprimenumeros (n);
}

So, when it starts printing n it will already print n-1 (in the recursive call), so the print order will grow from 0 to N.

    
29.10.2014 / 00:09
7

If you want to print the numbers, the print function can return void . What you do now is to do printf at the beginning of the function, see if n is zero,

1) If it is not called the same function (recursively),

2) if it is then return empty (hence the return;).

At the end of the main method vc returns 0, to indicate to the operating system that the application has finished correctly.

#include <stdio.h>

void imprimenumeros(int n){
    printf("%d\n", n);
    if (n==0) {
        return;
    } else {
        imprimenumeros(n-1);
    }
}

int main (){
    int n;
    int y;
    printf("digite um numero\n");
    scanf("%d", &n);
    imprimenumeros(n);
    return 0;
}

I leave the same problem solved here using the iteration method:

#include <stdio.h>

void imprimenumeros(int n){
    while(n >= 0) {
        printf("%d\n", n);
        n = n - 1; // ou n--;
    }
    return;
}

int main (){
    int n;
    int y;
    printf("digite um numero\n");
    scanf("%d", &n);
    imprimenumeros(n);
    return 0;
}
    
28.10.2014 / 23:13