Search code examples
javainteger-overflow

How to get rid of this overflow error?


I am having a code, which gives incorrect output as i am working with big numbers. I want a solution to this that how could i improve this to accommodate big numbers. Which datatype i should use?

CODE:

    static int get(int n,int i,int digit)
    {
      int p;
      p=(int)Math.pow(10,i-1);
      n=n/p;
      return n%10;
    }
    static boolean check_pal(int n)
    {
      int digit;
      digit=(int) (Math.log10(n)+1);
      int a=0,b=0,i,j,p;
      int sum=0;
      for(i=1,j=digit-1 ; i<=digit ; i++,j-- )
      {
        a=(int) get(n,i,digit);
        sum+=a*Math.pow(10,j);
      }
      if(sum==n)
        return true;
      else
        return false;
    }
    static int reverse(int n)
    {
        int digit;
        digit=(int) (Math.log10(n)+1);
        int a=0,b=0,i,j,p;
        int sum=0;
        for(i=1,j=digit-1 ; i<=digit ; i++,j-- )
        {
            a=(int) get(n,i,digit);
            sum+=a*Math.pow(10,j);
        }
        return n+sum;
    }
    public static void main(String[] args) {

    try{
        Scanner sc=new Scanner(System.in);
        int n=sc.nextInt();
        if(n<10 || n>999){
          System.out.println("None");   
            return;}
        boolean c;

        for(int i=1 ; i<=100 ; i++)
        {
            System.out.println("iteration"+i+" value is "+n);
        c=check_pal(n);
        if(c==true)
        {
            System.out.println(n);
            return;
        }

        n=reverse(n);
    }
    System.out.println("None");
    }

    catch(Exception e)
    {
       System.out.println("NONE"); 
    }
    }

Here is output: output

In output, Iteration 17th get negative value this shows overflow. I want a solution so that this work for all inputs between 10 to 999.

Here is problem definition click here !!


Solution

  • You could use long instead of int:

    static long get(long n,long i,long digit)
        {
          long p;
          p=(long)Math.pow(10,i-1);
          n=n/p;
          return n%10;
        }
    
    static boolean check_pal(long n)
        {
          long digit;
          digit=(long) (Math.log10(n)+1);
          long a=0,b=0,i,j,p;
          long sum=0;
          for(i=1,j=digit-1 ; i<=digit ; i++,j-- )
          {
            a=(long) get(n,i,digit);
            sum+=a*Math.pow(10,j);
          }
          if(sum==n)
            return true;
          else
            return false;
        }
    
    static long reverse(long n)
        {
            long digit;
            digit=(long) (Math.log10(n)+1);
            long a=0,b=0,i,j,p;
            long sum=0;
            for(i=1,j=digit-1 ; i<=digit ; i++,j-- )
            {
                a=(long) get(n,i,digit);
                sum+=a*Math.pow(10,j);
            }
            return n+sum;
        }
    

    iteration25 value is 8813200023188

    By the way: your check_pal method could be much shorter:

     static boolean check_pal(long n){
        return reverse(n) == n;
     }
    
     static long reverse(long n)
        {
            long digit;
            digit=(long) (Math.log10(n)+1);
            long a=0,b=0,i,j,p;
            long sum=0;
            for(i=1,j=digit-1 ; i<=digit ; i++,j-- )
            {
                a=(long) get(n,i,digit);
                sum+=a*Math.pow(10,j);
            }
            return sum;
        }
    
     static long reverseAndAdd(long n){
         return n + reverse(n);
     }
    

    (note I changed the last line of the reverse method and added a reverseAndAdd, because your reverse does not do what it says :-))