Comprobar si la suma de dígitos de N es palíndromo

Dado un número entero N , la tarea es verificar si la suma de los dígitos de N es palíndromo o no.
Ejemplo: 

Entrada: N = 56 
Salida: Sí 
Explicación: La suma de dígitos es (5 + 6) = 11, que es un palíndromo.

Entrada: N = 51241 
Salida: No 

Enfoque: Encuentre la suma de los dígitos de N y guárdela en una suma variable . Ahora verifique si sum es palíndromo o no usando el enfoque discutido en este artículo.
A continuación se muestra la implementación del enfoque anterior: 

C++

// 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

// 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

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>
Producción

Yes

Complejidad temporal: O(logN)
Espacio auxiliar: O(1)

Otro enfoque: la idea es encontrar la suma de los dígitos de N y almacenarla en una suma variable y luego convertir la suma en una string, digamos s1 y verificar si invertir la string s1 es igual a s1. En caso afirmativo, entonces esta suma de el dígito del número dado N es palíndromo, de lo contrario no lo es.

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

# 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
Producción

Yes

Complejidad temporal: O(N)
Espacio auxiliar: O(1)

Publicación traducida automáticamente

Artículo escrito por akshara_a y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *