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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T19:19:08+00:00 2026-06-09T19:19:08+00:00

I am new to the C programming language and I am trying to learn

  • 0

I am new to the C programming language and I am trying to learn recursion for computing the factorial of a given number. My question is the debugging printf statement is printing 2,6,24,120 if I input ‘5’. How does it print 4 times if the function calls are replaced with the corresponding values and computes the factorial at a time?

#include<stdio.h>
#include<stdlib.h>

int factorial(int n);

int main()
{
        int num;
        int fact_val;
        printf("Enter the number for which you are going to compute the factorial:");
        scanf("%d",&num);

        fact_val=factorial(num);

        printf("The factorial of the given number is %d\n",fact_val);

        return 0;
}

int factorial(int n)
{
        int factorial_val;
        if(n==1)
        return 1;
        else
        {
                factorial_val=factorial(n-1)*n;
                printf("Debugger-%d\n",factorial_val);
        }
        return factorial_val;
}
  • 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-09T19:19:09+00:00Added an answer on June 9, 2026 at 7:19 pm

    When you reach your base-case, you return immediately rather than printing.

    So you see a printf for cases: 5, 4, 3, 2, and when the function is passed 1, the value isn’t printed: you return instead.

    Furthermore you recurse before you print, so the cases are printed in order, least-first: the first print happens only after you’ve recursed all the way down to 2. Hence you see: 2, 6, 24, 120. Only when you’ve returned from the current recursion is the intermediate value printed.

    Write down the recursion to make it clearer:

    5 -> recurse with 4:
        4 -> recurse with 3:
            3 -> recurse with 2:
                2 -> recurse with 1:
                    1 -> base case, just return...
                printf (1 * 2) = 2;
            printf (2 * 3) = 6;
        printf (6 * 4) = 24;
    printf (24 * 5) = 120;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i am enthusiast and new in programming trying this simple c language code and
I always believed that when starting to learn a new programming language programmer must
I'm quite new to Scala programming language, and was trying something out stucked in
I am trying to learn my first programming language. Unfortunately I picked the Iphone
I am new to the C programming language and trying to improve by solving
Im new to the android programming language and am trying to get an android
i been thinking of a new programming language. Before trying to implement it i
I'm trying to learn C by reading C Programming Language, 2nd Edition . I
I'm trying to get Lua to work with the new programming language D. Everything
What features could be added to a new programming language to make it more

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.