An Armstrong number (also known as a narcissistic number or pluperfect digit) is a number that is equal to the sum of its own digits raised to the power of the number of digits. For example, the number 153 is an Armstrong number because:

13+53+33=1+125+27=1531^3 + 5^3 + 3^3 = 1 + 125 + 27 = 153

Logic of the Armstrong Number Program:

  1. Input a number from the user.
  2. Determine the number of digits in the number.
  3. Calculate the sum of each digit raised to the power of the total number of digits.
  4. Check if this sum is equal to the original number.
  5. Print whether the number is an Armstrong number or not.

Program:

Here’s a C program to check if a number is an Armstrong number:

#include <stdio.h> #include <math.h> int main() { int num, originalNum, remainder, count = 0, result = 0; // Input a number from the user printf("Enter an integer: "); scanf("%d", &num); originalNum = num; // Store the original number // Count the number of digits in the number while (originalNum != 0) { originalNum /= 10; // Remove the last digit count++; // Increment the digit count } originalNum = num; // Reset originalNum to the input number // Calculate the sum of digits raised to the power of count while (originalNum != 0) { remainder = originalNum % 10; // Get the last digit result += pow(remainder, count); // Add the power of the digit to result originalNum /= 10; // Remove the last digit } // Check if the result is equal to the original number if (result == num) { printf("%d is an Armstrong number.\n", num); } else { printf("%d is not an Armstrong number.\n", num); } return 0; }

Explanation:

  1. Variables:

    • int num: Holds the number inputted by the user.
    • int originalNum: Used to store the original number for comparison later.
    • int remainder: Stores the current digit being processed.
    • int count: Counts the number of digits in the number.
    • int result: Stores the sum of the digits raised to the power of the number of digits.
  2. Input:

    • The program prompts the user to enter an integer.
  3. Counting Digits:

    • The program counts the number of digits by continuously dividing originalNum by 10 until it reaches zero.
  4. Calculating the Armstrong Sum:

    • The program resets originalNum to the input number.
    • It then calculates the sum of each digit raised to the power of the total number of digits.
  5. Comparison:

    • Finally, the program checks if the calculated sum (result) is equal to the original number (num).
  6. Output:

    • The program prints whether the number is an Armstrong number or not based on the evaluation.

Sample Output:

Example 1:

Enter an integer: 153 153 is an Armstrong number.

Example 2:

Enter an integer: 123 123 is not an Armstrong number.

Example 3:

Enter an integer: 370 370 is an Armstrong number.

Key Points:

  • Power Calculation: The program uses the pow function from the math.h library to calculate the power of the digits.
  • Digit Extraction: The program effectively extracts digits by using the modulus and division operations.
  • User Interaction: The program interacts with the user, making it easy to input different numbers for testing.
  • Flexibility: The program can check for any integer, making it versatile for testing various cases.