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

  • Home
  • SEARCH
  • 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 6835665
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T23:15:33+00:00 2026-05-26T23:15:33+00:00

I am learning Java using the book Java: The Complete Reference. Currently I am

  • 0

I am learning Java using the book Java: The Complete Reference.
Currently I am working on the topic Recursion.

Please Note: There are similar questions on stackoverflow. I searched them but I didn’t find the solution to my question. I am confused with the logic in the following program.

If I run the below program, it produces the correct output, but I didn’t understand the logic.

  • I didn’t understand the logic in the following line : result = fact(n-1) * n;
  • From my knowledge, If we pass the value of n=4 as shown in the below program,
  • Then, 3 * 4 is stored in the result i.e., 12.
  • Again, fact(n-1) is called. Then n becomes 3.
  • Then the 2 * 3 is stored in the result replacing the previous 12.
  • I think you understood where I am stuck up/confused.

  • Thank you.

class Calculation
{
    int fact(int n)
    {
        int result;

       if(n==1)
         return 1;

       result = fact(n-1) * n;
       return result;
    }
}

public class Factorial
{
     public static void main(String args[])
     {
       Calculation obj_one = new Calculation();

       int a = obj_one.fact(4);
       System.out.println("The factorial of the number is : " + a);
     }
}
  • 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-05-26T23:15:34+00:00Added an answer on May 26, 2026 at 11:15 pm

    result is a local variable of the fact method. So each time the fact method is called, the result is stored in a different variable than the previous fact invocation.

    So when fact is invoked with 3 as argument, you can imagine that its result is

     result3 = fact(2) * 3
     result3 = result2 * 3
     result3 = 1 * 2 * 3
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm currently learning Java, using a combination of the Head First Java book and
I am currently learning Java EE using JBoss and I am kind of stuck
I have been using the Learning Java 2nd Edtion book to try make my
I am learning java, using the book Java how to program. I am solving
I'm learning Java here, and using a GLabel object. It's in the ACM Graphics
Forgive my ignorance - still learning here. I am using Eclipse Ganymede (Java 1.5)
I'm learning Java by reading the online Java book, but im finding it hard
I am currently porting my Android app to a desktop app using Java (Swing,
I've spent the last year learning and using Java in university but we didn't
Learning java server technologies, trying to clarify some things. There are few technologies that

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.