How it works...

First, we will apply the mod 10 operator to separate our digits. Assuming the number entered by us is 153, you can see that 153 is divided by 10 and the remaining 3 is pushed to the stack:

Figure 3.4

The value in the stack is pushed at the index location indicated by top. Initially, the value of top is -1. It is so because before the push operation, the value of top is incremented by 1, and the array is zero-based, that is, the first element in the array is placed at the 0 index location. Consequently, the value of top has to be initialized to -1. As mentioned, the value of top is incremented by 1 before pushing, that is, the value of top will become 0and the remainder of 3 is pushed to stack[0].

In the stack, the value of top is incremented by 1 to indicate the location in the stack where the value will be pushed.

We will again apply the mod 10 operator to the 15 quotient. The remainder that we will get is 5, which will be pushed to the stack. Again, before pushing to the stack, the value of top, which was 0, is incremented to 1. At stack[1], the remainder is pushed:

Figure 3.5

To the 1 quotient, we will again apply the mod 10 operator. But because 1 is not divisible by 10, 1 itself will be considered as the remainder and will be pushed to the stack. The value of top will again be incremented by 1 and 1 will be pushed to stack[2]:

Figure 3.6

Once all the digits are separated and placed in the stack, we will pop them out one by one. Then, we will raise each digit to the power equal to the count of the digits. Because the number 153 consists of three digits, each digit is raised to the power of 3.

While popping values out of the stack, the value indicated by the top pointer is popped out. The value of top is 2, hence the value at stack[2], that is, 1, is popped out and raised to the power of 3, as follows:

Figure 3.7

After the pop operation, the value of top will be decremented to 1 to indicate the next location to be popped out. Next, the value at stack[1] will be popped out and raised to the power of 3. We will then add this value to our previous popped-out one:

Figure 3.8

After the popping-out operation, the value of top is decremented by 1, now making its value 0. So, the value at stack[0] is popped out and raised to the power of 3. The result is added to our earlier computation:

Figure 3.9

The result after computing 13 + 53 + 33 is 153, which is the same as the original number. This proves that 153 is an Armstrong number.

Let's use GCC to compile the armstrong.c program, as follows:

D:\CBook>gcc armstrong.c -o armstrong

Let's check whether 127 is an Armstrong number:

D:\CBook>./armstrong
Enter a number 127
127 is not an armstrong number

Let's check whether 153 is an Armstrong number:

D:\CBook>./armstrong
Enter a number 153
153 is an armstrong number

Let's check whether 1634 is an Armstrong number:

D:\CBook>./armstrong
Enter a number 1634
1634 is an armstrong number

Voilà! We've successfully made a function to find whether a specified number is an Armstrong number or not.

Now, let's move on to the next recipe!