C program to find whether the number is Armstrong number or not.

Posted by Mangesh on March 16, 2018

Description :

A number is said to be Armstrong number, if sum of the cubes of its digits is equal to the number itself.
Example: 153 = 1*1*1 + 5*5*5 + 3*3*3 = 1 + 125 + 27.

Logic :

We can divide the number by 10 and get the remainder, the remainder here is also the right hand side digit. then we can divide the quotient by 10 and get the next digit. we shall do this till we get 0 as quotient. we can multiply the digit thrice by itself to get it cube.

Program :

#include<stdio.h>
#include<conio.h>
int armstrong(int num)
{  
  int total=0,rem;
  while (num != 0)
   {
      rem = num % 10;
      total = total + (rem*rem*rem);
      num = num / 10;
   }
   return total;
}
int main()
{
   int num,check;
   printf("\nEnter a number:");
   scanf("%d",&num);
   check = armstrong(num);
   if(check == num)
      printf("\n%d is an Armstrong Number.",num);
   else
      printf("\n%d is not an Armstrong Number.",num);
   getch();
}

Output :

C program for armstrong number` tested in Turbo C 3.2

Written with from Mangesh.

Related Post
1 C program to check whether given number is prime number or not.
2 C program for Fibonacci Series upto a limited ranges.
3 C program to find whether the number is Armstrong number or not.
4 C program to convert decimal number to octal number.
Latest Post
1 shubhanshu
2 test
3 C program for Breadth First Search.
4 C program for Depth First Search(DFS).
5 C for circular queue using linked list.