Why this code gives negative values for some input values?? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 4

Why this code gives negative values for some input values??

import java.util.Scanner; public class product { public static void main (String [] args){ System.out.println("Enter a number = ") ; Scanner sc = new Scanner(System.in); int n=sc.nextInt(); int i=1; long mul=1; while(i<=n){ mul=mul*i; i++; } System.out.println(mul); } } input=21 output=-4249290049419214848 why??

3rd Dec 2017, 2:30 PM
Pulkit sharma
Pulkit sharma - avatar
0 Answers