Recursion to return numbers from 0 to n

Asked

Viewed 10,734 times

5

I’m trying to make a recursive function that returns the n numbers of 0 until n, my code went 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’s not working... where is the error?

  • 4

    first puts the code, then selects it all and presses Ctrl+k that it formats for you, at first I would curl up to put code here but this way is the simplest.

  • Thanks for the tip!

  • I’m already writing the answer for you, it’s very simple, you missed one small detail

  • I didn’t understand, because when I type 1 the result is 0 and 1, when I type 2 the result is 0, 1 and 2. The exercise asked you to tell me if the number is prime or not.

3 answers

9

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

Another thing you forgot is that you’re not printing the numbers, you’re just giving Return, IE, it performs all the calculation but does not print.

Another observation is that if you want to start from 0 then you have to change 1 for 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 increment from 0 to a given N, just pass your initial variable "in this case is 0" along with your terminal variable "which in this case is N" and increment recursively:

#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;
} 
  • Got it! Brigade! But what if it is the opposite of 0 until n?

  • Any other question in the implementation is just ask :)

  • then it’s easier, it’s just you save the n and do ncrementando from 0 until n

  • @Lucy put the code in case you want the other option, which is to increment rather than decrement and gave a quick explanation of the logic tbm :)

  • I didn’t understand, because when I type 1 the result is 0 and 1, when I type 2 the result is 0, 1 and 2. The exercise asked you to tell me if the number is prime or not.

8

You speak in return the numbers from 0 to N, but its function calls imprimenumeros, So I’m assuming that’s enough for you print on screen the numbers of this function, am I right? (otherwise it would be necessary to store the list of numbers in an array or similar, which is a little more complicated...) After all, a function that returns a int only returns a single value, to return several would need another data structure.

One way to do this differently from other answers is to first make the recursive call and afterward 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 he goes to print the n he will have printed the n-1 (in recursive call), then the order of impressions will be increasing from 0 to N.

  • I didn’t understand, because when I type 1 the result is 0 and 1, when I type 2 the result is 0, 1 and 2. The exercise asked you to tell me if the number is prime or not.

7

If you want to print the numbers, the printable function can return void. What you do now is make 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 returns 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 here the same problem solved using the method of iteration:

#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;
}
  • Brigade! But I didn’t see where I went wrong :/

  • If you want to print the numbers, the printable function can return void. What you do now is to printf at the beginning of the function, see if n is zero, if it is not called the same function (recursively), if it is then returns 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. I edited the above answer.

  • Then a while(n != 0) could be considered a type of recursion?

  • 3

    No. Recursion is when you define a function at your own expense. Note that the imprimenumeros function calls itself until it returns. That is, printable(3) -flame-> printable(2) -flame-printer(1) -flame-printer(0). while(condition) is considered iteration, where vc does not call any function more than once, it soon solves your problem by using cycles (as while or for) until you reach the end of the problem. I will edit the above code to give you the iteration example :)

  • I didn’t understand, because when I type 1 the result is 0 and 1, when I type 2 the result is 0, 1 and 2. The exercise asked you to tell me if the number is prime or not.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.