Write a C program to check whether a given number is an Armstrong number or not.

2 min 45 sec Basic


An Armstrong number is a number that equals the sum of the cube of each digit in a number. 

For example, suppose a given number is 153. 

To check whether a given number is Armstrong or not, we have to take the cube of each digit in a number and take a sum of those.

153 = (1*1*1) + (5*5*5) + (3*3*3)

After the addition of the cube of each digit, we get the original number which is 153. Hence, the given number is Armstrong.

#include <stdio.h>
int main()
{
   int n = 153, sum = 0, remainder = 0,tempNumber;
   tempNumber = n;
   while(tempNumber!=0)
   {
      remainder = tempNumber % 10;
      sum = sum + (remainder*remainder*remainder);
      tempNumber /= 10;
   }

   if(sum==n)
   {
     printf("%d is an Armstrong number.",n);
   }
   else
   {
     printf("%d is not an Armstrong number.",n);
   }
   return 0;
  }

Output

153 is an Armstrong number

Program Logic

  1. First, we take the original number into a temporary variable which is tempNumber.
  2. Then, we get the last digit of a temporary number using the modulo method inside a loop.
  3. After getting the last digit of a number, we added it with the cube of that last digit number.
  4. Then assigned the remaining digits except for the last digit of a number using the Quotient method.
  5. This process will be in the loop till we find that givenNumber do not reach 0.

If at the end we find that this new number after addition is found as. same or not. If yes, then the given number is an Armstrong number else it is not an Armstrong number.

C Program to check whether a given number is Armstrong or not using a user-defined function

#include <stdio.h> 
int isArmstrong(int n)
{
  int tempNumber=n;
  int remainder,sum;
  sum=0;
  while(tempNumber!=0)
  {
    remainder=tempNumber%10;
    sum = sum + (remainder*remainder*remainder);
    tempNumber/=10;
  }

if(sum==n) 
   return 1
else
   return 0;
}

int main()
{
  int n = 153;
  if(isArmstrong(n))
      printf("%d is an Armstrong number.",n);
  else
      printf("%d is not an Armstrong number.",n);
  return 0;
}

Output

153 is an Armstrong number

How was the tutorial? Nice. Right?



Deep Concept videos to crack the highly-piad interviews.

Chekout out our Youtube Channel to get detailed video content on important topics in interviews.



What is your Interview Score?

Test your skillset with the curated questions created by experts around the globe.

Book a free test slot. Now !.

It usually takes 30 mins for an online test and this test will be MCQ based. Get detailed analytics based on your test and recommedations with personalised roadmaps.

Book a test slot. Now!

Have questions about the trial coding class?

Chat with our experts to discuss

Connect on WhatsApp


Recommended tutorials

#online compiler for c     #python for programming    

#dfs and bfs algorithm     #programming with c language

#storageclass in C    #listcomprehension in python