Check if the sum of digits of N is palindrome
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
Explanation: Digit sum is (5 + 6) = 11, which is a palindrome.Input: N = 51241
Output: No
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++
// 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
// 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
# 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#
// 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 |
Javascript
<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)
Another Approach: 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;int main(){ int num = 56; int sum = 0; while (num != 0) { int temp = (num % 10); sum = sum + temp; num /= 10; } // Driver Code // convert sum to string string str = to_string(sum); // reverse the string string string_rev = "" + str; reverse(string_rev.begin(), string_rev.end()); // check the palindrome cout << ((str == string_rev) ? "Yes" : "No");}// This code is contributed by phasing17 |
Java
/*package whatever //do not write package name here */import java.io.*;class GFG { public static void main(String[] args) { int n = 56; int sum = 0; while (n != 0) { int temp = n % 10; // Addition of digits sum = sum + temp; n = n / 10; } // Converting int sum to string String str = String.valueOf(sum); // Reverse the string String rev = new StringBuilder(str).reverse().toString(); if (str.equals(rev)) { System.out.println("Yes"); } else { System.out.println("No"); } }}// This code is contributed by keerthikarathan123 |
Python3
# codenum = int(56)sum = int(0)while num != 0: temp = int(num % 10) sum = sum+temp num = num/10# convert sum to stringstring = str(sum)# reverse the stringstring_rev = string[:: -1]# check the palindromeif string == string_rev: print("Yes")else: print("No")# This code is contributed by keerthikarathan123 |
C#
// C# code to implement the approachusing System;using System.Text;using System.Collections.Generic;class GFG { public static string Reverse( string s ) { char[] charArray = s.ToCharArray(); Array.Reverse( charArray ); return new string( charArray ); } public static void Main(string[] args) { int n = 56; int sum = 0; while (n != 0) { int temp = n % 10; // Addition of digits sum = sum + temp; n = n / 10; } // Converting int sum to string string str = Convert.ToString(sum); // Reverse the string string rev = Reverse(str); if (str.Equals(rev)) Console.WriteLine("Yes"); else { Console.WriteLine("No"); } }}// This code is contributed by phasing17 |
Javascript
// JS code to implement the approachlet num = 56let sum = 0while(num != 0){ let temp = (num % 10) sum = sum+temp num = Math.floor(num/10)}// Driver Code// convert sum to stringlet string = "" + sum// reverse the stringlet string_rev = string.split("").reverse().join("")// check the palindromeconsole.log( (string == string_rev) ? "Yes" : "No") // This code is contributed by phasing17 |
Yes
Time Complexity: O(N)
Auxiliary Space: O(1)


.png)


Please Login to comment...