Problem:  Write a program in C to check whether the given positive number is Armstrong or not.

Armstrong Number is a number whose sum of ‘powered digits with the “total number of digits in the number” ‘ is equal to the original number.

Check Armstrong number in C

Example 1: Check Armstrong for three Digits Number

  1. Take positive integer input into original_number
  2. Copy the original_number to the number
  3. Run while loop until number != 0
  4. Extract digit by dividing the number by 10( Remainder is the digit)
  5. Add cube of digit into sum as sum = sum + (digit*digit*digit)
  6. Update the number = number/10 (Getting rid of the last digit which has been used as the remainder in step 5)
  7. End loop
  8. Check if the sum == original_number
  9. If YES the Armstrong Number else NOT.
#include <stdio.h>
#include <stdlib.h>
 
int main()
{
    int number, original_number, sum=0;
    int digit;
 
    printf("Enter a number \n");
    scanf("%d",&original_number);
 
    number = original_number; //Copying the original number
 
    while(number != 0){
        digit = number % 10;
        sum += digit * digit *digit;
        number=number/10;
    }
 
    if(sum == original_number)
        printf("Armstrong Number \n");
    else
        printf("Not an Armstrong Number");
 
    return 0;
}

Output

When the input is 121 which is not an Armstrong Number.

Armstrong number in C

When the input is 153 which is an Armstrong Number.

Armstrong number C

Example 2: Check Armstrong for N digits Number

  1. Take a number as input.
  2. Copy the original number into temporary variable i.e temp=num.
  3. Using the While loop counts the number of digits in temp and store it as length. Update temp = temp / 10 until temp != 0.
  4. Reinitialize the temp variable with the original number.
  5. Using another while loop with the same condition, extract each digit as digit = temp%10 and update the sum as sum = sum + digit^length.
  6. Outside loop, check if sum==originalnumber. If yes then output “Armstrong Number, else “Not Armstrong number”.
#include <stdio.h>
#include <math.h>
 
int main(){
    int num, digit, sum = 0, length = 0;
    int temp;
 
    printf("Enter a number \n");
    scanf("%d",&num);
 
    temp = num;  //Copying the original number for operation on it
 
    //Counting how many digits do temp (i.e num) has
    while(temp != 0){
        length++;
        temp = temp / 10;
    }
    //On counting digits, the value of temp becomes 0
    //So we again assign the fresh copy to it
    temp = num;
 
    while(temp != 0){
        digit = temp % 10;
        sum += floor(pow(digit,length));
        temp = temp / 10;
    }
 
 
    //Output Result
    if(sum == num)
        printf("Armstrong number \n");
    else
        printf("Not an Armstrong number \n");
 
    return 0;
}

Output

C Program for Armstrong number

Checking 4 digit number.

Output of C Program

Example 3: Check Armstrong from 1 to 1000

In this C Program, we will check and print all Armstrong numbers in the range from 1 to 1000.

#include <stdio.h>
#include <math.h>
 
int main(){
    int num, digit, sum = 0;
    int i, temp;
 
    for(i=1; i<1000; i++){
 
        num = i;
        sum = 0;
 
        temp = num;  //Copying the number 
 
        while(temp != 0){
            digit = temp % 10;
            sum += floor(pow(digit,3));
            temp = temp / 10;
        }
 
 
        //Output Result
        if(sum == num)
            printf("%d, ",num);
 
    }
    return 0;
}

Output

Armstrong Number between 1 to 1000

In this tutorial, we learned to check Armstrong number in C with examples.

Leave a Reply

one × three =