C C++ program to generate natural numbers using recursion

Coding in C

#include<stdio.h>
void display(int);
int main()
{
    int num=1;
    display(num);
    return 0;
}
void display(int x)
{
    if(x<=5)
    {
        printf("%d\n",x);
        display(x+1);
    }
}

Coding in C++

#include<iostream>
using namespace std;
void display(int);
int main()
{
    int num=1;
    display(num);
    return 0;
}
void display(int x)
{
    if(x<=5)
    {
        cout<<x<<endl;
        display(x+1);
    }
}

Output

C C++ program to generate natural numbers using recursion

Published by

admin

I am a computer teacher, programmer and web developer