small factorials

how to rectify NZEC error in java for below code which runs well on netbeans 5.0 and gives correct factorial value for 100,99 etc.

/*
* Main.java
*
* Created on May 19, 2012, 12:39 PM
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/

package javaapplication5;

/**
*
* @author Shivran Roy
*/
import java.util.*;
import java.io.*;
public class Main {

	/** Creates a new instance of Main */
	public Main() {
	}

	/**
	* @param args the command line arguments
	*/
	public static void   main(String[] args) {

		int k,j,i,t,l;
		double f;
		Scanner input=new Scanner(System.in);
		t=input.nextInt();
		int arr[]=new int[t];
		j=0;
		for(i=0;i<t;i++)
		{
			arr[i]=input.nextInt();
			System.out.print("\n");
		}
		for(k=0;k<=i;k++)
		{

			for(f=1,l=1;l<=arr[k];l++)
			{
				f=f*l;
			}
			System.out.println(f);

			// TODO code application logic here
		}

	}
}
1 Like

Your class cannot be in package. Use so called default package.

edit:

description in detail

When you submit your solution, CodeChef server tries to compile it. If you want to compile your Main class from command line you have to execute:

javac Main.java

Next for correctness checking CodeChef server runs the class

java Main < test.input

That’s what CodeChef server tries to do. But if your class is in package (javaapplication5 in your case) CodeChef server have to now, that your submitted code have to be placed to javaapplication5 folder and compiled as

java javaapplication5\Main

and run as

javac javaapplication5.Main

Simple solution (if you are submitting code using editor) is removing package declaration line package javaapplication5 from code (it will work exactly the same way).

PS: I have to warn you that there is similar problem in your code as asked here.

1 Like

@shivran352 :There are quite a few petty errors and one or two blunders in your Java Code:

(1).The Main Reason Behind NZEC is the line “for(f=1,l=1;l<=arr[k];l++)” .The Array arr index becomes out of bound for large input input value like 100.

Ran your code on IDEONE.COM ,the runtime error detail is :: Exception in thread “main” java.lang.ArrayIndexOutOfBoundsException: 5
at Main.main(Main.java:31)

(2).I don’t know why the type of f is “Double”.Its meaning less and moreover running the loop for(f=1,l=1;l<=arr[k];l++) where the type of f is double,well i have never seen such thing in my life.

(3).Don’t use package word in your code if you are submitting it to codechef judge.

(4)what is the purpose of System.out.print("\n") after arr[i]=input.nextInt() instruction?Printing blank line is absurd.

4 Likes

thanks for ur analysis…
how can i make an integer array in java which takes input in runtime and its index does not go out of bounds for large values?
and i have used double so that the value of factorial for large no. like 100,98 etc remains in its range .

1 Like

thanks for suggestion…

Well as the factorial of 100 is quite large so no data type can hold such a large value.

But as you are a java Coder ,so u are quite lucky.
There is a data-type called Big Integers (in java.math.*) ,that can holds any large value.

Go through the following page http://wiki.compsci.ca/index.php?title=Java_Big_Integers to learn more about it.
Here is the code that will help you to calculate large factorials using BigInteger:
BigInteger fact=new BigInteger (“1”);
for(int j=1;j<=N;j++)
{
fact=fact.multiply(BigInteger.valueOf(j));
}
System.out.println(fact);

2 Likes