Search code examples
javasumpalindrome

To find the single digit sum of palindrome numbers in array


I tried to check the palindrome number and find the single digit sum of the palindrome numbers, but my code is not returning the proper value (I am finding it difficult to return the value of sum from the loops). Can anyone help me getting the mistake. Any help will be appriciated.

import java.util.Scanner;

public class SumOfPalindrome {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter the number of array elements");
        int n = sc.nextInt();
        int[] inp = new int[n];
        System.out.println("enter the elements");
        for(int i = 0; i< n ;i++)
        {
            inp[i] = sc.nextInt();
        }
        System.out.println(SumOfPalindromeNumber(inp));
        

    }

    private static int SumOfPalindromeNumber(int[] inp )
    {
        
        int sumpal =0;
        for(int i = 0; i<inp.length;i++)
        {
            int rem =0; 
            int sum = 0;
            while(inp[i]!=0)
            {
                rem = inp[i]%10;
                sum=(sum*10)+rem;
                inp[i]/=10;
            }
            if(inp[i]==sum)
            {
                sumpal+=inp[i];
                
                if(sumpal>9)
                {
                    sumpal=singledigitsum(sumpal);
                }
            }
            
        }
        return sumpal;  
    
    }

    private static int singledigitsum(int sumpal)
    {
        int rem1 = 0;
        int sum1 = 0;
        while(sumpal!=0)
        {
            rem1=sumpal%10;
            sum1+=rem1;
            sumpal/=10;
        }
        if(sum1>9)
        {
            sum1=singledigitsum(sum1);
        }
        return sum1;
    }

}

Solution

    1. Enter numbers

    2. Check which numbers are palindromes.

    3. If that number is a palindrome then find sum of its digits.

    Code:

        import java.util.Scanner;
    
    public class SumOfPalindrome {
    
        public static void main(String[] args) {
            SumOfPalindrome sumOfPalindrome=new SumOfPalindrome();
            Scanner sc = new Scanner(System.in);
        System.out.println("Enter the number of array elements");
        int n = sc.nextInt();
        int[] inp = new int[n];
        System.out.println("enter the elements");
        for(int i = 0; i< n ;i++)
        {
            inp[i] = sc.nextInt();
        }
        
        for(int i=0;i<n;i++)
        {
            if(sumOfPalindrome.isPallindrone(inp[i]))
            {
                System.out.println("No -> "+inp[i]+" Sum->"+sumOfPalindrome.sumOfDigits(inp[i]));
            }
        }
    }//
    
    public boolean isPallindrone(int no)
    {
        int r,sum=0,temp;    
        
    
        temp=no;    
        while(no>0){    
            r=no%10;  //getting remainder  
            sum=(sum*10)+r;    
            no=no/10;    
        }    
        if(temp==sum)    
        {
            return true;
        }
        else    
        {
            return false;
        }
    }
    
    
    
    public int sumOfDigits(int no) 
    {
        int sum = 0; 
        
        while (no != 0) 
        { 
            sum = sum + no % 10; 
            no = no/10; 
        }   
    
    
        return sum;
        }
    }
    

    I did not understand the purpose of sumpal in your code. You wanted a method which return sumOfPalindromes in an array. I commented the part which was wrong.

    import java.util.Scanner;
    
    public class SumOfPalindrome_1 
    {
    
        public static void main(String[] args) {
            Scanner sc = new Scanner(System.in);
            System.out.println("Enter the number of array elements");
            int n = sc.nextInt();
            int[] inp = new int[n];
            System.out.println("enter the elements");
            for(int i = 0; i< n ;i++)
            {
                inp[i] = sc.nextInt();
            }
            //System.out.println(SumOfPalindromeNumber(inp));
            SumOfPalindromeNumber(inp);
            
    
        }
    
        private static void SumOfPalindromeNumber(int[] inp )
        {
            
            int sumpal =0;
            for(int i = 0; i<inp.length;i++)
            {
                int rem =0; 
                int sum = 0;
                while(inp[i]!=0)
                {
                    rem = inp[i]%10;
                    sum=(sum*10)+rem;
                    inp[i]/=10;
                }
                if(inp[i]==sum)
                {
                   // sumpal+=inp[i];
                    
                    /*if(sumpal>9)
                    {
                        sumpal=singledigitsum(sumpal);
                    }
                    */
                    System.out.println(singleDigitSum(inp[i]));
                }
                
            }
            
        
        }
    
        private static int singleDigitSum(int sumpal)
        {
            int rem1 = 0;
            int sum1 = 0;
            while(sumpal!=0)
            {
                rem1=sumpal%10;
                sum1+=rem1;
                sumpal/=10;
            }
            if(sum1>9)
            {
                sum1=singleDigitSum(sum1);
            }
            return sum1;
        }
    
    }