Armstrong Number Or Not

Armstrong number or not

Armstrong number is a C program that checks whether an integer or number inserted by the user is an Armstrong number or not. This program uses while loop and if-else statement to evaluate the same. A positive integer is said to be an Armstrong number of order n if abcd… = an + bn + cn + dn + …

In case the Armstrong number have three digits, then the sum of cubes of each digit will be equal to the number itself. Then ‘sum’ inconstant is used to calculate the sum of the number of ‘sum’ number along with the value of ‘cube’ number. The If-else situation statement is used to test both the value of ‘sum’ variable as well as the value of ‘temp’ (temporary) number are equal. In case of true condition it prints the Armstrong number and if not then it executes the same.

Algorithm to check if the entered digit is an Armstrong number or not:

Step 1: Start

Step 2: The user is asked to enter a number.

Step 3: Set sum = 0 and duplicate = number.

Step 4: remainder = number%10

Step 5: Sum=sum + (remainder * remainder * remainder)

Step 6: number = number

Step 7: repeat steps 4 to 6 till the number > 0

Step 8: if sum = duplicate

Step 9: The entered number is Armstrong

Step 10: If not, then the inserted number is not Armstrong

Step 11: Stop

Read Also: ASCII Values Of A Character

Program to check if the entered digit is an Armstrong number or not:


/*
 * C Program to check whether the entered Number is Armstrong
 */
#include 
#include 
 
void main()
{
    int num, sum = 0, rem = 0, cube = 0, temp;
 
    
printf("Insert a num");
    
scanf("%d", &num);
    temp = num;
    while (num != 0)
    {
        rem = num % 10;
        cube = pow(rem, 3);
        sum = sum + cube;
        num = num / 10;
    }
    if (sum == temp)
        
printf("The inserted num is Armstrong no");
    else
        
printf("The inserted num is not an Armstrong no");
}


Output

1. Insert a num: 370
The inserted num is Armstrong number 

2. Insert a num: 1500 
The inserted num is not an Armstrong number