📌  相关文章
📜  检查N的数字总和是否是回文

📅  最后修改于: 2021-04-29 07:49:26             🧑  作者: Mango

给定整数N ,任务是检查N的数字总和是否是回文。

例子:

方法:找到N的数字总和并将其存储在变量sum中。现在,使用本文讨论的方法检查sum是否为回文。

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
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
// 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
# 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#
// 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


输出:
Yes