The general concept of processing the digits of a number is a fundamental technique. You can solve many tasks with this approach, like summing digits, getting the product of digits, counting digits, and reversing a number.

But what’s an Armstrong number and how do you test for one?

What Is an Armstrong Number?

An Armstrong number is a number whose sum of the cubes of its digits equals the number itself. For example, 153 is an Armstrong number. If you take the digits of 153 individually and cube them:

(1 × 1 × 1) + (5 × 5 × 5) + (3 × 3 × 3)

Then add the results:

1 + 125 + 27

You’ll get 153, the same as the original number.

An Algorithm to Find an Armstrong Number

Writing an algorithm is the first step to implementing any program. You can use the algorithm as a reference to write pseudocode, then implement it using your desired programming language. An algorithm gives you the exact series of instructions to follow, eliminating logic errors and making implementation simpler.

Here’s the algorithm to find whether a number is Armstrong or not:

Declare variables sum, temp, n, r Take the value of n from the user Initialize variable sum to 0 and take backup of n as temp = n Repeat steps 5 - 7 while n > 0 r = n % 10 sum = sum + cube of each digit (r × r × r) n = n / 10 If sum equals temp, display “Number is an Armstrong number” Else, display “Number is not an Armstrong Number”

Pseudocode for Implementing the Armstrong Algorithm

Pseudocode can be a useful step in designing the implementation of an algorithm. Writing the pseudocode helps you to easily convert it into code in any programming language. Here’s the pseudocode for the implementation of the Armstrong number:

The Armstrong Program in C

Observe the pseudocode above and convert each statement to C code.

Start by importing stdio.h to perform the input and output operations. Declare the main function and start implementing the logic of the program. Use n to store the input number, r to store the individual digits of the number, sum to store the sum of the cubes of the digits, and temp to store a copy of the number.

Use the printf function to ask the user to input a number. Use the scanf function to read the number and store it in variable n. %d is the decimal format specifier to take an integer as input.

Clear out any garbage value by initializing sum as zero and take a backup of n as temp.

Declare a while loop that runs until the number is zero or less. Inside the loop is a three-step process that you can use in various other programming tasks. The three steps are:

Get the individual digit of the number by taking the number’s modulus with 10. When you divide any number with 10 as a whole, the remainder is the last digit itself. For example, when you divide 153 by 10, the integer result is 15 and the modulus is 3. When you have the individual digit you can perform your desired operation. To find an Armstrong number, the desired operation is the sum of the cubes of the number’s digits. Take the cube of the digit r and add it to the sum variable. Eliminate the last digit of the number by dividing it by 10. On division by 10, you get the quotient, in this case, 15.

Check if the sum obtained equals the original number. If they’re equal, the number is indeed an Armstrong number, otherwise, it isn’t.

Other Applications of the General Algorithm

By altering step two of the three-step logic seen above, you can implement a variety of programs.

1. Sum, Product of Digits of Number

To get the sum of digits of a number, simply replace the line with:

For product, declare variable prod as 1 and replace the addition sum with a multiplication symbol:

2. Count of Digits of Number

To count the digits of a number, simply initialize a variable count to zero, omit step one, and increment it until n equals zero. The implementation of the loop will look like this:

3. Reverse of Number, Palindrome Number

To reverse a number, initialize a variable rev to one, and add it after multiplying by ten:

Once you get the reverse of a number, compare it to the copy of the original number itself. If the reverse number equals the number itself, it is a Palindrome number.

4. Smallest and Largest Digit of a Number

Initialize a variable min as nine and compare it with the digit extracted from step one to find the smallest digit of a number. You can implement it as:

Similarly, initialize a variable max with zero, and compare it with the digit extracted to find the largest digit of a number. You can implement it as:

This is how you can find the largest and smallest digits of a number.

5. Special Numbers

There are many other numbers, like an Armstrong number, that you can compute. These include Neon number, Automorphic number, Krishnamurthy number, Buzz number, Perfect number, Amicable number, and Twin prime number.

Programming and Mathematics

Mathematics is widely used in programming. Binary mathematics is the core of programming as all the software you use today is a combination of the digits zero and one. All the data you work with is stored in binary format. Linear algebra is used in machine learning applications, graph algorithms, quantum computation, and more.

Calculus, discrete mathematics, and statistics are widely used in problem-solving and algorithm design. Mathematics enhances your computational skill and is an essential part of programming.