×

# small factorials

 1 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

 4 @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. answered 20 May '12, 19:19 3.7k●4●25●49 accept rate: 27% 1 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) 2 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); (21 May '12, 10:07)
 1 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. answered 20 May '12, 03:01 16.9k●49●115●225 accept rate: 11% thanks for suggestion........ (20 May '12, 21:40)
 toggle preview community wiki:
Preview

By Email:

Markdown Basics

• *italic* or _italic_
• **bold** or __bold__
• 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:

×1,133
×387

question asked: 19 May '12, 22:35

question was seen: 3,472 times

last updated: 22 Jun '12, 14:54