JAVA Program to find factorial of a number
In mathematics, the factorial of a non-negative integer n, denoted by n!, is the product of all positive integers less than or equal to n:
For example: 6! = 6*5*4*3*2*1 = 720
Join Us
For those who prefer reading on mobile, we have Telegram channel and WhatsApp group that will allow you to receive updates, announcements, and links to our stories straight to your mobile device..
Steps to be followed for finding the factorial of a number.
- Input the number.
- Initialise a variable fact=1;
- Create a loop for 1 to Number
- Multiply each value of loop to the fact.
- Print the value of fact
Sounds Good ? Let's get started
CODE:
/* * Author: TheSchoolProgrammer * www.theschoolprogrammer.com * Question:Write a program to accept a number and print its factorial. */ import java.util.*; public class MyClass { public static void main(String args[]) { Scanner sc=new Scanner(System.in); System.out.println("Enter the number"); int n=sc.nextInt(); int i,fact=1; for(i=1;i<=n;i++) { fact=fact*i; } System.out.println("Factorial of "+n+" is: "+fact); } }
Output:
Example 1 Input: Enter the number 5 Output: Factorial of 5 is 120 Example 2 Input: Enter the number 4 Output: Factorial of 4 is 24
Also check:
- JAVA Program To Check Given Number Is Prime-Adam or Not [ISC 2020 Practical]
- ISC 2020 Computer Science Practical Paper solved
- Program to sort elements in ascending order using the bubble sort technique [ICSE 2019]
- JAVA Program to print common factors of two number.
- JAVA Program to find LCM of two numbers using simplest algorithm
- JAVA Program to find the largest factor of a number
- Java Program to accept three numbers and print the second lowest number.
Found this useful? Share with your friends. Share
NJ speeds sports betting at William Hill's New Jersey
ReplyDeleteThe William Hill 의정부 출장마사지 Sportsbook 정읍 출장안마 app will bring live-stream 여수 출장샵 betting to nearly 원주 출장안마 every sportsbook 전주 출장안마 in New Jersey. “We have made it possible to use all of our