C

Print 1 To 10 Using Recursion in C

In this tutorial, we are going to see how to print 1 to 10 using recursion in C. In the following example, We check if the number N is not zero in print1To10() function, in that case, we call the same function print1To10() recursively and transfer (N-1) to it. In the else block we write the base condition, that is, return the control back to the calling function if N is 0.
 

Print 1 To 10 Using Recursion in C

This prints the natural numbers from 1 to 10.

#include<stdio.h>

void print1To10(int);

int main()
{
    int N=10;

    printf("\nNumbers from 1 To 10 are: ");
    print1To10(N);

    return 0;
}

void print1To10(int N)
{
    if(N)
        print1To10(N-1);
    else
        return;

    printf("\n%d\n", N);
}

Output:

Numbers from 1 To 10 are: 
1
2
3
4
5
6
7
8
9
10

 

mcqMCQPractice competitive and technical Multiple Choice Questions and Answers (MCQs) with simple and logical explanations to prepare for tests and interviews.Read More

Leave a Reply

Your email address will not be published. Required fields are marked *