BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

C Program to check Armstrong number

Last Updated: May 19, 2024 by Chaitanya Singh | Filed Under: C Programs

In this example, we will write a C Program to check Armstrong number. An Armstrong number (also known as narcissistic number) is a number that is equal to the sum of its own digits each raised to the power of the number of digits.

For example, 370 is an Armstrong number

    //We are adding cubes of individual digits
    //because the number of digits in number is 3
    370 = 3*3*3 + 7*7*7 + 0*0*0
    = 27 + 343 + 0
    = 370

    C Program to check if a number is Armstrong number or not

    The explanation of the program is available at the end of this code.

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

    int main() {
    int number, originalNumber, remainder, numDigits = 0;
    double result = 0.0;

    // Prompt the user to enter a number
    printf("Enter an integer: ");
    scanf("%d", &number);

    //Copying the number to a different variable
    //so that the number doesn't get changed in calculations
    originalNumber = number;

    // Counting number of digits in the number
    while (originalNumber != 0) {
    originalNumber /= 10;
    ++numDigits;
    }

    originalNumber = number;

    // Calculate the sum of the digits raised to the power of numDigits
    while (originalNumber != 0) {
    //get the last digit
    remainder = originalNumber % 10;

    //add the last digit raised to the power of number of digits
    result += pow(remainder, numDigits);

    //remove last digit from number
    originalNumber /= 10;
    }

    // Check if the result is equal to the number
    if ((int)result == number)
    printf("%d is an Armstrong number.\n", number);
    else
    printf("%d is not an Armstrong number.\n", number);

    return 0;
    }

    Output:

    Enter an integer: 153
    153 is an Armstrong number.

    Explanation of the program:

    1. Include header files:
      • stdio.h is included for scanf and printf.
      • math.h is included for the pow function which calculates the power of a number.
    2. Declare variables:
      • number to store the number entered by user.
      • originalNumber to hold the copy of number for calculation purposes.
      • remainder to get the last digit of number on every iteration.
      • numDigits to store the number of digits in the number.
      • result to store the sum of each digit raised to the power of the number of digits.
    3. Input:
      • Prompt the user to enter a number using printf and then use scanf to read & store the entered value.
    4. Calculate the number of digits:
      • Dividing number by 10 (which removes last digit from number) and increasing the value of numDigits by 1 on every iteration.
    5. Calculate the Armstrong number:
      • Get last digit of number using modulo % operator on every iteration of while loop.
      • Raise each digit to the power of numDigits using pow function and add to result.
      • Divide the number by 10 to remove last digit so that the second last digit gets processed on next iteration.
    6. Check and output the result:
      • Compare the original number with the calculated result, if equal, print that the input number is an Armstrong number.
      • Otherwise, print that the input number is not an Armstrong number.

    Top Related Articles:

    1. C Program to Check Whether a Number is Prime or Not
    2. C Program to Count Number of Digits in an Integer
    3. C Program to check abundant number
    4. C Program to display factors of a number
    5. C Program to concatenate two strings without using strcat

    About the Author

    I have 15 years of experience in the IT industry, working with renowned multinational corporations. Additionally, I have dedicated over a decade to teaching, allowing me to refine my skills in delivering information in a simple and easily understandable manner.

    – Chaitanya

    Leave a Reply Cancel reply

    Your email address will not be published. Required fields are marked *

    Copyright © 2012 – 2025 BeginnersBook . Privacy Policy . Sitemap