Results 1 to 6 of 6

Thread: How to write java program to find factorial of number?

  1. #1
    Join Date
    May 2009
    Posts
    837

    How to write java program to find factorial of number?

    Hello,
    I am T.Y.B.Sc. student. As per our syllabus we have java programming language. I recently started learning java language. In our last tutorial our sir has said write a java program to find factorial of number. I tried various methods but unable to write that program. Please if you have any idea about this program share with me.
    Thanks in advanced.

  2. #2
    Join Date
    May 2008
    Posts
    2,297

    Re: How to write java program to find factorial of number?

    Code:
    public class FactorialNumber {
    
    public static void main(String[] args) {
    
    int N = 5;
    
    int F = N;
    
    for(int p =(N - 1); p > 1; p--)
    
    {
    
    F = F * p;
    
    }
    
    System.out.println("Factorial of a number is " + F);
    
    }
    
    }

  3. #3
    Join Date
    Oct 2005
    Posts
    2,393

    Re: How to write java program to find factorial of number?

    Don't just copy paste, tried to understand each step.

    Code:
    import java.io.*; 
    class FactorialNumber
    {
      public static void main(String[] args) {
          try{
            BufferedReader obj1 = new BufferedReader(new InputStreamReader(System.in));
            System.out.println("Enter the number");
            int A= Integer.parseInt(object.readLine());
            int F= 1;
            System.out.println("Factorial of " +A+ ":");
            for (int P= 1; P<=A;P++){
                  F=F*P;
            }
            System.out.println(F);
        }
        catch (Exception e){}
      }
    }

  4. #4
    Join Date
    May 2008
    Posts
    2,389

    Re: How to write java program to find factorial of number?

    Here I use recursion method.

    Code:
    public class RFactorial
    
    {
      public static void main( String [] args )
      {
        
        System.out.println( "Factorial ( 7 ) is "
                          + factorial( 7 ) );
      }
     
      public static int factorial( int p )
      {
        if ( p <= 0 )  
         return 1;
        else    
         return ( p * factorial ( p - 1 ) );
      }
    }

  5. #5
    Join Date
    Feb 2008
    Posts
    1,852

    Re: How to write java program to find factorial of number?

    You can use iterative method to calculate factorial using following code.


    Code:
    int FactorialOfNumber = 1;
    for (int p = 1; p<= x; p++)
    FactorialOfNumber *= p;

  6. #6
    Join Date
    Jan 2008
    Posts
    1,521

    Re: How to write java program to find factorial of number?

    Code:
       1.
          import java.util.*;
    
          public class factorialNumber
          {
    
          public static void main(String[]args)
    
          {
          int x,F,P;
    
          System.out.println("Factorial ");
    
          System.out.println("Enter the number");
    
          Scanner sc = new Scanner(System.in);
    
          P = sc.nextInt();
    
          x = 1;
    
          F = 1;
    
          F= F * x;
    
          while (x != P)
    
          {
    
          x = x + 1;
    
          }
    
          F = F * x;
    
          System.out.println(F);
    
           
    
          }
    
          }

Similar Threads

  1. How to write program to generate a pascal triangle in java?
    By Aandaleeb in forum Software Development
    Replies: 7
    Last Post: 11-09-2011, 12:37 PM
  2. Need help to write this program in java?
    By frkadeel in forum Software Development
    Replies: 1
    Last Post: 01-12-2010, 03:58 PM
  3. How to write string data to file using java program?
    By Linoo in forum Software Development
    Replies: 5
    Last Post: 21-01-2010, 09:26 PM
  4. What is the Program to find-out factorial of given number?
    By Sheenas in forum Software Development
    Replies: 5
    Last Post: 27-11-2009, 01:42 PM
  5. how to write program on palindrome in java?
    By Linoo in forum Software Development
    Replies: 3
    Last Post: 26-11-2009, 05:19 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,522,357.97203 seconds with 17 queries