In: Computer Science
Factorials in Java: Write out your Program Design for how to automate the testing. The objective is to repeat the factorial calculation for different values of n, as n changes from 2, 3, 4, 5, 6, 7..
1) test for an int
2) test for a long
3)test for a double
Code:
import java.util.*;
class sample
{
public static void main(String args[])
//main
method
{
Scanner scan=new
Scanner(System.in);
//object creation for scanner
int n=scan.nextInt();
//taking input n from user to what extent i've
to perform factorial
long fact=1;
System.out.println("Integers");
for(int
i=2;i<=n;i++)
//iterating loop from 2 to n
integers
{
fact=fact*i;
//finding factorial
System.out.println(i+" "+fact);
//printing factorial
}
long fact1=1;
System.out.println("Long
integer");
for(long
i=2;i<=n;i++)
//iterating loop from 2 to n
long integers
{
fact1=fact1*i;
//finding factorial
System.out.println(i+" "+fact1);
//printing factorial
}
double fact2=1;
System.out.println("Double");
for(double
i=2;i<=n;i++)
//iterating loop from 2 to n doubles
{
fact2=fact2*i;
//finding factorial
System.out.println(i+" "+fact2);
//printing factorial
}
}
}