Integer Overflow
Completed
A 32-bit int holds up to 2147483647. Read n and print n * n computed as an int and then as a double (no decimals).
Input
One integer.
Output
Two lines.
Example
Input
1000
Output
1000000 1000000
Editor
C
Hints
When the result is too big for an int, it wraps around and becomes wrong (even negative).
Solution explanation
Overflow is silent in C. A double can hold much larger whole numbers exactly (up to 2⁵³).
Show solution
#include <stdio.h>
int main() {
int n;
scanf("%d", &n);
int square = n * n;
double exact = (double) n * n;
printf("%d\n", square);
printf("%.0f\n", exact);
return 0;
}