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 numbersInput: 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 <bits/stdc++.h> 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 import java.io.*; 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
<script> // Javascript program to find the XOR // and OR of all Armstrong numbers // of N digits // Function to check if a number // is Armstrong or not function isArmstrong(x, n) { let sum1 = 0; let temp = x; while (temp > 0) { let digit = temp % 10; sum1 += Math.pow(digit, n); temp = parseInt(temp / 10, 10); } return (sum1 == x); } // Function to find XOR of all // N-digits Armstrong number function CalculateXORandOR(n) { // To store the XOR and OR of all // Armstrong number let CalculateXOR = 0; let CalculateOR = 0; // Starting N-digit // Armstrong number let start = Math.pow(10, n - 1); // Ending N-digit // Armstrong number let end = (Math.pow(10, n)) - 1; // Iterate over starting and // ending number for (let 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 document.write( "XOR = " + CalculateXOR + "</br>" ); document.write( "OR = " + CalculateOR + "</br>" ); } // Driver code let n = 4; CalculateXORandOR(n); // This code is contributed by divyeshrabadiya07 </script> |
XOR = 880 OR = 10098
Time Complexity: O((10n – 10n-1) * log10n)
Auxiliary Space: O(1)
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!