Calculate factorial of a number.

Ads
 

Calculate factorial of a number.

How to calculate factorial of a given number?

View Answers

May 23, 2012 at 6:00 PM

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;


public class Factorial {
    public static void main(String args[]) throws IOException {
        System.out.println("Please Input Any Number");
        int num = 0;
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String data = (String) br.readLine();
        num = Integer.parseInt(data);
            int result = num;
        for (int i = 1; i < num; i++) {
            result = result*i;
        }
        System.out.println(result);
    } }

Description: - Factorial of any number n is represented by !n and is equal to n(n-1)(n-2)(n-3)?.1. BufferReader () is used for user input.BufferReader consider inputted value as String so for int value you have to use Integer.parseInt(). For loop from 1 to num-1 calculating the factorial value of num as result=result*i. Initial value of result is num.


May 25, 2012 at 12:23 AM

Explanation : Java supports recursion. Recursion is the process of defining something in terms of itself. As it relates to java programming, recursion is the attribute that allows a method to call itself. A method that calls itself is said to be recursive.

The classic example of recursion is the computation of the factorial of a number. The factorial of a number N is the product of all the whole numbers between 1 and N. for example, 3 factorial is 1?2?3, or 6. Here is how a factorial can be computed by use of a recursive method

Ads









Related Tutorials/Questions & Answers:
Calculate factorial of a number.
Calculate factorial of a number.  How to calculate factorial of a given number?   import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class Factorial { public static
program on factorial of a given number in java
program on factorial of a given number in java  please tell me, how to write program on factorial of a given number in java
Advertisements
program on factorial of a given number in java
program on factorial of a given number in java  please tell me, how to write program on factorial of a given number in java
find factorial of any number using recursion
find factorial of any number using recursion  error in 14 line ; expected int rec( int a) ^ how to resolve this.please correct the code. import...)); System.out.println(?enter number?); int x=Integer.parseInt(br.readLine()); int result
Write a program to find a factorial in any given number
Write a program to find a factorial in any given number This programming tutorial will teach you how to write a factorial of any given number... the factorial of any given number import java.util.Scanner; class
Javascript calculate number of days between two dates
Javascript calculate number of days between two dates In this tutorial, you will learn how to calculate number of days between two dates.For this, you need.... The desired interval can then be determined by dividing that number
factorial of fibonacci
factorial of fibonacci   A code for the factorial of a fibonacci series. Example when the user enters 6, the fibonacci series is 8 and the factorial will be 8! or 8 x 7 x 6 x 5 x 4 x 3 x 2 x 1. So if the user enters the number 6
Modify the sales tax program to accept an arbitrary number of prices, total them, calculate the sales tax and print the total amount.
Modify the sales tax program to accept an arbitrary number of prices, total them, calculate the sales tax and print the total amount.  Modify the sales tax program to accept an arbitrary number of prices, total them, calculate
find the factorial
find the factorial  how to find the factorial through the while loop...) { int value = 5, factorial = 1, temp = value; while (temp > 0) { factorial *= temp; temp
How to get factorial in mysql
alot for a function in mysql to find factorial numbers of a number and couldn't...How to get factorial in mysql  Hi, In order to improve the speed.... example: for 1155, the factorial numbers would be: 3, 5, 7, 11 Thank you
factorial using recursive
factorial using recursive  can please give this code !i am going to do a factorial function using recursion definition and then . my professor told... OR NO..after that if yes : ENTER NUMBER: id try again input another number. if no back
ModuleNotFoundError: No module named 'factorial'
ModuleNotFoundError: No module named 'factorial'  Hi, My Python... 'factorial' How to remove the ModuleNotFoundError: No module named 'factorial' error? Thanks   Hi, In your python environment you
Factorial Program in Java
the number 5 Factorial of 5: 120.0...Factorial program in Java helps programmer to write factors of any given integer number. In the following example, a class by the name "Factorial1" is used
calculate average
calculate average   Design and write a java program to determine all... to the original number. abc = a^3 + b^3 + C^3 ( NOTE 407 is one of them) 2.Design and write a program and calculate your final average in this course. The details
ModuleNotFoundError: No module named 'factorial-hunan'
ModuleNotFoundError: No module named 'factorial-hunan'  Hi, My... named 'factorial-hunan' How to remove the ModuleNotFoundError: No module named 'factorial-hunan' error? Thanks   Hi, In your python
ModuleNotFoundError: No module named 'factorial_qxj'
ModuleNotFoundError: No module named 'factorial_qxj'  Hi, My... 'factorial_qxj' How to remove the ModuleNotFoundError: No module named 'factorial_qxj' error? Thanks   Hi, In your python
ModuleNotFoundError: No module named 'factorial-hunan'
ModuleNotFoundError: No module named 'factorial-hunan'  Hi, My... named 'factorial-hunan' How to remove the ModuleNotFoundError: No module named 'factorial-hunan' error? Thanks   Hi, In your python
ModuleNotFoundError: No module named 'factorial_qxj'
ModuleNotFoundError: No module named 'factorial_qxj'  Hi, My... 'factorial_qxj' How to remove the ModuleNotFoundError: No module named 'factorial_qxj' error? Thanks   Hi, In your python
calculate average
calculate average  Question 2 cont.d Test case c: home works/labs 10 9 9 9 10 10 9 9 10 10 9 9 10 test scores: 65 89 note, the program should work with different numbers of home works/labs
calculate the time ..
calculate the time ..  [CODE] Elapsed Time Example <script type="text/javascript"> var startTime = null...="button" value="Calculate Difference" onclick="calculateTimeElapsed();" />
calculate difference between two dates
calculate difference between two dates  hi, I was actually working on to calculate the number of days between two dates of dd/mm/yyyy format using javascript. can anyone suggest me how to work it outin calculating
calculate a list of products
calculate a list of products  Hi can you help me with this java... items displayed on the screen, then calculate the user's final bill, including... for 60. Buy something! Enter Order Number (0 to stop) : K3876 Enter Quantity: 2
calculate size of array
calculate size of array  Is it possible to calculate the size of array using pointers in Java
calculate reward points.
calculate reward points.  How to calculate reward points in a multiplex automation system
ModuleNotFoundError: No module named 'calculate'
ModuleNotFoundError: No module named 'calculate'  Hi, My Python... 'calculate' How to remove the ModuleNotFoundError: No module named 'calculate' error? Thanks   Hi, In your python environment you
Code to calculate hair density
Code to calculate hair density  Hello, Would you please give me a code to calculate the hair density on scalp from image in java
calculate sum of individual digit
calculate sum of individual digit   How calculate product of first and last digit of indiviual numbers Like a=43278 4*8=32
Calculate sum and Average in java
Calculate sum and Average in java  How to calculate sum and average in java program?   Example:- import java.io.BufferedReader; import... the sum by the total number of numbers, we have determined the average of numbers
calculate sum of individual digit
calculate sum of individual digit  Hi How to calculate product of first snd last digit of indiviual digit Like Num=23456 S=2*6
How to calculate attending hours?
How to calculate attending hours?  I need to calculate attending hours of a employee in a day in my project to make their monthly salary . By using work starting time and ending time. How should I do it using Date class? Or any
How to calculate days in a year
How to calculate days in a year  Hi, How to calculate no of days in a year? I want a program that calculates no of days in a year by specifying year a parameter. Thanks   Hi, Please check the thread example

Ads