# Armstrong number C program

Armstrong number C program: C programming code to check whether a number is an Armstrong number or not. An Armstrong number is a number which is equal to the sum of digits raise to the power total number of digits in the number. Some Armstrong numbers are: 0, 1, 2, 3, 153, 370, 407, 1634, 8208, etc. Read more about Armstrong numbers. We will consider base ten numbers in our program. The algorithm to do this is: First we calculate the number of digits in our program and then compute the sum of individual digits raise to the power number of digits. If this sum equals the input number, then the number is an Armstrong number otherwise not.

Examples:
7 = 7^1
371 = 3^3 + 7^3 + 1^3 (27 + 343 +1)
8208 = 8^4 + 2^4 +0^4 + 8^4 (4096 + 16 + 0 + 4096).
1741725 = 1^7 + 7^7 + 4^7 + 1^7 + 7^7 + 2^7 +5^7 (1 + 823543 + 16384 + 1 + 823543 +128 + 78125)

## C programming code

1. #include <stdio.h>
2.
3. int power(int, int);
4.
5. int main()
6. {
7.    int n, sum = 0, temp, remainder, digits = 0;
8.
9.    printf("Input an integer\n");
10.    scanf("%d", &n);
11.
12.    temp = n;
13.    // Count number of digits
14.    while (temp != 0) {
15.       digits++;
16.       temp = temp/10;
17.    }
18.
19.    temp = n;
20.
21.    while (temp != 0) {
22.       remainder = temp%10;
23.       sum = sum + power(remainder, digits);
24.       temp = temp/10;
25.    }
26.
27.    if (n == sum)
28.       printf("%d is an Armstrong number.\n", n);
29.    else
30.       printf("%d isn't an Armstrong number.\n", n);
31.
32.    return 0;
33. }
34.
35. int power(int n, int r) {
36.    int c, p = 1;
37.
38.    for (c = 1; c <= r; c++)
39.       p = p*n;
40.
41.    return p;
42. }

Output of program:

## C program to check Armstrong number using function

We will use long long data type in our program so that we can check numbers up to 2^64-1.

1. #include <stdio.h>
2.
3. int check_armstrong(long long);
4. long long power(int, int);
5.
6. int main () {
7.    long long n;
8.
9.    printf("Input a number\n");
10.    scanf("%lld", &n);
11.
12.    if (check_armstrong(n) == 1)
13.       printf("%lld is an armstrong number.\n", n);
14.    else
15.       printf("%lld isn't an armstrong number.\n", n);
16.
17.    return 0;
18. }
19.
20. int check_armstrong(long long n) {
21.    long long sum = 0, temp;
22.    int remainder, digits = 0;
23.
24.    temp = n;
25.
26.    while (temp != 0) {
27.       digits++;
28.       temp = temp/10;
29.    }
30.
31.    temp = n;
32.
33.    while (temp != 0) {
34.       remainder = temp%10;
35.       sum = sum + power(remainder, digits);
36.       temp = temp/10;
37.    }
38.
39.    if (n == sum)
40.       return 1;
41.    else
42.       return 0;
43. }
44.
45. long long power(int n, int r) {
46.    int c;
47.    long long p = 1;
48.
49.    for (c = 1; c <= r; c++)
50.       p = p*n;
51.
52.    return p;
53. }

An output of the program:

1. Input a number
2. 35641594208964132
3. 35641594208964132 is an Armstrong number.