Problem:  Write a program in C to check whether the given positive number is Armstrong or not.

Armstrong Number is a number whose sum of ‘powered digits with the “total number of digits in the number” ‘ is equal to the original number.

Check Armstrong number in C

Example 1: Check Armstrong for three Digits Number

  1. Take positive integer input into original_number
  2. Copy the original_number to the number
  3. Run while loop until number != 0
  4. Extract digit by dividing the number by 10( Remainder is the digit)
  5. Add cube of digit into sum as sum = sum + (digit*digit*digit)
  6. Update the number = number/10 (Getting rid of the last digit which has been used as the remainder in step 5)
  7. End loop
  8. Check if the sum == original_number
  9. If YES the Armstrong Number else NOT.

Output

When the input is 121 which is not an Armstrong Number.

Armstrong number in C

When the input is 153 which is an Armstrong Number.

Armstrong number C

Example 2: Check Armstrong for N digits Number

  1. Take a number as input.
  2. Copy the original number into temporary variable i.e temp=num.
  3. Using the While loop counts the number of digits in temp and store it as length. Update temp = temp / 10 until temp != 0.
  4. Reinitialize the temp variable with the original number.
  5. Using another while loop with the same condition, extract each digit as digit = temp%10 and update the sum as sum = sum + digit^length.
  6. Outside loop, check if sum==originalnumber. If yes then output “Armstrong Number, else “Not Armstrong number”.

Output

C Program for Armstrong number

Checking 4 digit number.

Output of C Program

Example 3: Check Armstrong from 1 to 1000

In this C Program, we will check and print all Armstrong numbers in the range from 1 to 1000.

Output

Armstrong Number between 1 to 1000

In this tutorial, we learned to check Armstrong number in C with examples.

Leave a Reply