# XOR and OR of all N-digit palindrome number

• Difficulty Level : Medium
• Last Updated : 03 May, 2021

Given an integer N. The task is to find the XOR and OR of all N digit palindromic numbers.
Examples

Input:
Output: XOR = 714 and OR = 1023
Input:
Output: XOR = 4606 and OR = 16383

Approach:

• Find the starting and ending number of N-digit palindromic number by:

```starting number = pow(10, n - 1)
ending number = pow(10, n) - 1```
• Iterate over starting number till ending number and check whether that number is palindromic or not.
• If the number is palindromic, then take XOR and OR of that number separately.
• Else proceed for next iteration and print the value of XOR and OR after all iterations.

Below is the implementation of the above approach:

## C++

 `// C++ program to find the XOR``// and OR of all palindrome numbers``// of N digits``#include ``using` `namespace` `std;`` ` `// Function to check if a number``// is palindrome or not``bool` `ispalin(``int` `num)``{``    ``// Convert the num n to string``    ``string s = to_string(num);`` ` `    ``int` `st = 0, ed = s.size() - 1;`` ` `    ``// Iterate over string to``    ``// check whether it is``    ``// palindromic or not``    ``while` `(st <= ed) {``        ``if` `(s[st] != s[ed])``            ``return` `false``;``        ``st++;``        ``ed--;``    ``}``    ``return` `true``;``}`` ` `// Function to find XOR of all``// N-digits palindrome number``void` `CalculateXORandOR(``int` `n)``{``    ``// To store the XOR and OR of all``    ``// palindromic number``    ``int` `CalculateXOR = 0;``    ``int` `CalculateOR = 0;`` ` `    ``// Starting N-digit``    ``// palindromic number``    ``int` `start = ``pow``(10, n - 1);`` ` `    ``// Ending N-digit``    ``// palindromic number``    ``int` `end = ``pow``(10, n) - 1;`` ` `    ``// Iterate over starting and``    ``/// ending number``    ``for` `(``int` `i = start; i <= end; i++) {`` ` `        ``// To check if i is``        ``// palindromic or not``        ``if` `(ispalin(i)) {``            ``CalculateXOR = CalculateXOR ^ i;``            ``CalculateOR = CalculateOR | i;``        ``}``    ``}`` ` `    ``// Print the XOR and OR of all``    ``// palindromic number``    ``cout << ``"XOR = "` `<< CalculateXOR;``    ``cout << ``" OR = "` `<< CalculateOR;``}`` ` `// Driver Code``int` `main()``{``    ``int` `n = 4;``    ``CalculateXORandOR(n);``    ``return` `0;``}`

## Java

 `// Java program to find the XOR``// and OR of all palindrome numbers``// of N digits``class` `GFG``{`` ` `// Function to check if a number``// is palindrome or not``static` `boolean` `ispalin(``int` `num)``{``    ``// Convert the num n to string``    ``String s = Integer.toString(num);`` ` `    ``int` `st = ``0``, ed = s.length() - ``1``;`` ` `    ``// Iterate over string to``    ``// check whether it is``    ``// palindromic or not``    ``while` `(st <= ed) {``        ``if` `(s.charAt(st) != s.charAt(ed))``            ``return` `false``;``        ``st++;``        ``ed--;``    ``}``    ``return` `true``;``}`` ` `// Function to find XOR of all``// N-digits palindrome number``static` `void` `CalculateXORandOR(``int` `n)``{``    ``// To store the XOR and OR of all``    ``// palindromic number``    ``int` `CalculateXOR = ``0``;``    ``int` `CalculateOR = ``0``;`` ` `    ``// Starting N-digit``    ``// palindromic number``    ``int` `start = (``int``)Math.pow(``10``, n - ``1``);`` ` `    ``// Ending N-digit``    ``// palindromic number``    ``int` `end = (``int``)Math.pow(``10``, n) - ``1``;`` ` `    ``// Iterate over starting and``    ``/// ending number``    ``for` `(``int` `i = start; i <= end; i++) {`` ` `        ``// To check if i is``        ``// palindromic or not``        ``if` `(ispalin(i)) {``            ``CalculateXOR = CalculateXOR ^ i;``            ``CalculateOR = CalculateOR | i;``        ``}``    ``}`` ` `    ``// Print the XOR and OR of all``    ``// palindromic number``    ``System.out.print(``"XOR = "` `+ CalculateXOR);``    ``System.out.println(``" OR = "` `+ CalculateOR);``}`` ` `// Driver Code``public` `static` `void` `main (String[] args) ``{``    ``int` `n = ``4``;``    ``CalculateXORandOR(n);``}`` ` `}`` ` `// This code is contributed by AnkitRai01`

## Python3

 `# Python3 program to find the XOR ``# and OR of all palindrome numbers ``# of N digits `` ` `# Function to check if a number ``# is palindrome or not ``def` `ispalin(num) : `` ` `    ``# Convert the num n to string ``    ``s ``=` `str``(num); `` ` `    ``st ``=` `0``;``    ``ed ``=` `len``(s) ``-` `1``; `` ` `    ``# Iterate over string to ``    ``# check whether it is ``    ``# palindromic or not ``    ``while` `(st <``=` `ed) :``        ``if` `(s[st] !``=` `s[ed]) :``            ``return` `False``; ``        ``st ``+``=` `1``; ``        ``ed ``-``=` `1``; ``     ` `    ``return` `True``; `` ` `# Function to find XOR of all ``# N-digits palindrome number ``def` `CalculateXORandOR(n) : `` ` `    ``# To store the XOR and OR of all ``    ``# palindromic number ``    ``CalculateXOR ``=` `0``; ``    ``CalculateOR ``=` `0``; `` ` `    ``# Starting N-digit ``    ``# palindromic number ``    ``start ``=` `10` `*``*` `(n ``-` `1``); `` ` `    ``# Ending N-digit ``    ``# palindromic number ``    ``end ``=` `(``10``*``*``n) ``-` `1``; `` ` `    ``# Iterate over starting and ``    ``# ending number ``    ``for` `i ``in` `range``( start, end ``+` `1``) :`` ` `        ``# To check if i is ``        ``# palindromic or not ``        ``if` `(ispalin(i)) :``            ``CalculateXOR ``=` `CalculateXOR ^ i; ``            ``CalculateOR ``=` `CalculateOR | i; `` ` `    ``# Print the XOR and OR of all ``    ``# palindromic number ``    ``print``(``"XOR ="``, CalculateXOR,end ``=` `" "``); ``    ``print``(``"OR = "``, CalculateOR); `` ` `# Driver Code ``if` `__name__ ``=``=` `"__main__"` `: `` ` `    ``n ``=` `4``;``    ``CalculateXORandOR(n); `` ` `# This code is contributed by AnkitRai01`

## C#

 `// C# program to find the XOR``// and OR of all palindrome numbers``// of N digits`` ` `using` `System;`` ` `class` `GFG``{`` ` `// Function to check if a number``// is palindrome or not``static` `bool` `ispalin(``int` `num)``{``    ``// Convert the num n to string``    ``string` `s = num.ToString();`` ` `    ``int` `st = 0;``    ``int` `ed = s.Length - 1;`` ` `    ``// Iterate over string to``    ``// check whether it is``    ``// palindromic or not``    ``while` `(st <= ed) {``        ``if` `(s[st] != s[ed])``            ``return` `false``;``        ``st++;``        ``ed--;``    ``}``    ``return` `true``;``}`` ` `// Function to find XOR of all``// N-digits palindrome number``static` `void` `CalculateXORandOR(``int` `n)``{``    ``// To store the XOR and OR of all``    ``// palindromic number``    ``int` `CalculateXOR = 0;``    ``int` `CalculateOR = 0;`` ` `    ``// Starting N-digit``    ``// palindromic number``    ``int` `start = (``int``)Math.Pow(10, n - 1);`` ` `    ``// Ending N-digit``    ``// palindromic number``    ``int` `end = (``int``)Math.Pow(10, n) - 1;`` ` `    ``// Iterate over starting and``    ``/// ending number``    ``for` `(``int` `i = start; i <= end; i++) {`` ` `        ``// To check if i is``        ``// palindromic or not``        ``if` `(ispalin(i)) {``            ``CalculateXOR = CalculateXOR ^ i;``            ``CalculateOR = CalculateOR | i;``        ``}``    ``}`` ` `    ``// Print the XOR and OR of all``    ``// palindromic number``    ``Console.Write(``"XOR = "` `+ CalculateXOR);``    ``Console.WriteLine(``" OR = "` `+ CalculateOR);``}`` ` `// Driver Code``public` `static` `void` `Main (``string``[] args) ``{``    ``int` `n = 4;``    ``CalculateXORandOR(n);``}`` ` `}`` ` `// This code is contributed by AnkitRai01`

## Javascript

 ``

Output:

`XOR = 4606 OR = 16383`

Time Complexity: O(10n)

My Personal Notes arrow_drop_up