Friday, 18 August 2017

Armstrong Number in C

Armstrong Number

An Armstrong number is a number such that the sum of its digits raised to the power n(number of digit in a number) is the number itself.

Example:
153 because 1^3 + 5^3 + 3^3 = 1 + 125 + 27 = 153.

Understanding of Program:

  • Firstly, we have to take input a number from a user.
  • Find the number of digits in the number with the help of the first "while loop" in the program.
  • Now, Find the n-power of the each digit of the number and sum it all.
  • At last, If the resultant number is equal to the number taken by the user, then it is an Armstrong number else it is not.

Program in C

#include<stdio.h>
#include<math.h>
void main()
{
  int num,temp,i,n=0,rem,result=0;
  printf("enter the number:\n");
  scanf("%d",&num);
  temp=num;
  while(temp>0)
   {
     temp=temp/10;
     n++;
   }
    temp=num;
    while(temp!=0)
    {
       rem=temp%10;
       result=result+pow(rem,n);
       temp=temp/10;
     }
    if(result==num)
         printf("Number is an Armstrong number");
     else
         printf("Number is not an Armstrong number");
}


OUTPUT

Enter the number:
153
Number is an Armstrong number

No comments:

Post a Comment