Armstrong Number Program in C Language

Spread the love

Program Description:

C Program to check if the given number is Armstrong number.

What is Armstrong Number:

An Armstrong number of an integer such that the sum of the cubes of its digits is equal to the number itself.

Lets look at couple of examples

Example 1:

Number 371 is an Armstrong number

since 3^3 + 7^3 + 1^3 = 371

Example 2:

Number 153 is also a Armstrong number

since 1^3 + 5^3 + 3^3 = 153

Armstrong number Program Algorithm:

We need to get each digit and then calculate the cubes of each digit. Then add all cubes which should be equal to the given number. If the cubes are equal to the given number then it’s an Armstrong number. Otherwise, it is not.

Here is the flow of execution.

  1. We are going to use an extra variable temp to hold the input number n. As we need 'n' to verify our result.
  2. We are going to use the Modulus operator to get the last digit of the number, We can get the last digit of any number by performing the modulus with 10.
    • For example, 8798 % 10 = 8
  3. Once we get the last digit, calculate the cube of the digit. Then add to arm variable. arm variable is initialised with zero at the start. and we will add the cubes of each digit to the arm variable.
  4. We can remove the last digit of any number by diving the number by 10. As we already calculated the cube of the last digit we no longer need it. So we will remove the last digit by dividing the number with 10.

We are going to repeat above steps from 2 to 4 until the temp becomes less than 0. So at each iteration we are going to get one digit and then calculate the cube of that digit, Add to the ‘arm’ variable. Then remove that digit from the number. So once we pass through the all numbers the ‘temp’ will becomes less than zero. And our loop will be terminated.

Once we are out of the loop, We are going to check the 'arm' variable value with the input number 'n'. If both 'arm' value and 'n' are equal then the given number ‘n’ is Armstrong number. If the numbers are not equal then 'n' is not an Armstrong number.

Program: To check or detect Armstrong number :

Program Output:

Related Programs:

Learn More on Armstrong Numbers :

Venkat

Hi Guys, I am Venkatesh. I am a programmer and an Open Source enthusiast. I write about programming and technology on this blog.

You may also like...

5 Responses

  1. May 26, 2021

    […] Armstrong Number Program in C Language – SillyCodes […]

  2. May 27, 2021

    […] Armstrong Number Program in C Language – SillyCodes […]

  3. May 27, 2021

    […] Armstrong Number Program in C Language – SillyCodes […]

  4. May 31, 2021

    […] Armstrong Number Program in C Language – SillyCodes […]

  5. July 22, 2021

    […] Armstrong Number Program in C Language – SillyCodes […]

Leave a Reply