# Sum of first K even-length Palindrome numbers

• Difficulty Level : Easy
• Last Updated : 28 May, 2021

Given a integer k, find the sum of first k even-length palindrome numbers.
Even length here refers to the number of digits of a number is even.
Examples:

```Input : k = 3
Output : 66
Explanation: 11 + 22 + 33  = 66 (Sum
of first three even-length palindrome
numbers)

Input : 10
Output : 1496
Explanation: 11+22+33+44+55+66+77+88+
99+1001 = 1496```

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.

A naive approach will be to check every even length number, if it is a palindrome number then we sum it up. We repeat the same process for first K even length palindrome numbers and sum them up to get the sum.
In this case complexity will go high as even length numbers are from 10-99 and then 1000-9999 and then so on…
10-99, 1000-9999, 100000-999999.. has 9, 90, 900 respectively palindrome numbers in them, so to check k numbers we have to check a lot of numbers which will not be efficient enough.
An efficient approach will be to observe a pattern for even length prime numbers.

11, 22, 33, 44, 55, 66, 77, 88, 99, 1001, 1111, 1221, 1331, 1441, 1551, 1661…

1st number is 11, 2nd is 22, third is 33, 16th is 16-rev(16) i.e., 1661
So the Nth number will int(string(n)+rev(string(n)).
See here for conversion of integer to string and string to integer.
Below is the implementation of the above approach:

## C++

 `#include ``#include ``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

 ``

Output:

`66`

My Personal Notes arrow_drop_up