Checking for Armstrong Numbers in C Programming


This program is written in C and it is used to check whether a given 3-digit number is an Armstrong number or not.

An Armstrong number, also known as a narcissistic number, is a number that is equal to the sum of its own digits each raised to the power of the number of digits.

The program starts with the inclusion of the header file "stdio.h" which contains the functions used in the program, like printf() and scanf(). In the main function, the program declares four integer variables: n, a, d1, d2, d3. The user is prompted to enter a 3-digit number which is stored in the variable n using the scanf() function.

Then, the program uses the modulus operator to separate the last digit of the entered number and store it in the variable d3. The quotient of the division of n by 10 is stored in the variable a. The last digit of a is stored in the variable d2 and the first digit of a is stored in the variable d1.

The program then calculates the sum of the cubes of each digit, which is d1d1d1 + d2d2d2 + d3d3d3. If this sum is equal to the original number entered by the user, the program prints that the number is an Armstrong number. If the sum is not equal to the original number, the program prints that the number is not an Armstrong number.

Finally, the program returns 0 to indicate successful execution.

  Examples:
     153 = ( 1*1*1 ) + ( 5*5*5 ) + ( 3*3*3 )
              = 1 + 125 + 27
              = 153 is Armstrong Number

     237 = ( 2*2*2 ) + ( 3*3*3 ) + ( 7*7*7 )
              = 8 + 27 + 343
              = 378 is Not Armstrong Number


Source Code

/*
Three digit number input through the keyboard write a program
to find the given number is Armstrong number or not.
 
*/
 
#include<stdio.h>
 
int main()
{
    int n,a,d1,d2,d3;
    printf("\nEnter 3 Digit No : ");
    scanf("%d",&n);//153
    d3=n%10;//3
    a=n/10;//15
    d2=a%10;//5
    d1=a/10;//1
    //printf("%d   %d   %d",d1,d2,d3);
    a=(d1*d1*d1)+(d2*d2*d2)+(d3*d3*d3);
    if(a==n)
    {
        printf("%d is an armstrong number",n);
    }
    else
    {
         printf("%d is not an armstrong number",n);
    }
    return 0;
}
 
To download raw file Click Here

Output

Enter 3 Digit No : 153
153 is an armstrong number
Enter 3 Digit No : 237 237 is not an armstrong number

List of Programs


Sample Programs


Switch Case in C


Conditional Operators in C


Goto Statement in C


While Loop Example Programs


Looping Statements in C

For Loop Example Programs


Array Examples in C

One Dimensional Array


Two Dimensional Array in C


String Example Programs in C


Functions Example Programs in C