Algorithm


1. Initialization:
      Initialize variables originalNumber, remainder, result to 0.
     Initialize a variable n to store the number of digits.

2. Copy the number:
     Copy the original value of number to originalNumber for later comparison.

3. Count the number of digits:
      Use a loop to count the number of digits in number and store it in n.

4. Calculate Armstrong sum:
      Use a loop to extract each digit from originalNumber.
     Raise each digit to the power of n and add the result to result.

5. Check for Armstrong number:
      If result is equal to the original number, then it is an Armstrong number.
     Otherwise, it is not an Armstrong number.

6. Output:
      Display whether the number is an Armstrong number or not.

  1. A positive integer is called an Armstrong number (of order n) if

    abcd... = an + bn + cn + dn + 
    

    In the case of an Armstrong number of 3 digits, the sum of cubes of each digit is equal to the number itself. For example, 153 is an Armstrong number because

    153 = 1*1*1 + 5*5*5 + 3*3*3 
    


 

Code Examples

#1 Code Example-Programing Check Armstrong Number of three digits

Code - C Programming

#include <stdio.h>
int main() {
    int num, originalNum, remainder, result = 0;
    printf("Enter a three-digit integer: ");
    scanf("%d", &num);
    originalNum = num;

    while (originalNum != 0) {
       // remainder contains the last digit
        remainder = originalNum % 10;
        
       result += remainder * remainder * remainder;
        
       // removing last digit from the orignal number
       originalNum /= 10;
    }

    if (result == num)
        printf("%d is an Armstrong number.", num);
    else
        printf("%d is not an Armstrong number.", num);

    return 0;
}
Copy The Code & Try With Live Editor

Output

x
+
cmd
Enter a three-digit integer: 371
371 is an Armstrong number.

#2 Code Example-Programing Check Armstrong Number of n digits

Code - C Programming

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

int main() {
   int num, originalNum, remainder, n = 0;
   float result = 0.0;

   printf("Enter an integer: ");
   scanf("%d", &num);

   originalNum = num;

   // store the number of digits of num in n
   for (originalNum = num; originalNum != 0; ++n) {
       originalNum /= 10;
   }

   for (originalNum = num; originalNum != 0; originalNum /= 10) {
       remainder = originalNum % 10;

      // store the sum of the power of individual digits in result
      result += pow(remainder, n);
   }

   // if num is equal to result, the number is an Armstrong number
   if ((int)result == num)
    printf("%d is an Armstrong number.", num);
   else
    printf("%d is not an Armstrong number.", num);
   return 0;
}
Copy The Code & Try With Live Editor

Output

x
+
cmd
Enter an integer: 1634
1634 is an Armstrong number.
Advertisements

Demonstration


C Programing  to Check Armstrong Number Example-DevsEnv

Next
Appending into a File in C Programming