Check if the sum of digits of N is palindrome
Last Updated :
26 Nov, 2022
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++
#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)
{
int divisor = 1;
while (n / divisor >= 10)
divisor *= 10;
while (n != 0) {
int leading = n / divisor;
int trailing = n % 10;
if (leading != trailing)
return false ;
n = (n % divisor) / 10;
divisor = divisor / 100;
}
return true ;
}
bool isDigitSumPalindrome( int n)
{
int sum = digitSum(n);
if (isPalindrome(sum))
return true ;
return false ;
}
int main()
{
int n = 56;
if (isDigitSumPalindrome(n))
cout << "Yes" ;
else
cout << "No" ;
return 0;
}
|
Java
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)
{
int divisor = 1 ;
while (n / divisor >= 10 )
divisor *= 10 ;
while (n != 0 )
{
int leading = n / divisor;
int trailing = n % 10 ;
if (leading != trailing)
return false ;
n = (n % divisor) / 10 ;
divisor = divisor / 100 ;
}
return true ;
}
static boolean isDigitSumPalindrome( int n)
{
int sum = digitSum(n);
if (isPalindrome(sum))
return true ;
return false ;
}
public static void main(String []args)
{
int n = 56 ;
if (isDigitSumPalindrome(n))
System.out.println( "Yes" );
else
System.out.println( "No" );
}
}
|
Python3
def digitSum(n) :
sum = 0 ;
while (n > 0 ) :
sum + = (n % 10 );
n / / = 10 ;
return sum ;
def isPalindrome(n) :
divisor = 1 ;
while (n / / divisor > = 10 ) :
divisor * = 10 ;
while (n ! = 0 ) :
leading = n / / divisor;
trailing = n % 10 ;
if (leading ! = trailing) :
return False ;
n = (n % divisor) / / 10 ;
divisor = divisor / / 100 ;
return True ;
def isDigitSumPalindrome(n) :
sum = digitSum(n);
if (isPalindrome( sum )) :
return True ;
return False ;
if __name__ = = "__main__" :
n = 56 ;
if (isDigitSumPalindrome(n)) :
print ( "Yes" );
else :
print ( "No" );
|
C#
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)
{
int divisor = 1;
while (n / divisor >= 10)
divisor *= 10;
while (n != 0)
{
int leading = n / divisor;
int trailing = n % 10;
if (leading != trailing)
return false ;
n = (n % divisor) / 10;
divisor = divisor / 100;
}
return true ;
}
static bool isDigitSumPalindrome( int n)
{
int sum = digitSum(n);
if (isPalindrome(sum))
return true ;
return false ;
}
static public void Main ()
{
int n = 56;
if (isDigitSumPalindrome(n))
Console.Write( "Yes" );
else
Console.Write( "No" );
}
}
|
Javascript
<script>
function digitSum(n)
{
let sum = 0;
while (n > 0)
{
sum += (n % 10);
n = parseInt(n / 10, 10);
}
return sum;
}
function isPalindrome(n)
{
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 (leading != trailing)
return false ;
n = parseInt((n % divisor) / 10, 10);
divisor = parseInt(divisor / 100, 10);
}
return true ;
}
function isDigitSumPalindrome(n)
{
let sum = digitSum(n);
if (isPalindrome(sum))
return true ;
return false ;
}
let n = 56;
if (isDigitSumPalindrome(n))
document.write( "Yes" );
else
document.write( "No" );
</script>
|
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++
#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;
}
string str = to_string(sum);
string string_rev = "" + str;
reverse(string_rev.begin(), string_rev.end());
cout << ((str == string_rev) ? "Yes" : "No" );
}
|
Java
import java.io.*;
class GFG {
public static void main(String[] args)
{
int n = 56 ;
int sum = 0 ;
while (n != 0 ) {
int temp = n % 10 ;
sum = sum + temp;
n = n / 10 ;
}
String str = String.valueOf(sum);
String rev
= new StringBuilder(str).reverse().toString();
if (str.equals(rev)) {
System.out.println( "Yes" );
}
else {
System.out.println( "No" );
}
}
}
|
Python3
num = int ( 56 )
sum = int ( 0 )
while num ! = 0 :
temp = int (num % 10 )
sum = sum + temp
num = num / 10
string = str ( sum )
string_rev = string[:: - 1 ]
if string = = string_rev:
print ( "Yes" )
else :
print ( "No" )
|
C#
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;
sum = sum + temp;
n = n / 10;
}
string str = Convert.ToString(sum);
string rev = Reverse(str);
if (str.Equals(rev))
Console.WriteLine( "Yes" );
else {
Console.WriteLine( "No" );
}
}
}
|
Javascript
let num = 56
let sum = 0
while (num != 0)
{
let temp = (num % 10)
sum = sum+temp
num = Math.floor(num/10)
}
let string = "" + sum
let string_rev = string.split( "" ).reverse().join( "" )
console.log( (string == string_rev) ? "Yes" : "No" )
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...