# Sum of all odd length palindromic numbers within the range [L, R]

Given two integers and , the task is to find the sum of all the palindromic numbers within the range [L, R] which are of odd length.

Examples:

Input: L = 10, R = 130
Output: 333
101 + 111 + 121 = 333

Input: L = 110, R = 1130
Output: 49399

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Approach: Iterate from to and for every number check whether it is a palindrome and of odd length. If yes then add it to the sum. Finally print the value of the sum in the end.

Below is the implementation of the above approach:

## C

 `// C program to find the sum of all odd length ` `// palindromic numbers within the given range ` `#include ` `#include ` ` `  `// Function that returns true if ` `// the given number is a palindrome ` `bool` `isPalindrome(``int` `num) ` `{ ` `    ``int` `reverse_num = 0, remainder, temp; ` ` `  `    ``/* Here we are generating a new number (reverse_num) ` `     ``* by reversing the digits of original input number ` `     ``*/` `    ``temp = num; ` `    ``while` `(temp != 0) { ` `        ``remainder = temp % 10; ` `        ``reverse_num = reverse_num * 10 + remainder; ` `        ``temp /= 10; ` `    ``} ` ` `  `    ``/* If the original input number (num) is equal to ` `     ``* to its reverse (reverse_num) then its palindrome ` `     ``* else it is not. ` `     ``*/` `    ``if` `(reverse_num == num) { ` `        ``return` `true``; ` `    ``} ` ` `  `    ``return` `false``; ` `} ` ` `  `// Function that returns true if the ` `// given number is of odd length ` `bool` `isOddLength(``int` `num) ` `{ ` `    ``int` `count = 0; ` `    ``while` `(num > 0) { ` `        ``num /= 10; ` `        ``count++; ` `    ``} ` `    ``if` `(count % 2 != 0) { ` `        ``return` `true``; ` `    ``} ` `    ``return` `false``; ` `} ` ` `  `// Function to return the sum of all odd length ` `// palindromic numbers within the given range ` `long` `sumOfAllPalindrome(``int` `L, ``int` `R) ` `{ ` `    ``long` `sum = 0; ` `    ``if` `(L <= R) ` `        ``for` `(``int` `i = L; i <= R; i++) { ` ` `  `            ``// if number is palindrome and of odd length ` `            ``if` `(isPalindrome(i) && isOddLength(i)) { ` `                ``sum += i; ` `            ``} ` `        ``} ` `    ``return` `sum; ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` `    ``int` `L = 110, R = 1130; ` `    ``printf``(``"%ld"``, sumOfAllPalindrome(L, R)); ` `} `

## Java

 `// Java program to find the sum of all odd length ` `// palindromic numbers within the given range ` ` `  `class` `GFG { ` ` `  `    ``// Function that returns true if ` `    ``// the given number is a palindrome ` `    ``static` `boolean` `isPalindrome(``int` `num) ` `    ``{ ` `        ``int` `reverse_num = ``0``, remainder, temp; ` ` `  `        ``/* Here we are generating a new number (reverse_num) ` `    ``* by reversing the digits of original input number ` `         ``*/` `        ``temp = num; ` `        ``while` `(temp != ``0``) { ` `            ``remainder = temp % ``10``; ` `            ``reverse_num = reverse_num * ``10` `+ remainder; ` `            ``temp /= ``10``; ` `        ``} ` ` `  `        ``/* If the original input number (num) is equal to ` `    ``* to its reverse (reverse_num) then its palindrome ` `    ``* else it is not. ` `         ``*/` `        ``if` `(reverse_num == num) { ` `            ``return` `true``; ` `        ``} ` ` `  `        ``return` `false``; ` `    ``} ` ` `  `    ``// Function that returns true if the ` `    ``// given number is of odd length ` `    ``static` `boolean` `isOddLength(``int` `num) ` `    ``{ ` `        ``int` `count = ``0``; ` `        ``while` `(num > ``0``) { ` `            ``num /= ``10``; ` `            ``count++; ` `        ``} ` `        ``if` `(count % ``2` `!= ``0``) { ` `            ``return` `true``; ` `        ``} ` `        ``return` `false``; ` `    ``} ` ` `  `    ``// Function to return the sum of all odd length ` `    ``// palindromic numbers within the given range ` `    ``static` `long` `sumOfAllPalindrome(``int` `L, ``int` `R) ` `    ``{ ` `        ``long` `sum = ``0``; ` `        ``if` `(L <= R) ` `            ``for` `(``int` `i = L; i <= R; i++) { ` ` `  `                ``// if number is palindrome and of odd length ` `                ``if` `(isPalindrome(i) && isOddLength(i)) { ` `                    ``sum += i; ` `                ``} ` `            ``} ` `        ``return` `sum; ` `    ``} ` ` `  `    ``// Driver code ` `    ``public` `static` `void` `main(String[] args) ` `    ``{ ` `        ``int` `L = ``110``, R = ``1130``; ` `        ``System.out.println(sumOfAllPalindrome(L, R)); ` `    ``} ` `} `

## Python3

 `# Python 3 program to find the sum of  ` `# all odd length palindromic numbers ` `# within the given range ` ` `  `# Function that returns true if ` `# the given number is a palindrome ` `def` `isPalindrome(num): ` `    ``reverse_num ``=` `0` `     `  `    ``# Here we are generating a new number  ` `    ``# (reverse_num) by reversing the digits  ` `    ``# of original input number ` `    ``temp ``=` `num ` `    ``while` `(temp !``=` `0``): ` `        ``remainder ``=` `temp ``%` `10` `        ``reverse_num ``=` `reverse_num ``*` `10` `+` `remainder ` `        ``temp ``=` `int``(temp``/``10``) ` ` `  `    ``# If the original input number (num) is  ` `    ``# equal to its reverse (reverse_num) then  ` `    ``# its palindrome else it is not. ` `    ``if` `(reverse_num ``=``=` `num): ` `        ``return` `True` ` `  `    ``return` `False` ` `  `# Function that returns true if the given  ` `# number is of odd length ` `def` `isOddLength(num): ` `    ``count ``=` `0` `    ``while` `(num > ``0``): ` `        ``num ``=` `int` `(num ``/` `10``) ` `        ``count ``+``=` `1` ` `  `    ``if` `(count ``%` `2` `!``=` `0``): ` `        ``return` `True` ` `  `    ``return` `False` ` `  `# Function to return the sum of all odd length ` `# palindromic numbers within the given range ` `def` `sumOfAllPalindrome(L, R): ` `    ``sum` `=` `0` `    ``if` `(L <``=` `R): ` `        ``for` `i ``in` `range``(L, R ``+` `1``, ``1``): ` `             `  `            ``# if number is palindrome and of  ` `            ``# odd length ` `            ``if` `(isPalindrome(i) ``and` `isOddLength(i)): ` `                ``sum` `+``=` `i ` `             `  `    ``return` `sum` ` `  `# Driver code ` `if` `__name__ ``=``=` `'__main__'``: ` `    ``L ``=` `110` `    ``R ``=` `1130` `    ``print``(sumOfAllPalindrome(L, R)) ` ` `  `# This code is contributed by ` `# Shashank_Sharma `

## C#

 `// C# program to find the sum of all odd length ` `// palindromic numbers within the given range ` `using` `System; ` ` `  `public` `class` `GFG { ` ` `  `    ``// Function that returns true if ` `    ``// the given number is a palindrome ` `    ``static` `bool` `isPalindrome(``int` `num) ` `    ``{ ` `        ``int` `reverse_num = 0, remainder, temp; ` ` `  `        ``/* Here we are generating a new number (reverse_num) ` `    ``* by reversing the digits of original input number ` `        ``*/` `        ``temp = num; ` `        ``while` `(temp != 0) { ` `            ``remainder = temp % 10; ` `            ``reverse_num = reverse_num * 10 + remainder; ` `            ``temp /= 10; ` `        ``} ` ` `  `        ``/* If the original input number (num) is equal to ` `    ``* to its reverse (reverse_num) then its palindrome ` `    ``* else it is not. ` `        ``*/` `        ``if` `(reverse_num == num) { ` `            ``return` `true``; ` `        ``} ` ` `  `        ``return` `false``; ` `    ``} ` ` `  `    ``// Function that returns true if the ` `    ``// given number is of odd length ` `    ``static` `bool` `isOddLength(``int` `num) ` `    ``{ ` `        ``int` `count = 0; ` `        ``while` `(num > 0) { ` `            ``num /= 10; ` `            ``count++; ` `        ``} ` `        ``if` `(count % 2 != 0) { ` `            ``return` `true``; ` `        ``} ` `        ``return` `false``; ` `    ``} ` ` `  `    ``// Function to return the sum of all odd length ` `    ``// palindromic numbers within the given range ` `    ``static` `long` `sumOfAllPalindrome(``int` `L, ``int` `R) ` `    ``{ ` `        ``long` `sum = 0; ` `        ``if` `(L <= R) ` `            ``for` `(``int` `i = L; i <= R; i++) { ` ` `  `                ``// if number is palindrome and of odd length ` `                ``if` `(isPalindrome(i) && isOddLength(i)) { ` `                    ``sum += i; ` `                ``} ` `            ``} ` `        ``return` `sum; ` `    ``} ` ` `  `    ``// Driver code ` `    ``public` `static` `void` `Main(String[] args) ` `    ``{ ` `        ``int` `L = 110, R = 1130; ` `        ``Console.WriteLine(sumOfAllPalindrome(L, R)); ` `    ``} ` `} `

## PHP

 ` 0)  ` `    ``{ ` `        ``\$num` `= (int)(``\$num` `/ 10); ` `        ``\$count``++; ` `    ``} ` `    ``if` `(``\$count` `% 2 != 0)  ` `    ``{ ` `        ``return` `true; ` `    ``} ` `    ``return` `false; ` `} ` ` `  `// Function to return the sum of  ` `// all odd length palindromic numbers  ` `// within the given range ` `function` `sumOfAllPalindrome(``\$L``, ``\$R``) ` `{ ` `    ``\$sum` `= 0; ` `    ``if` `(``\$L` `<= ``\$R``) ` `        ``for` `(``\$i` `= ``\$L``; ``\$i` `<= ``\$R``; ``\$i``++)  ` `        ``{ ` ` `  `            ``// if number is palindrome and  ` `            ``// of odd length ` `            ``if` `(isPalindrome(``\$i``) && isOddLength(``\$i``)) ` `            ``{ ` `                ``\$sum` `+= ``\$i``; ` `            ``} ` `        ``} ` `    ``return` `\$sum``; ` `} ` ` `  `// Driver code ` `\$L` `= 110; ` `\$R` `= 1130; ` `echo` `sumOfAllPalindrome(``\$L``, ``\$R``); ` ` `  `// This code is contributed by mits ` `?> `

Output:

```49399
```

GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details

My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.