Wednesday 21 June 2017

Java Code to determine whether or not a number n is a factorial number

Problem:

Design an algorithm to determine whether or not a number n is a factorial number.

Solution:

package com.myprograms;

import java.util.Scanner;

public class FactorialComputation3 {

int n;

public static void main(String[] args) {

FactorialComputation3 factorialComputation3 = new FactorialComputation3();
factorialComputation3.getTheValues();
factorialComputation3.printTheResult();
}

private void getTheValues(){
Scanner s = new Scanner(System.in);
System.out.println("enter a  number");
n = s.nextInt();
s.close();
}

private int findFactorial(){
if(n <= 0)
return -1;
int i = 1;
int j = 2;
while(n%j == 0)
{
if(i*j <= n)
i = i*j;
j++;
}
return i;
}

private void printTheResult(){
if(n==findFactorial()){
System.out.println("the given number is factorial");
}
else{
System.out.println("the given number is not a factorial");
}
}
}


Output:
enter a  number
3628800
the given number is factorial


1 comment:

Program for primality test in JAVA

Problem: ============= Program for primality test in JAVA What is primality test? A primality test is an algorithm for determining wh...