In this tutorial you will learn about the C Program to Check Armstrong Number and its application with practical example.
In this tutorial, we will learn to create a program to check whether a given number is an Armstrong number or not using C programming language.
Prerequisites
Before starting with this tutorial we assume that you are best aware of the following C programming topics:
- C if else
- C while loop
What Is Armstrong Number?
An Armstrong number of a 3 digit number is a number in which the sum of the cube of the individual digits in number is equal to the number itself. For example 0, 1, 153, 370, 371 and 407 are the Armstrong numbers. Let’s try to understand why 153 is an Armstrong number.
153 = ((1*1*1)+(5*5*5)+(3*3*3) )
153 =( 1 + 125 + 27)
153 = 153 (So this is an Armstrong number.)
Similarly, Let’s take number 370 and understand why 370 is an Armstrong number.
370=(3*3*3+7*7*7+0*0*0)
370=(27+343+0)
370=370.(So this is a Armstrong number.)
So this is how we can check whether a given 3 digit number is an Armstrong number or not
C Program to Check Armstrong Number
In this program we will find whether a given three digit number is Armstrong number or not using while loop. We would first declared and initialized the required variables. Next, we would prompt user to input a three digit number. Later we will find given no is Armstrong number or not.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
#include <stdio.h> int main() { int no,copyno,rem,armno; printf("Enter any 3 digit no: "); scanf("%d",&no); copyno=no; // here we copy the giving by user to variable copyno. while(copyno!=0) { rem=copyno%10; //it holds last digit armno+=rem*rem*rem; copyno/=10 // it removes last digit form original number. } if(no==armno) { printf(“%d is a Armstrong number ”,no); } else { printf(“%d is not a Armstrong number ”,no) } return 0; } |
Output:-
1 2 |
Enter any 3 digit no: 153 153 is a Armstrong number |
In the above program, we have first declared and initialized a set variables required in the program.
- no = take given number by user.
- copyno = to copy original no.
- rem= for holding remainder
- armno= for result
Here we created a program in which we find a three digit number is a Armstrong number or not. First we copy the original no to copyno variable and then with the help of while loop iterate loop until we reach the last value or given no.
And in loop we break the number’s last digit and multiply this number three time itself and store in armno variable and after that we break copyno to two digit no with (/) operator and copy the new two digit number in our copyno variable and repeat this process to last number then we compare the given number and armno is equal it to the given no if they are equal then the number is Armstrong number else the given number is not a Armstrong number.