Suma de los primeros K números palíndromos de longitud par

Dado un entero k, encuentre la suma de los primeros k números  palíndromos de longitud par.
Incluso la longitud aquí se refiere a que el número de dígitos de un número es par.

Ejemplos: 

C++

#include <bits/stdc++.h>
#include <boost/lexical_cast.hpp>
using namespace std;
 
// function to return the sum of
// first K even length palindrome numbers
int sum(int k)
{
    // loop to get sum of first K even
    // palindrome numbers
    int sum = 0;
    for (int i = 1; i <= k; i++) {
 
        // convert integer to string
        string num = to_string(i);
 
        // Find reverse of num.
        string revNum = num;
        reverse(revNum.begin(), revNum.end());
 
        // string(n)+rev(string(n)
        string strnum = (num + revNum);
 
        // convert string to integer
        int number = boost::lexical_cast<int>(strnum);
 
        sum += number; // summation
    }
    return sum;
}
// driver program to check the above function
int main()
{
    int k = 3;
    cout << sum(k);
    return 0;
}

Java

// Java implementation to find sum of
// first K even-length Palindrome numbers
import java.util.*;
import java.lang.*;
 
public class GfG{
 
public static String reverseString(String str)
{
    StringBuilder sb = new StringBuilder(str);   
    sb.reverse();  
    return sb.toString();
}
 
// function to return the sum of
// first K even length palindrome numbers
static int sum(int k)
{
    // loop to get sum of first K even
    // palindrome numbers
    int sum = 0;
    for (int i = 1; i <= k; i++) {
 
    // convert integer to string
    String num = Integer.toString(i);
 
    // Find reverse of num.
    String revNum = num;
    revNum = reverseString(num);
 
    // string(n)+rev(string(n)
    String strnum = (num + revNum);
 
    // convert string to integer
    int number = Integer.parseInt(strnum);
 
    sum += number; // summation
    }
     
    return sum;
}
 
// driver function
public static void main(String argc[])
{
    int n = 3;
    System.out.println(sum(n));
}
}
 
// This code is contributed by Prerna Saini

Python3

# Python3 implementation of the approach
 
# function to return the sum of
# first K even length palindrome numbers
def summ(k):
 
    # loop to get sum of first K even
    # palindrome numbers
    sum = 0
    for i in range(1, k + 1):
 
        # convert integer to string
        num = str(i)
 
        # Find reverse of num.
        revNum = num
        revNum = ''.join(reversed(revNum))
 
        # string(n)+rev(string(n)
        strnum = num + revNum
 
        # convert string to integer
        number = int(strnum)
 
        sum += number # summation
 
    return sum
 
# Driver Code
if __name__ == "__main__":
    k = 3
    print(summ(k))
 
# This code is contributed by
# sanjeev2552

C#

// C# implementation to find sum of
// first K even-length Palindrome numbers
using System;
 
class GfG
{
     
    // function to return the sum of
    // first K even length palindrome numbers
    static int sum(int k)
    {
         
        // loop to get sum of first K even
        // palindrome numbers
        int sum = 0;
        for (int i = 1; i <= k; i++)
        {
 
            // convert integer to string
            String num = Convert.ToString(i);
 
            // Find reverse of num.
            String revNum = num;
            revNum = reverse(num);
 
            // string(n)+rev(string(n)
            String strnum = (num + revNum);
 
            // convert string to integer
            int number = Convert.ToInt32(strnum);
 
            sum += number; // summation
        }
 
        return sum;
    }
     
    static String reverse(String input)
    {
        char[] temparray = input.ToCharArray();
        int left, right = 0;
        right = temparray.Length - 1;
 
        for (left = 0; left < right; left++, right--)
        {
             
            // Swap values of left and right
            char temp = temparray[left];
            temparray[left] = temparray[right];
            temparray[right] = temp;
        }
        return String.Join("",temparray);
    }
     
    // Driver code
    public static void Main(String []argc)
    {
        int n = 3;
        Console.WriteLine(sum(n));
    }
}
 
// This code is contributed by PrinciRaj1992

Javascript

<script>
 
 
// function to return the sum of
// first K even length palindrome numbers
function sum(k)
{
    // loop to get sum of first K even
    // palindrome numbers
    var sum = 0;
    for (var i = 1; i <= k; i++) {
 
        // convert integer to string
        var num = (i.toString());
 
        // Find reverse of num.
        var revNum = num;
        revNum = revNum.split('').reverse().join('');
 
        // string(n)+rev(string(n)
        var strnum = (num + revNum);
 
        // convert string to integer
        var number = parseInt(strnum);
 
        sum += number; // summation
    }
    return sum;
}
 
 
// driver program to check the above function
var k = 3;
document.write(sum(k));
 
</script>

Publicación traducida automáticamente

Artículo escrito por Striver 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 *