Skip to content
Utopper skill
  • ProgrammingExpand
    • Programming Examples
  • Interview QuestionsExpand
    • DevOps Interview Questions
    • Android Interview Questions
  • How to
  • Tools
  • Top 10
  • Book Summaries
Utopper skill
Programming Examples C Programming Example

Armstrong number in C

Article Contents

  1. What is an Armstrong Number?
  2. Approach to check Armstrong number in C language
  3. Pseudo code to check Armstrong number in C language.
  4. C program to check Armstrong number

What is an Armstrong Number?

An Armstrong number (also known as a 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, 153 is an Armstrong number because 1^3 + 5^3 + 3^3 = 153. Similarly, 9474 is an Armstrong number because 9^4 + 4^4 + 7^4 + 4^4 = 9474.

An n-digit number is called an Armstrong number if the sum of cubes of its digits is equal to the number itself. For example, 153 is an Armstrong number as 153 = 111 + 555 + 333.

Approach to check Armstrong number in C language

Here is an approach to check if a number is an Armstrong number in C programming language in point form:

  1. Take input from the user: Prompt the user to enter a number and store it in a variable.
  2. Find the number of digits: Use a while loop to count the number of digits in the given number.
  3. Separate the digits: Use another while loop to separate the digits of the number. In each iteration, take the remainder of the number when divided by 10 and store it in a variable.
  4. Raise each digit to the power of the number of digits: Use the pow() function from the math.h library to raise each digit to the power of the number of digits. Keep a separate variable to keep track of the sum of these values.
  5. Compare the sum with the original number: If the sum is equal to the original number, it is an Armstrong number. If not, it is not an Armstrong number.
  6. Print the result: Print whether the given number is an Armstrong number or not.

Our approach is to separate the digits of the number, raise each digit to the power of the number of digits, and add the resulting values. If the sum is equal to the original number, it is an Armstrong number.

Pseudo code to check Armstrong number in C language.

int number, originalNumber, remainder, result = 0;
printf("Enter a three-digit integer: ");
scanf("%d", &number);
originalNumber = number;

while (originalNumber != 0) {
    remainder = originalNumber % 10;
    result += remainder * remainder * remainder;
    originalNumber /= 10;
}

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

This code prompts the user to enter a three-digit integer, then uses a while loop to find the sum of the cubes of each digit. If the sum is equal to the original number, it’s an Armstrong number; if not, it’s not an Armstrong number.

C program to check Armstrong number

Here’s a C program that checks if a number is an Armstrong number:

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

int main() {
    int number, originalNumber, remainder, result = 0, n = 0;
    printf("Enter a number: ");
    scanf("%d", &number);
    originalNumber = number;

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

    originalNumber = number;

    while (originalNumber != 0) {
        remainder = originalNumber % 10;
        result += pow(remainder, n);
        originalNumber /= 10;
    }

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

    return 0;
}

This program first takes input from the user then using while loop it separates the digits of the number and then using another while loop it uses pow function to find the power of the each digit by the number of digits. At the end it compares if the sum is equal to the original number, If it is equal then it is an Armstrong number otherwise it is not. Please note that this program uses the math.h library, which must be included at the top of the file in order to use the pow() function.

You can also check the Other C Programs :

  • Palindrome Number
  • Fibonacci Series in C
  • Prime Number in C
Post Tags: #armstrong number#C Program Examples
Avatar photo
Utopper Skill Team
Facebook X Instagram YouTube Linkedin Pinterest

Post navigation

Previous Previous
Print Factorial in C
NextContinue
Print Alphabet Right Triangle in C

Latest Interview Questions

  • C++ Program To Print Prime Numbers From 1 To N
  • C Program to Print Your Own Name with Example
  • Book Summary of Why We Sleep By Matthew Walker
  • Book Summary of Indistractable By Nir Eyal
  • C Program To Convert Fahrenheit To Celsius
  • Book Summary of The One Thing By Gary Keller and Jay Papasan
  • Facebook
  • Instagram
  • YouTube
  • Telegram
  • LinkedIn
  • Twitter

Mail : [email protected]

Privacy Policy | DISCLAIMER | Contact Us

Learn Development

learn HTML

learn CSS

learn JavaScript

Examples

C Examples

C++ Examples

Java Examples

Study Material

Interview Questions

How to

Hosting

SEO

Blogging

© 2023 Utopper.com

All Rights Reserved with Copyright & Registered TradeMarks
OWNED BY : GROWTH EDUCATION SOLUTIONS PRIVATE LIMITED

Scroll to top
  • Programming
    • Programming Examples
  • Interview Questions
    • DevOps Interview Questions
    • Android Interview Questions
  • How to
  • Tools
  • Top 10
  • Book Summaries
Search