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++ implementation of the approach #include <bits/stdc++.h> using namespace std;
// Function to return the // sum of digits of n int digitSum( int n)
{ int sum = 0;
while (n > 0) {
sum += (n % 10);
n /= 10;
}
return sum;
} // Function that returns true // if n is palindrome 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 palindrome 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 code int main()
{ int n = 56;
if (isDigitSumPalindrome(n))
cout << "Yes" ;
else
cout << "No" ;
return 0;
} |
// Java implementation of the approach import java.util.*;
class GFG
{ // Function to return the // sum of digits of n static int digitSum( int n)
{ int sum = 0 ;
while (n > 0 )
{
sum += (n % 10 );
n /= 10 ;
}
return sum;
} // Function that returns true // if n is palindrome 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 ;
} // Function that returns true if // the digit sum of n is palindrome 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 ;
} // Driver code public 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 n def digitSum(n) :
sum = 0 ;
while (n > 0 ) :
sum + = (n % 10 );
n / / = 10 ;
return sum ;
# Function that returns true # if n is palindrome 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 ;
# Function that returns true if # the digit sum of n is palindrome 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 ;
# Driver code if __name__ = = "__main__" :
n = 56 ;
if (isDigitSumPalindrome(n)) :
print ( "Yes" );
else :
print ( "No" );
# This code is contributed by AnkitRai01 |
// C# implementation of the approach using System;
class GFG
{ // Function to return the // sum of digits of n static int digitSum( int n)
{ int sum = 0;
while (n > 0)
{
sum += (n % 10);
n /= 10;
}
return sum;
} // Function that returns true // if n is palindrome 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 ;
} // Function that returns true if // the digit sum of n is palindrome 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 ;
} // Driver code static 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)
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++ 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 |
/*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 |
# code num = int ( 56 )
sum = int ( 0 )
while num ! = 0 :
temp = int (num % 10 )
sum = sum + temp
num = num / 10
# convert sum to string string = str ( sum )
# reverse the string string_rev = string[:: - 1 ]
# check the palindrome if string = = string_rev:
print ( "Yes" )
else :
print ( "No" )
# This code is contributed by keerthikarathan123 |
// C# code to implement the approach using 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 |
// JS code to implement the approach let num = 56 let sum = 0 while (num != 0)
{ let temp = (num % 10)
sum = sum+temp
num = Math.floor(num/10)
} // Driver Code // convert sum to string let string = "" + sum
// reverse the string let string_rev = string.split( "" ).reverse().join( "" )
// check the palindrome console.log( (string == string_rev) ? "Yes" : "No" )
// This code is contributed by phasing17 |
Yes
Time Complexity: O(N)
Auxiliary Space: O(1)