Sum of Digit is Pallindrome or not | Practice | GeeksforGeeks

Given a number N.Find if the digit sum(or sum of digits) of N is a Palindrome number or not.
Note:A Palindrome number is a number which stays the same when reversed.Example- 121,131,7 etc.
Example 1:
Input:
N=56
Output:
1
Explanation:
The digit sum of 56 is 5+6=11.
Since, 11 is a palindrome number.Thus,
answer is 1.
Example 2:
Input:
N=98
Output:
0
Explanation:
The digit sum of 98 is 9+8=17.
Since 17 is not a palindrome,thus, answer
is 0.

Your Task:
You don't need to read input or print aything.Your Task is to complete the function isDigitSumPalindrome() which takes a number N as input parameter and returns 1 if the Digit sum of N is a palindrome.Otherwise it returns 0.

Expected Time Complexity:O(LogN)
Expected Auxillary Space:O(1)
Constraints:
1<=N<=109
+1
rapuriteja11 hours ago
class Solution:   def isDigitSumPalindrome(self,N):       num1 = 0       for i in str(N):           num1 += int(i)       num2 = str(num1)[::-1]              if num1 == int(num2):           return 1       else:           return 0
0
mayank180919991 week ago
  int isDigitSumPalindrome(int N) {
        // code here
        int sum=0,digit;
        while(N!=0){
            digit=N%10;
            sum+=digit;
            N=N/10;
        }
        int m=sum,rem;
        int rev=0;
        while(m!=0){
           rem=m%10;
           rev=rev*10+rem;
           m=m/10;
        }
        if(sum==rev){
            return 1;
        }
      return 0;
    }
0
rsbly7300951 week ago
class Solution {
   isDigitSumPalindrome(N){
       // here time complexity is 4.72;
       // let sum = N.toString().split("").reduce((a,b)=> Number(a) + Number(b));
       // let rev = sum.toString().split('').reverse().join("");
       // if(sum == rev){
       //     return 1;
       // }
       // else return 0;
       
       // time complexity is 3.7;
       let sum = 0;
       while(N) {
           sum = sum + (N%10);
           N = Math.trunc(N/10);
       }
       let rev = 0;
       N = sum;
       
       while(N) {
           rev = rev * 10 + (N%10);
           N = Math.trunc(N/10);
       }
       if (sum == rev){
           return 1;
       }else return 0;
   }
}
0
harshilrpanchal19982 weeks ago
java solution
 
 String temp = String.valueOf(N);   int sum=0;
   for (int i=0 ; i < temp.length() ; i++){       sum += (temp.charAt(i) - '0');   }   StringBuilder sb = new StringBuilder(String.valueOf(sum));   temp = sb.reverse().toString();   if (temp.equals(String.valueOf(sum))){       return 1;   }   return 0;
0
pranjalverma83492 weeks ago
// { Driver Code Starts// Initial Template for C++
#include <bits/stdc++.h>using namespace std;
// } Driver Code Ends// User function Template for C++
class Solution { public:   int isDigitSumPalindrome(int N) {            int c=N,d,s,v,n,g;            while(c)      {          d=c%10;          s=s+d;          c=c/10;      }      v=s;      while(v)      {       n=v%10;       g=g*10+n;       v=v/10;      }            if(g==s)      {
           return 1;                 }      else       return 0;               }};
// { Driver Code Starts.int main() {   int t;   cin >> t;   while (t--) {       int N;       cin >> N;       Solution ob;       cout << ob.isDigitSumPalindrome(N) << "\n";   }} // } Driver Code Ends
0
nyapparel17723 weeks ago
static boolean isPallindrome(int n) { int inputNum = n/10; // not chaing n to compare later int res=n%10; int pow =1; while(inputNum !=0) {  res += ((inputNum%10) *10 *pow);  inputNum /= 10;  pow *=10; } return ( res == n) ? true : false;}
0
ershubhamkewat3 weeks ago
//Recursive solution without using java library
 
int isDigitSumPalindrome(int N) {
        // code here
        int sum = 0;
        //doing sum of digits--================
        while(N!=0){
            sum = sum+N%10;
            N=N/10;
        }
        
 
        
        String s = sum+"";
        Solution sol = new Solution();
        boolean flag = sol.checkPallindrome(0,s.length()-1,s);
        if(flag==true)
            return 1;
        return 0;
        
    }
    
    //============= checking pallindrome=======
    
    public boolean checkPallindrome(int i,int j,String str){
        if((str.charAt(i)==str.charAt(j))&&i<j){
            return checkPallindrome(i+1,j-1,str);
        }else if(i>=j){
            return true;
        }
        
        return false;
    }
 
 
0
sayanmazumder9991 month ago
JAVA
         String n = String.valueOf(N); int sum = 0;         for(int i = 0; i < n.length(); i++) sum += (n.charAt(i) - '0');         StringBuilder str = new StringBuilder(String.valueOf(sum));         n = str.reverse().toString();         if(n.equals(String.valueOf(sum))) return 1;         return 0;
0
0niharika22 months ago
int sum=0, x=N, rev=0;       while(x){           sum+=x%10;           x/=10;       }       x = sum;       while(x){           rev = rev*10 + x%10;           x/=10;       }       if(rev==sum)           return 1;       else           return 0;
+1
krishnakshirsagar412 months ago
class Solution:   def isDigitSumPalindrome(self,N):       strr = str(N)       list1 = []       list1 = list(map(int, strr.strip()))       sum1 = sum(list1)                     if str(sum1) == str(sum1)[::-1]:           return 1       return 0
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.