Run-error in the code

import java.util.Scanner;
import java.lang.Math;
public class Main {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
int num = sc.nextInt();
if(num >=1)
{
int digits = (int)(Math.log10(num)) + 1;
int [] a = new int[digits];
for(int i = 0 ;i< digits ;i++)
{
a[i] = num%10;
num = num/10;
}
int result = 0;
for(int i =digits-1;i>=0 ; i–)
{
if(a[i]<9-a[i])
{
result = result10 + a[i];
}
else
{
result = result
10 + (9-a[i]);
}
} System.out.println(result);
}
else
{System.out.println(“Invalid Input”);}
}

}

Hi Manan,
You are using ‘int’ data type which has a size of 4 byte, therefore its range is 2^31-1 which is approx 10^9. But in the question, the given range is 10^18. So, you should change your data type to long which has a size of 8 bytes. After that there’ll be another error when you store the digits into the array. Its a type casting error, try to figure it out. Ping me if you need help. :slight_smile:

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.