Scanner num=new Scanner(System.in);
System.out.println("Enter number in range from 1 to 20");
int n=num.nextInt();
int product=1;
for(int i=1;i<=n;i++){
product*=i;
if(product>Integer.MAX_VALUE || product<Integer.MIN_VALUE){
System.err.println("Out od Integer boundary");
}
else System.out.println("Product of numbers between 1 and "+n+" is "+product);
}
}
}
I'm working on same basic tasks and this particular one is: Compute the product from 1 to 11, 1 to 12, 1 to 13 and 1 to 14. Write down the product obtained and decide if the results are correct. and then Hints: Product of 1 to 13 (=6227020800) is outside the range of int [-2147483648, 2147483647], but within the range of long. Take note that computer programs may not produce the correct answer even though everything seems correct! So if i understand correct Java will automatically transcend int value into long value, but i was thinking to make program which will not allow that. Is is possible? Also feel free to explain anything what is incorrect in code or my thinking.Thanks. p.s Sorry for bad English.