+ 4

How to write code in java for factorial of a number in which number is taken as input from user ?

code in java factorial of a number taking input from user

3rd Nov 2017, 8:44 AM
Neeraj
Neeraj - avatar
7 Answers
+ 26
Scanner sc=new Scanner (System.in); int n=sc.next ();int y=1; for (int x=1;n>=x;x++){ y*=x; }
3rd Nov 2017, 9:48 AM
Gaurav Agrawal
Gaurav Agrawal - avatar
+ 6
Note that factorials can be created by for loops, or by recursion, which method you want?
3rd Nov 2017, 8:59 AM
👑 Prometheus 🇸🇬
👑 Prometheus 🇸🇬 - avatar
+ 3
thanks guys really appreciate your help #Itz_Arnab # gaurav
3rd Nov 2017, 12:55 PM
Neeraj
Neeraj - avatar
+ 2
#include<iostream> using namespace std; int main(){ int fact=1; int num; cout<<"Please enter a number"<<endl; cin>>num; while(num>0) { fact*=num; } cout<<"Factorial : "<<fact<<endl; return 0; } Hope it helps.
3rd Nov 2017, 9:30 AM
Itz_Arnab
Itz_Arnab - avatar
+ 1
import java.util.Scanner; class Demo{ public static void main(String[] args) { Scanner sc= new Scanner(System.in); int N= sc.nextInt(); int x=1; int fact=1; for (x,x<=N,x++){ fact=fact*x }{System.out.print(fact);} }
26th Sep 2023, 3:04 PM
Dhruv
17th Nov 2017, 9:27 AM
#RahulVerma
#RahulVerma - avatar
0
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int num = sc.nextInt(); long factorial = multiplyNumbers(num); System.out.println(factorial); } public static long multiplyNumbers(int num) { if (num >= 1) return num * multiplyNumbers(num - 1); else return 1; } } Use this and thank me later.
28th Feb 2024, 2:00 AM
Tony