Are you wondering how to seek help from subject matter experts and learn the Java language? Go with these Basic Java Programming Examples and try to code all of them on your own then check with the exact code provided by expert programmers.
Program for fining factorial of a number
In this article we will see different way to find out the factorial of a number .
Concept :
Factorial is mostly used in permutation and combination. It is calculated as the product of the all positive number from 1 to n. for example if the number is N , then the factorial would be ( 1 x 2 x 3 x ….. x N) .
The factorial value of 0 is ‘1’.
There is no factorial value for negative number .
Example : number =5 factorial = 1 x 2 x 3 x 4 x 5 =120
Now we will see 2 different ways to do it.
Method 1: By using for loop
By using for loop we can calculate the factorial of a number.
Approach :
- Enter a number .
- Take a variable and initialize 1 into it .
- Take a for loop form 1 to that number and for each step multiply the value with that variable .
- Print the value .
Program :
import java.util.Scanner; class Main{ public static void main(String args[]) { int x,f=1, n ; // creating object . Scanner sc = new Scanner(System.in); // entering a number . System.out.println("Enter a number :"); n = sc.nextInt(); sc.close(); //calculating factorial value . for(x=1;x<=n;x++) { f=f*x; } // printing factorial value System.out.println("Factorial of "+n+" = "+f); } }
Output: Enter a number : 5 Factorial of 5 = 120
Method 2: By using while loop
By using while loop we can calculate the factorial of a number.
Approach :
- Enter a number .
- Take a variable and initialize 1 into it .
- Take a while loop form 1 to that number and for each step multiply the value with that variable .
- Print the value .
Program:
import java.util.Scanner; class Main { public static void main(String args[]) { int x=1,f=1, n ; // creating object . Scanner sc = new Scanner(System.in); // entering a number . System.out.println("Enter a number :"); n = sc.nextInt(); sc.close(); //calculating factorial value . while(x<=n) { f = f * x; x++; } // printing factorial value System.out.println("Factorial of "+n+" = "+f); } }
Output: Enter a number : 5 Factorial of 5 = 120
Get started with learning the programming language Java from beginner to experienced level by referring to our collection of Java Programs with Source Code and become a pro in the subject.
Related Java Decision Making and Loop Programs:
- Java Program to Check Leap Year
- Java Program to Check Whether a Number is Positive or Negative
- Java Program to Check Whether a Character is Alphabet or Not
- Java Program to Calculate the Sum of Natural Numbers
- Java Program to Generate Multiplication Table
- Java Program to Find GCD of two Numbers
- Java Program to Find LCM of two Numbers
- Java Program to Display Alphabets (A to Z) using loop
- Java Program to Count Number of Digits in an Integer
- Java Program to Check Palindrome
- Java Program to Check Whether a Number is Prime or Not
- Java Program to Check Armstrong Number
- Java Program to Display Armstrong Number Between Two Intervals
- Java Program to Make a Simple Calculator Using switch…case
- Java Program to Sort Elements in Lexicographical Order (Dictionary Order)