+ 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
7 Respuestas
+ 26
Scanner sc=new Scanner (System.in);
int n=sc.next ();int y=1;
for (int x=1;n>=x;x++){
y*=x;
}
+ 6
Note that factorials can be created by for loops, or by recursion, which method you want?
+ 3
thanks guys
really appreciate your help
#Itz_Arnab
# gaurav
+ 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.
+ 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);}
}
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.