answersLogoWhite

0

Factorial program in java

Updated: 8/10/2023
User Avatar

Wiki User

13y ago

Best Answer

If you're talking about factorial as in "4! = 4 * 3 * 2 * 1 = 24" then you could make a function something like this:

public int fact(int Num) {

int fact;

for (int i = Num; i > 1; i--) {

fact = i * (i - 1)

}

Return fact;

}

User Avatar

Wiki User

13y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

11y ago

fibonacci:

import java.io.*;

class FibonacciSeries

{

public static void main(String args[])throws IOException

{

BufferedReader in=new BufferedReader(new InputStreamReader(System.in));

System.out.println("Enter the limit");

int a=Integer.parseInt(in.readLine()),b=0,c=1,d,e;

System.out.print(b+" "+c+" ");

for(d=1;d<=a;d++)

{

e=b+c;

System.out.print(e+" ");

b=c; c=e;

}

}

}

factorial:

import java.io.*;

class Factoial

{

public static int fac(int x)

{

int a,b=x;

for(a=2;a<b;a++)

{

x*=a;

}

return x;

}

public static void main(String args[])throws IOException

{

BufferedReader in=new BufferedReader(new InputStreamReader(System.in));

System.out.println("Enter the number");

int a=Integer.parseInt(in.readLine());

System.out.println("Factorial of "+a+" is= "+fac(a));

}

}

This answer is:
User Avatar

User Avatar

Wiki User

14y ago

// returns n!

public static long factorial(long n) {

// base case

if(n <= 1) {

return 1;

}

// recursion

return n * factorial(n-1);

}

This answer is:
User Avatar

User Avatar

Wiki User

11y ago

ubet ubet ubet ubet

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Factorial program in java
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

Why factorial is not a valid identifier in Java?

It's been a little while since I've learned Java, but the exclamation point used to denote factorials (!) is already used in Java to denote a negation (e.g. a != 2 means a is not equal to 2). To create a factorial in Java, construct a for loop and multiply all the consecutive integers.


What do you mean by multithread program in java?

A Program in Java that spawns multiple threads is called a multithreaded program in Java.


Write a program in java for factorial?

// Iterative solution public static final long iterativeFactorial(final long n) { long factorial = 1; for (long i = 1; i &lt;= n; i++) { factorial *= i; } return factorial; } // Recursive solution public static final long recursiveFactorial(final long n) { if (n &lt;= 1) { return n; } return n * recursiveFactorial(n - 1); } // Arbitrary length solution - may take a while, but works on any positive number. public static final BigInteger factorial(final BigInteger n) { BigInteger factorial = BigInteger.ONE; for (BigInteger i = BigInteger.ONE; i.compareTo(n) &lt;= 0; i = i.add(BigInteger.ONE)) { factorial = factorial.multiply(i); } return factorial; }


What is java virtucal mechine?

That refers to the program that runs the compiled Java program.


Write a java script program to print first ten odd natural numbers in C?

Q.1 Write a program to print first ten odd natural numbers. Q.2 Write a program to input a number. Print their table. Q.3 Write a function to print a factorial value.

Related questions

Jntu 2-2 oops through java answers?

write a java program to find factorial using recursive and non recursive


What is a factorial loop?

An example in Java, to compute 10!: int factorial = 1; for(int i = 1; i &lt; 11; i++) { factorial *= i; }


Write a program in java to find factorial of a number?

I suggest to use a for loop, more or less like this (assuming the parameter is "n"): product = 1; for (int i = 1; i &lt;= n; i++) { product *= i; }


Why factorial is not a valid identifier in Java?

It's been a little while since I've learned Java, but the exclamation point used to denote factorials (!) is already used in Java to denote a negation (e.g. a != 2 means a is not equal to 2). To create a factorial in Java, construct a for loop and multiply all the consecutive integers.


What do you mean by multithread program in java?

A Program in Java that spawns multiple threads is called a multithreaded program in Java.


Write a program in java for factorial?

// Iterative solution public static final long iterativeFactorial(final long n) { long factorial = 1; for (long i = 1; i &lt;= n; i++) { factorial *= i; } return factorial; } // Recursive solution public static final long recursiveFactorial(final long n) { if (n &lt;= 1) { return n; } return n * recursiveFactorial(n - 1); } // Arbitrary length solution - may take a while, but works on any positive number. public static final BigInteger factorial(final BigInteger n) { BigInteger factorial = BigInteger.ONE; for (BigInteger i = BigInteger.ONE; i.compareTo(n) &lt;= 0; i = i.add(BigInteger.ONE)) { factorial = factorial.multiply(i); } return factorial; }


What is java virtucal mechine?

That refers to the program that runs the compiled Java program.


What is java program on computers?

#!/usr/bin/perl print 'java program';


What powder game doesnt need java?

Any program written for the Java technology needs Java. Any program NOT written for Java DOESN'T need it.


C program to find factorial of a no?

this is a code for calculating it recursivelly: float Factorial (float n) { if (n&lt;=1) return 1.0; else return n* Factorial(n-1); }


Java Track?

Java Track is the java training program offered by SynergisticIT


Where can one get the program Java?

The program Java can be downloaded from the official Java website on the internet. The program is free for personal use, so it can be downloaded without any cost by anyone.