Check the number is Armstrong or not using C


Problem

How to check whether the given number is an Armstrong number or not using C Programming language?

Solution

Armstrong number is the number that is equal to the sum of cubes of its digits.

Syntax

pqrs………=pow(p,n)+pow(q,n)+pow(r,n)+……….

For example, 153,371,1634, etc., are Armstrong numbers.

153=1*1*1 + 5*5*5 + 3*3*3
   =1+125+27
   =153 (Armstrong number)

Program

 Live Demo

#include<stdio.h>
int main(){
   int number,remainder,total=0,temp;
   printf("enter the number=");
   scanf("%d",&number);
   temp=number;
   while(number>0){
      remainder=number%10;
      total=total+(remainder*remainder*remainder);
      number=number/10;
   }
   if(temp==total)
      printf("This number is Armstrong number");
   else
      printf("This number is not Armstrong number");
   return 0;
}

Output

enter the number=371
This number is Armstrong number
Check: 371=3*3*3 +7*7*7 + 1*1*1
           =27 + 343 +1
           =371
enter the number=53
This number is not Armstrong number

Explanation

53 = 5*5*5 + 3*3*3
   =125 +27
   = 152 != 53

Updated on: 05-Mar-2021

8K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements