Question:  We are given a number and we need to check whether the given positive number is Armstrong or not using C programming language.

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 Number of three digits

  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 of Program

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 Number of N digits

  1. Take a number as input.
  2. Copy the original number into temporary variable i.etemp = num.
  3. Using While loop count the number of digits in temp and store it as length. Update temp = temp / 10 until temp != 0.
  4. Reinitialize temp variable with original number.
  5. Using another while loop with same condition, extract each digit as digit = temp % 10 and update the sum as sum = sum + digit^length.
  6. Outside loop, check if sum == original number.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 are checking each number from 1 to 1000 whether it is Armstrong or not.

Output of Program

Armstrong Number between 1 to 1000

That’s all we need to do to check whether a number is Armstrong or not in C. If you have any doubts then comment below.

Leave a Reply

Close Menu