You are not logged in. Please login at to post your questions!


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.

* 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.*;
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(;
        int arr[]=new int[t];


            // TODO code application logic here


asked 19 May '12, 22:35

shivran352's gravatar image

accept rate: 0%

edited 22 Jun '12, 14:54

admin's gravatar image

0★admin ♦♦

@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(

(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.


answered 20 May '12, 19:19

ritesh_gupta's gravatar image

5★ritesh_gupta ♦
accept rate: 27%

edited 20 May '12, 19:26


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 .

(20 May '12, 21:39) shivran3520★

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 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);

(21 May '12, 10:07) ritesh_gupta ♦5★

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


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:


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.


answered 20 May '12, 03:01

betlista's gravatar image

3★betlista ♦♦
accept rate: 11%

edited 20 May '12, 14:29

thanks for suggestion........

(20 May '12, 21:40) shivran3520★
toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here



Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text]( "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported
  • mathemetical formulas in Latex between $ symbol

Question tags:


question asked: 19 May '12, 22:35

question was seen: 3,548 times

last updated: 22 Jun '12, 14:54