Check if the sum of digits of N is palindrome - GeeksforGeeks

04 Aug, 2021
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: Yes Digit sum is (5 + 6) = 11 which is a palindrome.Input: N = 51241 Output: No (5 + 1 + 2 + 4 + 1) = 13  
 
Approach: Find the sum of digits of N and store it in a variable sum. Now check whether sum is palindrome or not using the approach discussed in this article.Below is the implementation of the above approach:  
C++
Java
Python3
C#
Javascript
// C++ implementation of the approach#include <bits/stdc++.h>using namespace std; // Function to return the// sum of digits of nint digitSum(int n){    int sum = 0;    while (n > 0) {        sum += (n % 10);        n /= 10;    }    return sum;} // Function that returns true// if n is palindromebool 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;} // Function that returns true if// the digit sum of n is palindromebool 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;} // Driver codeint main(){    int n = 56;     if (isDigitSumPalindrome(n))        cout << "Yes";    else        cout << "No";     return 0;}
// Java implementation of the approachimport java.util.*; class GFG{ // Function to return the// sum of digits of nstatic int digitSum(int n){    int sum = 0;    while (n > 0)    {        sum += (n % 10);        n /= 10;    }    return sum;} // Function that returns true// if n is palindromestatic 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;} // Function that returns true if// the digit sum of n is palindromestatic 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;} // Driver codepublic static void main(String []args){    int n = 56;     if (isDigitSumPalindrome(n))        System.out.println("Yes");    else        System.out.println("No");}} // This code is contributed by Surendra_Gangwar
# Python3 implementation of the approach # Function to return the# sum of digits of ndef digitSum(n) :     sum = 0;    while (n > 0) :        sum += (n % 10);        n //= 10;     return sum; # Function that returns true# if n is palindromedef 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; # Function that returns true if# the digit sum of n is palindromedef isDigitSumPalindrome(n) :     # Sum of the digits of n    sum = digitSum(n);     # If the digit sum is palindrome    if (isPalindrome(sum)) :        return True;    return False; # Driver codeif __name__ == "__main__" :     n = 56;     if (isDigitSumPalindrome(n)) :        print("Yes");    else :        print("No"); # This code is contributed by AnkitRai01
// C# implementation of the approachusing System; class GFG{     // Function to return the// sum of digits of nstatic int digitSum(int n){    int sum = 0;    while (n > 0)    {        sum += (n % 10);        n /= 10;    }    return sum;} // Function that returns true// if n is palindromestatic 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;} // Function that returns true if// the digit sum of n is palindromestatic 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;} // Driver codestatic public void Main (){    int n = 56;     if (isDigitSumPalindrome(n))        Console.Write("Yes");    else        Console.Write("No");}} // This code is contributed by ajit
<script>    // Javascript implementation of the approach         // Function to return the    // sum of digits of n    function digitSum(n)    {        let sum = 0;        while (n > 0)        {            sum += (n % 10);            n = parseInt(n / 10, 10);        }        return sum;    }     // Function that returns true    // if n is palindrome    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 that returns true if    // the digit sum of n is palindrome    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;    }         let n = 56;       if (isDigitSumPalindrome(n))        document.write("Yes");    else        document.write("No"); // This code is contributed by suresh07.</script>
Yes
 
Time Complexity: O(logN)Auxiliary Space: O(1)
ankthon
SURENDRA_GANGWAR
jit_t
suresh07
pankajsharmagfg
Numbers
palindrome
Algorithms
Mathematical
Technical Scripter
Mathematical
Numbers
palindrome
Algorithms
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
SDE SHEET - A Complete Guide for SDE Preparation
DSA Sheet by Love Babbar
How to write a Pseudo Code?
How to Start Learning DSA?
Understanding Time Complexity with Simple Examples
Write a program to print all permutations of a given string
C++ Data Types
Set in C++ Standard Template Library (STL)
Merge two sorted arrays
Modulo Operator (%) in C/C++ with Examples