Armstrong Number in C

Armstrong Number in C


A number is called Armstrong number when the sum of the nth power of each digit is equal to the given number.

  1. Introduction
  2. Algorithm of Armstrong Number in C
  3. Program of Armstrong Number in C
  4. Output
  5. Explanation

Introduction

If the Armstrong number is a positive integer of order n then it can be defined as,

abcde…. = pow (a, n) + pow (b, n) + pow (c, n) + pow (d, n) + pow (e, n) + ………

For example : 0, 1, 153, 370, 371, 1634 etc.

Let’s check whether 370 is an Armstrong number or not

370 = (3 * 3 * 3) + (7 * 7 * 7) + (0 * 0 * 0)

Here,    

(3 * 3 * 3) = 27

(7 * 7 * 7) = 343

(0 * 0 * 0) = 0

27 + 343 + 0 = 370, which is equal to the given number; hence it is an Armstrong number.

Let’s check four digits Armstrong number:

1634 = (1 * 1 * 1 * 1) + (6 * 6 * 6 * 6) + (3 * 3 * 3 * 3) + (4 * 4 * 4 * 4)

Here, 

(1 * 1 * 1 * 1) = 1

(6 * 6* 6 * 6) = 1296

(3 * 3 * 3 * 3) = 81

(4 * 4 * 4 * 4) = 256

1 + 1296 + 81 + 256 = 1634, which is equal to the given number; hence it is an Armstrong number.

Algorithm of Armstrong Number in C

  1. Take input from the user
  2. Initialize sum = 0 and take temporary variable to temporarily store user input (var = num)
  3. Now find out the total number of digits in the given number
  4. Total number of digits get stored in a
  5. Repeat the loop till var > 0
  6. Store the output of while loop in sum
  7. Check whether the user number is equal to sum or not
  8. If it is equal than print “It is an Armstrong number”
  9. Else print “It is not an Armstrong number”

Program of Armstrong Number in C