C Program to Check If Number is Armstrong Number and Find All Armstrong Numbers In Range

Armstrong number is the number whose sum of cube of individual digits is the number itself. For example: 153, 407 etc.

153 = 13 + 53 + 33 // Armstrong Number

Example: Source Code to Check Armstrong Number

#include<stdio.h>

int main()
{
     int n, a, ans=0, chk;

     printf ("Enter a number \n");
     scanf ("%d", &n);

     chk = n;

     while (n>0)
     {
         a = n%10;
         ans = ans + (a*a*a);
         n = n/10;
     }

     if (chk==ans)
          printf ("The number is ARMSTRONG");
     else
          printf ("The number is NOT ARMSTRONG");
      return 0;

}

Here, the number entered by user is stored in variable n. Another variable chk is assigned the same value as n so as to check the answer at the end of the program. Inside the loop, variable a stores single last digit of n, ans is the sum of cube of individual digits of n, and the expression n=n/10 removes the last digit of the number(for eg. 122/10 gives result 12). The loop continues as long as the value of n is greater than 0. In the end, if the value of ans is equal to the value of chk(i.e. number entered by the user) then the number is Armstrong. Otherwise, the number is not Armstrong.

Output

Enter a number
153
The number is ARMSTRONG
Enter a number
250
The number is NOT ARMSTRONG

 


We can use similar logic to get the armstrong numbers between a range of numbers.

For displaying all Armstrong numbers between a range, say 100 and 1000, nested loop has to be used. The outer loop runs from 100 to 1000 and the inner loop checks whether the number is Armstrong or not. C program to display all Armstrong numbers from 100 to 1000 is along with total number of Armstrong numbers is shown below:

#include<stdio.h>
int main()
{
    int i, j, count=0, a, ans=0, chk;
    printf("The armstrong numbers between 100 and 1000 are: \n");
    for (i=100; i<=1000; i++)
    {
        chk = i;
        j = i;
        ans = 0;
        do
        {
            a = j % 10;
            ans = ans+(a*a*a);
            j = j/10;
        } while (j>0);
        if (ans==chk)
        {
            printf ("%d \n",ans);
            count = count+1;
        }
    }
    printf("\n The total number of armstrong numbers between 100 and 1000 is %d",count);
    return 0;
} 

Here, the outer loop runs from 100 to 1000. Variable chk is assigned the value of i for checking whether the sum of cube of individual digits is equal to the number itself and variable j is assigned the value of i as the operation inside the do loop is performed on variable j.

NOTE: Here the value i itself cannot be performed any operation inside the do loop because if variable i is used inside do loop then the value of i changes and i is to be used in the outer loop for checking each and every number from 100 to 1000.

The expressions inside do loop calculate the sum of cube of individual digits of i. And the value is stored in variable ans. If variable ans is equal to variable chk (or variable i) then the number is Armstrong and is displayed on the screen. After the number is displayed on the screen, the variable count is increased by 1 as it is used as a counter (which is initialized to 0 to avoid addition of garbage values) which counts the total number of armstrong numbers and the total number of armstrong numbers is displayed on the screen.

Output:

The armstrong numbers between 100 and 1000 are:

153

370

371

407

The total number of armstrong numbers between 100 and 1000 is 4