How to Implement Armstrong Number in C?

What are Armstrong numbers?

An Armstrong number is a 3-digit number such that the sum of the cube of each of its digits is equal to the number itself.

For example,

371 is an Armstrong number since 3**3 + 7**3 + 1**3 = 371.

 

In general, a positive integer number of n digits is called an Armstrong number of order n (order is a number of digits) if the sum of the power of n of each digit is equal to the number itself.

For example,

abcd... = pow(a,n) + pow(b,n) + pow(c,n) + pow(d,n) + ....

 

Below steps will show common approach to find Armstrong Number in C programming steps:

  • Enter any number
  • Divide the given number into individual digits (For Example, Divide 153 into 1, 5, and 3) and count number digits (or find order).
  • If the order is n, then calculate the power of n for each individual digits.
  • Compare the original value with the sum value. If they are equal, then it is an Armstrong number. Otherwise, it is not an Armstrong Number in C.

 

C Program to find Armstrong numbers:

 

#include <stdio.h>
#include <math.h>

int main()
{
    int num, tmp, rem, sum = 0, n = 0 ;

    printf("Enter a Number: ");
    scanf("%d", &num);

    tmp = num;

    //Get number of digit
    while (tmp != 0)
    {
        tmp /= 10;
        ++n;
    }

    //Now again take original value
    tmp = num;

    //Get sum of power of each digits
    while (tmp != 0)
    {
        rem = tmp%10;
        sum += pow(rem, n);
        tmp /= 10;
    }

    if(sum == num)
    {
        printf("%d is an Armstrong number.", num);
    }
    else
    {
        printf("%d is not an Armstrong number.", num);
    }
    return 0;
}

Output:

Enter a Number: 370
370 is an Armstrong number.

 

C Program to find Armstrong numbers using recursion :

The below program will check whether a number is Armstrong Number or not using the recursion concept in C.

#include <stdio.h>
#include <math.h>

int checkArmstrong (int num, int n)
{
    static int rem, Sum = 0;

    if (num > 0)
    {
        rem = num %10;
        Sum = Sum + pow(rem, n);
        checkArmstrong (num /10, n);
        return Sum;
    }
    return 0;
}

int main()
{
    int num, Sum = 0, n =0,tmp;

    printf("\nPlease Enter number to Check for Armstrong = ");
    scanf("%d", &num);

    tmp = num;
    while (tmp != 0)
    {
        ++n;
        tmp = tmp / 10;
    }

    Sum = checkArmstrong (num, n);
    printf("Sum of entered number is = %d\n", Sum);

    if ( num == Sum )
    {
        printf("\n%d is Armstrong number.\n", num);
    }
    else
    {
        printf("%d is not the Armstrong number.\n", num);
    }

    return 0;
}

Output:

Enter a Number: 370
370 is an Armstrong number.

 

Generate Armstrong number within a given range (1 to 1000 (or n)):

The mentioned C program find the Armstrong Number in a given range. The minimum and maximum value of the range ask by users.

#include<stdio.h>
#include <math.h>

int checkArmstrong (int num)
{
    int tmp, Reminder, Times =0, Sum = 0;

    tmp = num;

    while (tmp != 0)
    {
        Times = Times + 1;
        tmp = tmp / 10;
    }

    for(tmp = num; tmp > 0; tmp =  tmp /10 )
    {
        Reminder = tmp %10;
        Sum = Sum + pow(Reminder, Times);
    }

    return Sum;
}

int main()
{
    int num,Reminder,Reverse,tmp, Sum;
    int rangeMinValue,rangeMaxValue;

    printf("Please Enter the rangeMinValue & rangeMaxValue Values = ");
    scanf("%d %d",&rangeMinValue, &rangeMaxValue);

    printf("Armstrong Numbers Between %d and %d are:\n",rangeMinValue, rangeMaxValue);
    for(num = rangeMinValue; num <= rangeMaxValue; num++)
    {
        Sum = checkArmstrong (num);

        if(num == Sum)
        {
            printf("%d ",num);
        }
    }

    return 0;
}

Output:

Please Enter the rangeMinValue & rangeMaxValue Values = 1 1000

Armstrong Numbers Between 1 and 1000 are:
1 2 3 4 5 6 7 8 9 153 370 371 407

 

C Program to find nth Armstrong number:

The mentioned C program find nth Armstrong Number in a given range. The minimum and maximum value of the range ask by users.

For example,

9th Armstrong NumberĀ  is 9

10th Armstrong Number is 153

 

#include<stdio.h>
#include <math.h>

int main()
{
    int rangeMinValue,rangeMaxValue;
    int count=1, n = 0;
    int i;
    printf("Please Enter the rangeMinValue = ");
    scanf("%d",&rangeMinValue);

    printf("Please Enter the rangeMaxValue = ");
    scanf("%d",&rangeMaxValue);

    printf("Please Enter the n to find nth Armstrong Number = ");
    scanf("%d",&n);

    for(i = rangeMinValue; i <= rangeMaxValue; i++)
    {
        int num=i, rem, digit=0, sum=0;

        //Copy the value for num in num
        num = i;

        // Find total digits in num
        digit = (int) log10(num) + 1;

        // Calculate sum of power of digits
        while(num > 0)
        {
            rem = num % 10;
            sum = sum + pow(rem,digit);
            num = num / 10;
        }
        // Check for Armstrong number
        if(i == sum)
        {
            if(count==n)
            {
                printf("%d\n",i);
                break;
            }
            else
            {
                count++;
            }
        }
    }

return 0;
}

Output:

Please Enter the rangeMinValue = 1
Please Enter the rangeMaxValue = 1000
Please Enter the n to find nth Armstrong Number = 9
9