# XOR and OR of all N-digit Armstrong numbers

• Last Updated : 26 Nov, 2021

Given an integer N, the task is to find the XOR and OR values of all N-digit Armstrong numbers.

Examples

Input: N = 3
Output: XOR = 271, OR = 511
153, 370, 371, 407 are the three digit armstrong numbers

Input: N = 4
Output: XOR = 880, OR = 10098

Approach:

• Find the starting and ending number of N-digit Armstrong number by:
```Starting N-digit Armstrong number = pow(10, n - 1)
Ending N-digit Armstrong number   = pow(10, n) - 1```
• Iterate over the N-digit Armstrong numbers from starting number till ending number and check whether that number is Armstrong or not.
• If the number is Armstrong, 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++14

 `// C++ program to find the XOR ` `// and OR of all Armstrong numbers ` `// of N digits` `#include ` `using` `namespace` `std;`   `// Function to check if a number` `// is Armstrong or not` `bool` `isArmstrong(``int` `x, ``int` `n) ` `{` `    ``int` `sum1 = 0;` `    ``int` `temp = x;` `    ``while` `(temp > 0) {` `        ``int` `digit = temp % 10;` `        ``sum1 += (``int``)``pow``(digit, n);` `        ``temp /= 10;` `    ``}` `    ``return` `sum1 == x;` `}`   `// Function to find XOR of all` `// N-digits Armstrong number` `void` `CalculateXORandOR(``int` `n) ` `{`   `    ``// To store the XOR and OR of all` `    ``// Armstrong number` `    ``int` `CalculateXOR = 0;` `    ``int` `CalculateOR = 0;`   `    ``// Starting N-digit` `    ``// Armstrong number` `    ``int` `start = (``int``)``pow``(10, n - 1);`   `    ``// Ending N-digit` `    ``// Armstrong number` `    ``int` `end = (``int``)``pow``(10, n) - 1;` `    `  `    ``// Iterate over starting and` `    ``// ending number` `    ``for` `(``int` `i = start; i < end + 1; i++) ` `    ``{`   `        ``// To check if i is` `        ``// Armstrong or not` `        ``if` `(isArmstrong(i, n)) {` `            ``CalculateXOR = CalculateXOR ^ i;` `            ``CalculateOR = CalculateOR | i;` `        ``}` `    ``}` `    `  `    ``// Print the XOR and OR of all` `    ``// Armstrong number` `    ``cout << ``"XOR = "` `<< CalculateXOR << endl;` `    ``cout << ``"OR = "` `<< CalculateOR << endl;` `}`   `// Driver Code` `int` `main() ` `{`   `    ``int` `n = 4;` `    ``CalculateXORandOR(n);` `}`   `// This code is contributed by shivanisinghss2110`

## Java

 `// Java program to find the XOR ` `// and OR of all Armstrong numbers ` `// of N digits ` `class` `GFG ` `{` `    `  `    ``// Function to check if a number` `    ``// is Armstrong or not` `    ``static` `boolean` `isArmstrong(``int` `x, ``int` `n) {` `        ``int` `sum1 = ``0``;` `        ``int` `temp = x;` `        ``while` `(temp > ``0``) {` `            ``int` `digit = temp % ``10``;` `            ``sum1 += Math.pow(digit, n);` `            ``temp /= ``10``;` `        ``}` `        ``return` `sum1 == x;` `    ``}`   `    ``// Function to find XOR of all` `    ``// N-digits Armstrong number` `    ``static` `void` `CalculateXORandOR(``int` `n) {`   `        ``// To store the XOR and OR of all` `        ``// Armstrong number` `        ``int` `CalculateXOR = ``0``;` `        ``int` `CalculateOR = ``0``;`   `        ``// Starting N-digit` `        ``// Armstrong number` `        ``int` `start = (``int``) Math.pow(``10``, n - ``1``);`   `        ``// Ending N-digit` `        ``// Armstrong number` `        ``int` `end = (``int``) (Math.pow(``10``, n)) - ``1``;` `        `  `        ``// Iterate over starting and` `        ``// ending number` `        ``for` `(``int` `i = start; i < end + ``1``; i++) {`   `            ``// To check if i is` `            ``// Armstrong or not` `            ``if` `(isArmstrong(i, n)) {` `                ``CalculateXOR = CalculateXOR ^ i;` `                ``CalculateOR = CalculateOR | i;` `            ``}` `        ``}` `        `  `        ``// Print the XOR and OR of all` `        ``// Armstrong number` `        ``System.out.println(``"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 29AjayKumar`

## Python3

 `# Python3 program to find the XOR ` `# and OR of all Armstrong numbers ` `# of N digits `   `# Function to check if a number ` `# is Armstrong or not ` `def` `isArmstrong (x, n): ` `    ``sum1 ``=` `0`  `    ``temp ``=` `x  ` `    ``while` `temp > ``0``: ` `        ``digit ``=` `temp ``%` `10` `        ``sum1 ``+``=` `digit ``*``*``n ` `        ``temp ``/``/``=` `10` `    ``return` `sum1 ``=``=` `x`   `# Function to find XOR of all ` `# N-digits Armstrong number ` `def` `CalculateXORandOR(n) : `   `    ``# To store the XOR and OR of all ` `    ``# Armstrong number ` `    ``CalculateXOR ``=` `0` `    ``CalculateOR ``=` `0`   `    ``# Starting N-digit ` `    ``# Armstrong number ` `    ``start ``=` `10` `*``*` `(n ``-` `1``)`   `    ``# Ending N-digit ` `    ``# Armstrong number ` `    ``end ``=` `(``10``*``*``n) ``-` `1` `    ``# Iterate over starting and ` `    ``# ending number ` `    ``for` `i ``in` `range``( start, end ``+` `1``) : `   `        ``# To check if i is ` `        ``# Armstrong or not ` `        ``if` `(isArmstrong(i, n)) : ` `            ``CalculateXOR ``=` `CalculateXOR ^ i` `            ``CalculateOR ``=` `CalculateOR | i`   `    ``# Print the XOR and OR of all ` `    ``# Armstrong number ` `    ``print``(``"XOR = "``, CalculateXOR) ` `    ``print``(``"OR = "``, CalculateOR)`   `# Driver Code ` `if` `__name__ ``=``=` `"__main__"` `: `   `    ``n ``=` `4``; ` `    ``CalculateXORandOR(n); `

## C#

 `// C# program to find the XOR ` `// and OR of all Armstrong numbers ` `// of N digits ` `using` `System;`   `class` `GFG ` `{` `    `  `    ``// Function to check if a number` `    ``// is Armstrong or not` `    ``static` `bool` `isArmstrong(``int` `x, ``int` `n) {` `        ``int` `sum1 = 0;` `        ``int` `temp = x;` `        ``while` `(temp > 0) {` `            ``int` `digit = temp % 10;` `            ``sum1 += (``int``)Math.Pow(digit, n);` `            ``temp /= 10;` `        ``}` `        ``return` `sum1 == x;` `    ``}`   `    ``// Function to find XOR of all` `    ``// N-digits Armstrong number` `    ``static` `void` `CalculateXORandOR(``int` `n) {`   `        ``// To store the XOR and OR of all` `        ``// Armstrong number` `        ``int` `CalculateXOR = 0;` `        ``int` `CalculateOR = 0;`   `        ``// Starting N-digit` `        ``// Armstrong number` `        ``int` `start = (``int``) Math.Pow(10, n - 1);`   `        ``// Ending N-digit` `        ``// Armstrong number` `        ``int` `end = (``int``) (Math.Pow(10, n)) - 1;` `        `  `        ``// Iterate over starting and` `        ``// ending number` `        ``for` `(``int` `i = start; i < end + 1; i++) {`   `            ``// To check if i is` `            ``// Armstrong or not` `            ``if` `(isArmstrong(i, n)) {` `                ``CalculateXOR = CalculateXOR ^ i;` `                ``CalculateOR = CalculateOR | i;` `            ``}` `        ``}` `        `  `        ``// Print the XOR and OR of all` `        ``// Armstrong number` `        ``Console.WriteLine(``"XOR = "` `+ CalculateXOR);` `        ``Console.WriteLine(``"OR = "` `+ CalculateOR);` `    ``}`   `    ``// Driver Code` `    ``public` `static` `void` `Main(String[] args) {`   `        ``int` `n = 4;` `        ``CalculateXORandOR(n);` `    ``}` `}`   `// This code is contributed by PrinciRaj1992`

## Javascript

 ``

Output:

```XOR =  880
OR =  10098```

Time Complexity: O((10n – 10n-1) * log10n)

Auxiliary Space: O(1)

My Personal Notes arrow_drop_up
Recommended Articles
Page :