Armstrong Number in C++

Learn via video course
FREE
View all courses
C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
by Prateek Narang
1000
5
Start Learning
C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
by Prateek Narang
1000
5
Start Learning
Topics Covered

In this guide, we explore how to identify Armstrong Numbers in C++, where a number is considered an Armstrong Number if the sum of its digits, each raised to the power of the number's total digits, equals the original number. For instance, 370 is an Armstrong number because 3^3 + 7^3 + 0^3 = 370. Understanding Armstrong Numbers helps in mastering numerical manipulation in programming.

Example

Input: 407407 Output: Yes 407407 is an Armstrong number because 43+03+73=4074^3 + 0^3 + 7^3 = 407.

Input: 371371 Output: Yes 371371 qualifies as an Armstrong number since 33+73+13=3713^3 + 7^3 + 1^3 = 371.

Input: 94749474 Output: Yes 94749474 is an Armstrong number because 94+44+74+44=94749^4 + 4^4 + 7^4 + 4^4 = 9474.

Input: 100100 Output: No 100100 is not an Armstrong number as 13+03+03=11^3 + 0^3 + 0^3 = 1, not equal to 100100.

Input: 94759475 Output: No 94759475 is not an Armstrong Number since 94+44+74+5494759^4 + 4^4 + 7^4 + 5^4 ≠ 9475.

The Naive Approach

The naive approach to verifying whether a given number is an Armstrong number in CPP involves calculating the sum of its digits each raised to the power of the number of digits in the number and comparing this sum to the original number. If they match, the number is an Armstrong Number.

Algorithm:

  1. Count the number of digits (N) in the input number (X).
  2. For each digit d in X, raise d to the power of N and calculate the sum of these values.
  3. If the sum equals X, then X is an Armstrong Number. Otherwise, it is not.

Code:

C++ program for armstrong number is given below:

Output:

Finding the Nth Armstrong Number

Finding the Nth Armstrong Number in c++ requires a more systematic approach than simply identifying whether a given number is an Armstrong Number. This task involves generating Armstrong Numbers in sequence until the Nth Armstrong Number is reached.

Approach:

  1. Start from the lowest possible number (0) and incrementally check each number to determine if it is an Armstrong Number.
  2. Maintain a count of Armstrong Numbers found.
  3. When the count reaches N, the current number is the Nth Armstrong Number.
  4. Stop the iteration and return or print the Nth Armstrong Number.

Code:

Output:

For N = 12, assuming the first few Armstrong Numbers are known,

Complexity:

  • Time Complexity: O(MN)O(M*N), where M is the range of numbers checked until the Nth Armstrong Number is found, and N is the operation to check if a number is an Armstrong Number. .
  • Space Complexity: O(1)O(1), as the algorithm uses a fixed amount of space regardless of the input N.

Finding Armstrong Number in C++ Using Strings

Finding an Armstrong Number in C++ using strings involves converting each number into its string representation to easily iterate over each digit, calculate the sum of the digits raised to the power of the number of digits (order), and check if the sum equals the original number.

Approach:

  1. Iterate through possible numbers, converting each to a string to determine its order (number of digits).
  2. Sum the powers of each digit according to the order.
  3. Compare the sum to the original number to determine if it is an Armstrong Number.

Code:

Output:

Complexity:

  • Time Complexity: O(NM)O(N*M), where N is the range of numbers to check, and M is the complexity of converting a number to a string and iterating over its digits.
  • Space Complexity: O(1)O(1), disregarding the variable-length internal storage for strings, since the algorithm processes one number at a time and does not accumulate results.

Finding All Armstrong Numbers within a Range

Finding all Armstrong number in cpp within a given range in C++ involves iterating through all numbers in the specified range, checking each to determine if it's an Armstrong Number, and then printing or storing the numbers that meet the criteria.

Approach:

  1. Iterate through each number in the specified range.
  2. For each number, calculate the sum of its digits raised to power of number of digits in the number.
  3. Compare the sum with the number to check if it's an Armstrong Number.
  4. Print or store the number if it meets the Armstrong Number criteria.

Code:

Output:

Complexity:

  • Time Complexity: O(NM)O(N*M), where N is the number of integers in the specified range, and M represents the operations performed on each integer (converting to string, iterating through digits, and raising to power).
  • Space Complexity: O(1)O(1), since the algorithm processes numbers one at a time without requiring additional space proportional to the input range.

Conclusion

  1. Armstrong number in c++ possess a unique property where the sum of the digits raised to power of the number of digits equals the number itself.
  2. We explored various methods to identify Armstrong Numbers, including naive approaches and optimizations for finding all Armstrong Numbers within a range.
  3. Techniques using strings offer a creative way to manipulate and check numbers, showcasing the flexibility of C++ in handling numerical data.
  4. The investigation into Armstrong Numbers underscores their relevance in programming challenges and their potential application in areas like data security.