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 8684589
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T22:20:44+00:00 2026-06-12T22:20:44+00:00

Possible Duplicate: How write a recursive print program I have already asked this question,

  • 0

Possible Duplicate:
How write a recursive print program

I have already asked this question, but it was closed because of insufficient information.
How write a recursive print program

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

123

Issues

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

  • 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-12T22:20:46+00:00Added an answer on June 12, 2026 at 10:20 pm

    May this be what you want?

    #include <stdio.h>
    
    void print(int n)
    {
        int i;
        if (n > 0)                  // [2]
        {
            //call print recursively
            print(n-1);             // [3]
            for (i=1; i<=n; i++)    // [4]
                printf("%d",i);
            printf("\n");
        }
    }
    int main(int argc, char *argv[])
    {
        // If no argument, default to 8
        int value=8;
        if (argc > 1 )
            value = atoi(argv[1]);
        print(value);                // [1]
        return 0;
    }
    

    Compile it and execute like this:

    display 3
    

    or without any argument (which defaults to display 8):

    display
    

    The trick is to call print(n-1) before printing the output for the current n. This is the program flow when executed as display 3:

    <main() function is running>
    main() calls print(3) in [1]
       <print(3) is running, n is 3>
       since n>0 in [2] print(3) calls print(2) in [3]
          <print(2) is running, n is 2>
          since n>0 print(2) calls print(1)
             <print(1) is running, n is 1>
             since n>0 print(1) calls print(0)  
                <print(0) is running, n is 0>
                since n=0 print(0) doesn't print anything and returns
                now the function that called print(0) (that is, print(1)) takes over
             <print(1) is running, n is 1, continues executing in [4]>
             print (1) enters the for loop, prints "1\n" and returns
             now the function that called print(1) (that is, print(2)) takes over
          <print(2) is running, n is 2, continues executing in [4]>
          print(2) enters the for loop, prints "12\n" and returns
          now the function that called print(2) (that is, print(3)) takes over
       <print(3) is running, n is 3, continues executing in [4]>
       print (3) enters the for loop, prints "123\n" and returns
       now the function that called print(3) (that is, main()) takes over
    <main() is running>
    main executes return(0) and exits
    

    This is what gets printed to the console:

    1
    12
    123
    

    If you move print(n-1) after printf("\n") you will have:

    123
    12
    1
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: How to write program during compiling? I found this problem on a
Possible Duplicate: enhancing a program - complete failure im asked to write a program
Possible Duplicate: In C++ why can't I write a for() loop like this: for(
Possible Duplicate: Allow access permission to write in Program Files of Windows 7 okay,
Possible Duplicate: Read/write .txt file with special characters Im reading a file but I
Possible Duplicate: Do write-only properties have practical applications? A getter without a setter makes
Possible Duplicate: Invalid read/write sometimes creates segmentation fault and sometimes does not I have
Possible Duplicate: Oracle DB: How can I write query ignoring case? I have an
Possible Duplicate: Writing C# Plugin System Is it possible to write a program that
Possible Duplicate: Write a recursive function that reverses the input Recently, I've been reading

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.