Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Java
  4. What is wrong with this java code, why is it not printing Factorial?

What is wrong with this java code, why is it not printing Factorial?

Scheduled Pinned Locked Moved Java
questionjava
5 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • U Offline
    U Offline
    User 13592267
    wrote on last edited by
    #1

    class facto
    {
    public static void main(String args[])
    {
    System.out.println(dust(5));
    }

    static int dust(int z)
    {
    	if (z==1||z==2)
    	{
    		return 2;
    	}
    
    	else
    	{	
    		return dust(z-1) \* dust(z-2);	
    	} 
    
    }
    

    }

    L V 2 Replies Last reply
    0
    • U User 13592267

      class facto
      {
      public static void main(String args[])
      {
      System.out.println(dust(5));
      }

      static int dust(int z)
      {
      	if (z==1||z==2)
      	{
      		return 2;
      	}
      
      	else
      	{	
      		return dust(z-1) \* dust(z-2);	
      	} 
      
      }
      

      }

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      Firstly, you never print any of the interim values, and secondly, your calculations are wrong. See Fibonacci numbers[^]

      U 1 Reply Last reply
      0
      • L Lost User

        Firstly, you never print any of the interim values, and secondly, your calculations are wrong. See Fibonacci numbers[^]

        U Offline
        U Offline
        User 13592267
        wrote on last edited by
        #3

        I wanted to print Factorial. So, I am sorry for the mistake in the question.

        Richard DeemingR 1 Reply Last reply
        0
        • U User 13592267

          I wanted to print Factorial. So, I am sorry for the mistake in the question.

          Richard DeemingR Online
          Richard DeemingR Online
          Richard Deeming
          wrote on last edited by
          #4

          Your calculation is still wrong. f(1) === 1, but your code returns 2. And f(n) === n * f(n-1), but your code is returning f(n-1) * f(n-2). Also, unless this is a homework question to test your ability to write recursive functions, it's much more efficient to use a loop to calculate factorials or the Fibonacci sequence.


          "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

          "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

          1 Reply Last reply
          0
          • U User 13592267

            class facto
            {
            public static void main(String args[])
            {
            System.out.println(dust(5));
            }

            static int dust(int z)
            {
            	if (z==1||z==2)
            	{
            		return 2;
            	}
            
            	else
            	{	
            		return dust(z-1) \* dust(z-2);	
            	} 
            
            }
            

            }

            V Offline
            V Offline
            vishaljamdagni
            wrote on last edited by
            #5

            Maybe you shouldn't give the factorial of 1 the value 2 as it is returning in your first "if" construct. Now I am not expert but I think this should work:

            public class facto{
            public static void main(String[] args){
            System.out.println(dust(5));
            }

            static int dust(int n)
            {
            int output;
            if(n==1){
            return 1;
            }

               output = dust(n-1)\* n;
               return output;
            

            }
            }

            I suppose that in your version the recursion is not able to complete properly because of "maybe" your trying to both return the value and calculate it at the same time. Hope the code snippet helps, I am just a beginner myself so apologies if I didn't get it right. P.S I know I haven't considered the condition of the input being 0.

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups