factorial....God what is the code??

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • cess
    New Member
    • Feb 2007
    • 19

    factorial....God what is the code??

    i think this is not right!!!!

    Code:
    import java.io.*;
     public class factorial{
      public static void main (String args[])throws Exception
     {
      String number;
      int numberInteger;
      int n;
      
          System.out.print("Input number:");
    	  InputStreamReader no= new InputStreamReader(System.in);
          BufferedReader num=new BufferedReader(no);
          number=num.readLine();
          numberInteger=Integer.parseInt(number);
         {
                  
      {
                  
        while (n!=0)
        n=numberInteger;
        {
        System.out.println("Result:"+n);
        System.out.println("Factorial:"+(n));
           
         } 
     
          }
    }
    }
    }
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Originally posted by cess
    i think this is not right!!!!

    Code:
    import java.io.*;
    public class factorial{
    public static void main (String args[])throws Exception
    {
    String number;
    int numberInteger;
    int n;
     
    System.out.print("Input number:");
    	 InputStreamReader no= new InputStreamReader(System.in);
    BufferedReader num=new BufferedReader(no);
    number=num.readLine();
    numberInteger=Integer.parseInt(number);
    {
     
    {
     
    while (n!=0)
    n=numberInteger;
    {
    System.out.println("Result:"+n);
    System.out.println("Factorial:"+(n));
     
    } 
     
    }
    }
    }
    }
    You should know the formula for factorial. What is it?

    Comment

    • cess
      New Member
      • Feb 2007
      • 19

      #3
      Originally posted by r035198x
      You should know the formula for factorial. What is it?
      i think this is: if the user inputs 5, the result is: 5*4*3*2*1 and the product of that which is 120

      Comment

      • r035198x
        MVP
        • Sep 2006
        • 13225

        #4
        Originally posted by cess
        i think this is: if the user inputs 5, the result is: 5*4*3*2*1 and the product of that which is 120
        Correct. Now generalize it.

        Given any integer n, what is the factorial of n in terms of n only?

        Comment

        • hirak1984
          Contributor
          • Jan 2007
          • 316

          #5
          I hate giving codes.Next time you should come up with posts after you have gone through the topic,yourself well.

          Code:
           public class Factorial {
           
          /**
          * @param args
          */
          public static void main(String[] args) {
          // TODO Auto-generated method stub
          int i=3,j,fact=0;
          fact=Fact(i);
          System.out.println(fact);
          }
          static int Fact(int j)
          {
          if(j==1)
          return j;
          else
          return (j*Fact(j-1));
          }
          }

          Comment

          • r035198x
            MVP
            • Sep 2006
            • 13225

            #6
            Originally posted by hirak1984
            I hate giving codes.Next time you should come up with posts after you have gone through the topic,yourself well.

            Code:
             public class Factorial {
             
            /**
            * @param args
            */
            public static void main(String[] args) {
            // TODO Auto-generated method stub
            int i=3,j,fact=0;
            fact=Fact(i);
            System.out.println(fact);
            }
            static int Fact(int j)
            {
            if(j==1)
            return j;
            else
            return (j*Fact(j-1));
            }
            }
            Ah hirak, you just spoiled it. I think the OP would have learnt more if they'd gone on to figure the formula and use it themselves.

            Comment

            • cess
              New Member
              • Feb 2007
              • 19

              #7
              Originally posted by r035198x
              Ah hirak, you just spoiled it. I think the OP would have learnt more if they'd gone on to figure the formula and use it themselves.
              Yah your right!!! tnx for the support!!!!

              Comment

              • cess
                New Member
                • Feb 2007
                • 19

                #8
                Originally posted by hirak1984
                I hate giving codes.Next time you should come up with posts after you have gone through the topic,yourself well.

                Code:
                 public class Factorial {
                 
                /**
                * @param args
                */
                public static void main(String[] args) {
                // TODO Auto-generated method stub
                int i=3,j,fact=0;
                fact=Fact(i);
                System.out.println(fact);
                }
                static int Fact(int j)
                {
                if(j==1)
                return j;
                else
                return (j*Fact(j-1));
                }
                }
                I'M sorry guy!!!! If you hate giving codes, then DO'NT give anymore... In fact, your given CODE doesn't work...

                Comment

                • Ganon11
                  Recognized Expert Specialist
                  • Oct 2006
                  • 3651

                  #9
                  it should probably be

                  fact = Factorial.Fact( i);

                  instead of

                  fact = Fact(i);

                  Fact(int) is a static method and, therefore, must be accessed using the class name.

                  Comment

                  • hirak1984
                    Contributor
                    • Jan 2007
                    • 316

                    #10
                    It is not Factorial.Fact( i) because both main() and fact() method being in the same class we can call it directly.(At least in my eclipse)
                    If this code doesnot work in your environment,ple ase post the errors .
                    It worked fine in my eclipse 3.1
                    may be you guys use bitta advanced version!!!!!!!! !!!!!!!!!!!
                    Originally posted by Ganon11
                    it should probably be

                    fact = Factorial.Fact( i);

                    instead of

                    fact = Fact(i);

                    Fact(int) is a static method and, therefore, must be accessed using the class name.

                    Comment

                    • r035198x
                      MVP
                      • Sep 2006
                      • 13225

                      #11
                      Originally posted by cess
                      I'M sorry guy!!!! If you hate giving codes, then DO'NT give anymore... In fact, your given CODE doesn't work...
                      When you say the code does not work, please give the error message or exception trace. don't just say it doesn't work.

                      And we don't hate giving the codes. We hate doing students' assignments where the students themselves are not willing to put in any work themselves.

                      Comment

                      • r035198x
                        MVP
                        • Sep 2006
                        • 13225

                        #12
                        Originally posted by hirak1984
                        It is not Factorial.Fact( i) because both main() and fact() method being in the same class we can call it directly.(At least in my eclipse)
                        If this code doesnot work in your environment,ple ase post the errors .
                        It worked fine in my eclipse 3.1
                        may be you guys use bitta advanced version!!!!!!!! !!!!!!!!!!!
                        The reason why you can call Fact inside main using only the method name is not only because they are in the same class in this case. It is because Fact is declared as static. Since both main and Fact are static and are in the same class, you can call one from the other without needing an object of the class' type.

                        Comment

                        • hirak1984
                          Contributor
                          • Jan 2007
                          • 316

                          #13
                          ya thats a very nice explanation.tha nks
                          Originally posted by r035198x
                          The reason why you can call Fact inside main using only the method name is not only because they are in the same class in this case. It is because Fact is declared as static. Since both main and Fact are static and are in the same class, you can call one from the other without needing an object of the class' type.

                          Comment

                          • cess
                            New Member
                            • Feb 2007
                            • 19

                            #14
                            Originally posted by r035198x
                            When you say the code does not work, please give the error message or exception trace. don't just say it doesn't work.

                            And we don't hate giving the codes. We hate doing students' assignments where the students themselves are not willing to put in any work themselves.
                            I'm sorry... I am just down of what h**** is saying...peace! !!

                            Comment

                            • r035198x
                              MVP
                              • Sep 2006
                              • 13225

                              #15
                              Originally posted by cess
                              I'm sorry... I am just down of what h**** is saying...peace! !!
                              Did you get it to work then?

                              Comment

                              Working...