Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8694453
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T00:47:33+00:00 2026-06-13T00:47:33+00:00

Gurus, I want to know how to write a recursive function that prints 1

  • 0

Gurus,

I want to know how to write a recursive function that prints

1
12
123
1234
…
……

For eg: display(4) should print

1
12
123
1234

Code

#include <stdio.h>

void print(int n)
{
        if(n != 0)
        {
                print(n-1);
                printf("\n");
                print(n-1);
                printf("%d",n);
        }
}
int main()
{
        print(3);
}

Output

1

12

1

123

Issues

I wanted to write a pure recursive (without any loop) function but unable to filter unwanted prints.
Hope someone will help me out!!!

Update

Thanks all for the answers.From all the comments which were given it seems like we can write one with only recursion and a loop is required.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T00:47:35+00:00Added an answer on June 13, 2026 at 12:47 am

    To define a recursive function, you have to do three things:

    1. Define what the function does. In this case it is printing numbers from 1 up to n.
    2. Define what the recursive call is. What happens the next time around? The easiest way is to think from the bottom up; in this case, on each earlier line, it is printing numbers up to one less than the previous. Therefore, every time you call the function again, you want to call it with one less than the previous number.
    3. Define your stop condition. When should I stop recursing? In this case, once you hit the number 1, this will be your last iteration. This means, we want to call the recursive function until this stop condition is reached – or in other words, while n is greater than 1.

    Therefore, we end up with the following algorithm:

    function display(n):
        if(n > 1):
            display(n-1);
    
        print 1..n;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Just want to ask the gurus out there about this. I know that CSS
For all you compiler gurus, I wanna write a recursive descent parser and I
Almighty Gurus, Please tell me, I want to know can comparison sm. set of
i am a newbie of html and css.now i want to know other gurus
Greetings code-gurus! I am writing an algorithm that connects, for instance node_A of Region_A
Possible Duplicate: How write a recursive print program I have already asked this question,
LINQ gurus, I am looking for help to write a query... I have a
WCF gurus, you probably already know the answer I cannot find ... let's say
Hi Javascript gurus, I have this Javascript code which is working fine on Firefox
For you database design/performance gurus out there. If you have a database that is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.