Sum of Digits is Palindrome or not (original) (raw)

Last Updated : 14 Apr, 2026

Given an integer **n, the task is to check whether the sum of digits of **n is palindrome or not.
**Example:

**Input: n = 56
**Output: true
**Explanation: Digit sum is (5 + 6) = 11, which is a palindrome.

**Input: n = 51241
**Output: false
**Explanation: Digit sum is (5 + 1 + 2 + 4 + 1) = 13, which is not a palindrome.

Try It Yourselfredirect icon

Table of Content

[Expected Approach] Checking Iteratively - O(log n) Time and O(1) Space

The basic idea is to find the sum of the digits of a number and then check if this sum is a palindrome by comparing its digits from both ends one by one.

C++ `

// C++ implementation of the approach #include <bits/stdc++.h> using namespace std;

int digitSum(int n) { int sum = 0; while (n > 0) { sum += (n % 10); n /= 10; } return sum; }

bool isPalindrome(int n) {

// Find the appropriate divisor
// to extract the leading digit
int divisor = 1;
while (n / divisor >= 10)
    divisor *= 10;

while (n != 0) {
    int leading = n / divisor;
    int trailing = n % 10;

    // If first and last digit
    // not same return false
    if (leading != trailing)
        return false;

    // Removing the leading and trailing
    // digit from number
    n = (n % divisor) / 10;

    // Reducing divisor by a factor
    // of 2 as 2 digits are dropped
    divisor = divisor / 100;
}
return true;

}

bool isDigitSumPalindrome(int n) {

int sum = digitSum(n);

// If the digit sum is palindrome
if (isPalindrome(sum))
    return true;
return false;

}

int main() { int n = 56;

if (isDigitSumPalindrome(n))
    cout << "true";
else
    cout << "false";

return 0;

}

C

#include <stdio.h>

int digitSum(int n) { int sum = 0; while (n > 0) { sum += (n % 10); n /= 10; } return sum; }

int isPalindrome(int n) { // Find the appropriate divisor // to extract the leading digit int divisor = 1; while (n / divisor >= 10) divisor *= 10;

while (n != 0) {
    int leading = n / divisor;
    int trailing = n % 10;

    // If first and last digit
    // not same return false
    if (leading != trailing)
        return 0;

    // Removing the leading and trailing
    // digit from number
    n = (n % divisor) / 10;

    // Reducing divisor by a factor
    // of 2 as 2 digits are dropped
    divisor = divisor / 100;
}
return 1;

}

int isDigitSumPalindrome(int n) { int sum = digitSum(n);

// If the digit sum is palindrome
if (isPalindrome(sum))
    return 1;
return 0;

}

int main() { int n = 56;

if (isDigitSumPalindrome(n))
    printf("true");
else
    printf("false");

return 0;

}

Java

// Java implementation of the approach import java.util.*;

class GfG { static int digitSum(int n) { int sum = 0; while (n > 0) { sum += (n % 10); n /= 10; } return sum; }

static boolean isPalindrome(int n) {

// Find the appropriate divisor
// to extract the leading digit
int divisor = 1;
while (n / divisor >= 10)
    divisor *= 10;

while (n != 0) {
    int leading = n / divisor;
    int trailing = n % 10;

    // If first and last digit
    // not same return false
    if (leading != trailing)
        return false;

    // Removing the leading and trailing
    // digit from number
    n = (n % divisor) / 10;

    // Reducing divisor by a factor
    // of 2 as 2 digits are dropped
    divisor = divisor / 100;
}
return true;

}

static boolean isDigitSumPalindrome(int n) {

// Sum of the digits of n
int sum = digitSum(n);

// If the digit sum is palindrome
if (isPalindrome(sum))
    return true;
return false;

}

public static void main(String []args) { int n = 56;

if (isDigitSumPalindrome(n))
    System.out.println("true");
else
    System.out.println("false");

} }

Python

Python3 implementation of the approach

def digitSum(n) :

sum = 0; 
while (n > 0) :
    sum += (n % 10); 
    n //= 10; 

return sum; 

def isPalindrome(n) :

# Find the appropriate divisor 
# to extract the leading digit 
divisor = 1; 
while (n // divisor >= 10) :
    divisor *= 10; 

while (n != 0) :
    leading = n // divisor; 
    trailing = n % 10; 

    # If first and last digit 
    # not same return false 
    if (leading != trailing) :
        return False; 

    # Removing the leading and trailing 
    # digit from number 
    n = (n % divisor) // 10; 

    # Reducing divisor by a factor 
    # of 2 as 2 digits are dropped 
    divisor = divisor // 100; 

return True; 

def isDigitSumPalindrome(n) :

# Sum of the digits of n 
sum = digitSum(n); 

# If the digit sum is palindrome 
if (isPalindrome(sum)) :
    return True; 
return False; 

if name == "main" :

n = 56; 

if (isDigitSumPalindrome(n)) :
    print("true"); 
else :
    print("false"); 

C#

// C# implementation of the approach using System;

class GfG { static int digitSum(int n) { int sum = 0; while (n > 0) { sum += (n % 10); n /= 10; } return sum; }

static bool isPalindrome(int n) {

// Find the appropriate divisor
// to extract the leading digit
int divisor = 1;
while (n / divisor >= 10)
    divisor *= 10;

while (n != 0) {
    int leading = n / divisor;
    int trailing = n % 10;

    // If first and last digit
    // not same return false
    if (leading != trailing)
        return false;

    // Removing the leading and trailing
    // digit from number
    n = (n % divisor) / 10;

    // Reducing divisor by a factor
    // of 2 as 2 digits are dropped
    divisor = divisor / 100;
}
return true;

}

static bool isDigitSumPalindrome(int n) {

// Sum of the digits of n
int sum = digitSum(n);

// If the digit sum is palindrome
if (isPalindrome(sum))
    return true;
return false;

}

static public void Main () { int n = 56;

if (isDigitSumPalindrome(n))
    Console.Write("true");
else
    Console.Write("false");

} }

JavaScript

// Javascript implementation of the approach

function digitSum(n) { 
    let sum = 0;
    while (n > 0) {
        sum += (n % 10);
        n = parseInt(n / 10, 10);
    }
    return sum;
}

function isPalindrome(n)  {

    // Find the appropriate divisor
    // to extract the leading digit
    let divisor = 1;
    while (parseInt(n / divisor, 10) >= 10)
        divisor *= 10;

    while (n != 0) {
        let leading = parseInt(n / divisor, 10);
        let trailing = n % 10;

        // If first and last digit
        // not same return false
        if (leading != trailing)
            return false;

        // Removing the leading and trailing
        // digit from number
        n = parseInt((n % divisor) / 10, 10);

        // Reducing divisor by a factor
        // of 2 as 2 digits are dropped
        divisor = parseInt(divisor / 100, 10);
    }
    return true;
}

function isDigitSumPalindrome(n) {

    // Sum of the digits of n
    let sum = digitSum(n);

    // If the digit sum is palindrome
    if (isPalindrome(sum))
        return true;
    return false;
}

//driver code
let n = 56;

if (isDigitSumPalindrome(n))
    console.log("true");
else
    console.log("false");

`

**Time Complexity: O(log n), iterates through each digit
**Auxiliary Space: O(1)

[Alternate Approach] Coverting to String - O(n) Time and O(1) Space

The idea is to find the sum of digits of **n and store it in a variable **sum and then convert the **sum into a string say **s1 and check whether reversing the string **s1 is equal to **s1, If yes, then this sum of the digit of the given number **n is palindrome otherwise not.

C++ `

// C++ code to implement the approach #include <bits/stdc++.h> using namespace std;

bool isDigitSumPalindrome(int n) { int sum = 0;

while (n != 0) {
    int temp = (n % 10);
    sum = sum + temp;
    n /= 10;
}

// convert sum to string
string s = to_string(sum);

// reverse the string
string stringRev = "" + s;
reverse(stringRev.begin(), stringRev.end());

 return s==stringRev?true:false;

}

int main() { int num = 56; cout << ((isDigitSumPalindrome(num)) ? "true" : "false"); }

C

// C code to implement the approach #include <stdio.h>

int isDigitSumPalindrome(int n) { int sum = 0;

while (n != 0) {
    int temp = (n % 10);
    sum = sum + temp;
    n /= 10;
}

// convert sum to string
char s[20];
sprintf(s, "%d", sum);

// reverse the string
int len = strlen(s);
for (int i = 0; i < len / 2; i++) {
    char temp = s[i];
    s[i] = s[len - i - 1];
    s[len - i - 1] = temp;
}

return strcmp(s, s) == 0;

}

int main() { int num = 56; printf("%s", isDigitSumPalindrome(num) ? "true" : "false"); }

Java

// Java code to implement the approach class GfG { static boolean isDigitSumPalindrome(int n) { int sum = 0;

    while (n != 0) {
        int temp = (n % 10);
        sum += temp;
        n /= 10;
    }

    // convert sum to string
    String s = Integer.toString(sum);

    // reverse the string
    String stringRev = new StringBuilder(s).reverse().toString();

    return s.equals(stringRev);
}

public static void main(String[] args) {
    int num = 56;
    System.out.println(isDigitSumPalindrome(num) ? "true" : "false");
}

}

Python

#python code to implement the approach def isDigitSumPalindrome(n): sum = 0

while n != 0:
    temp = (n % 10)
    sum += temp
    n //= 10

# convert sum to string
s = str(sum)

# reverse the string
string_rev = s[::-1]

return s == string_rev

if name == "main" : num = 56 print("true" if isDigitSumPalindrome(num) else "false")

C#

using System;

class GfG { static bool IsDigitSumPalindrome(int n) { int sum = 0;

    while (n != 0) {
        int temp = (n % 10);
        sum += temp;
        n /= 10;
    }

    // convert sum to string
    string s = sum.ToString();

    // reverse the string
    char[] charArray = s.ToCharArray();
    Array.Reverse(charArray);
    string stringRev = new string(charArray);

    return s == stringRev;
}

static void Main() {
    int num = 56;
    Console.WriteLine(IsDigitSumPalindrome(num) ? "true" : "false");
}

}

JavaScript

function isDigitSumPalindrome(n) { let sum = 0;

while (n !== 0) {
    let temp = (n % 10);
    sum += temp;
    n = Math.floor(n / 10);
}

// convert sum to string
let s = sum.toString();

// reverse the string
let stringRev = s.split('').reverse().join('');

return s === stringRev;

}

//Driver code let num = 56; console.log(isDigitSumPalindrome(num) ? "true" : "false");

`

**Time Complexity: O(n)
**Auxiliary Space: O(1)