C Program to Check Armstrong Number

Last Updated:

What is Armstrong Number?

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

For example, 153 is an armstrong number

153 = 13 + 53 + 33
153 = 1 + 125 + 27
153 = 153

Note: Each number is raised to the power of 3, because, the number of digits in 153 is 3.

Tips: It is recommended to use our online Armstrong Number calculator for better understanding.

Check Armstrong Number

In the following example, we will check whether the number 19 is an Armstrong number or not.

Example

C Compiler
#include <stdio.h> #include <math.h> int main() { int num = 19; int copyNum = num; int digits = 0; int balance = 0; int total = 0; // find number of digits in num variable while(copyNum != 0) { digits++; copyNum = copyNum / 10; } copyNum = num; // slice the numbers from last digits while(copyNum != 0) { balance = copyNum % 10; total += pow(balance, digits); copyNum = copyNum/ 10; } // result if(num == total) printf("%d is an armstrong number", num); else printf("%d is not an armstrong number", num); return 0; }

Armstrong Numbers between the Given Range

In the following example, we will find all the Armstrong numbers between 1 and 200.

Example

C Compiler
#include <stdio.h> #include <math.h> int main() { int start = 1; int end = 200; int flag = 0; int copyNum = 0; int total = 0; int digits = 0; int balance = 0; for(start=start; start<=end; start++) { // find the number of digits in start variable copyNum = start; total = 0; digits = 0; balance = 0; while(copyNum != 0) { digits++; copyNum = copyNum / 10; } copyNum = start; // slice the start variable from last digit while(copyNum != 0) { balance = copyNum % 10; total += pow(balance, digits); copyNum = copyNum/ 10; } // result if((start == total) && (start != 0)) { if(flag == 0) { printf("Armstrong numbers between %d and %d:\n", start, end); flag = 1; } printf("%d ", start); } } if(flag == 0) printf("There is no armstrong numbers"); return 0; }

Check Armstrong Number for any Given Number

In the following example, we will find whether the user entered number is an Armstrong number or not.

Example

C Compiler
#include <stdio.h> #include <math.h> int main() { int num, copyNum; int digits = 0; int balance = 0; int total = 0; printf("Enter a (int) number: "); scanf("%d", &num); copyNum = num; // find number of digits in num variable while(copyNum != 0) { digits++; copyNum = copyNum / 10; } copyNum = num; // slice the numbers from last digits while(copyNum != 0) { balance = copyNum % 10; total += pow(balance, digits); copyNum = copyNum/ 10; } // result if(num == total) printf("%d is an armstrong number", num); else printf("%d is not an armstrong number", num); return 0; }

Share this Page

Meet the Author