Ticker

6/recent/ticker-posts

Header Ads Widget

Responsive Advertisement

Armstrong Number using C

Problem Statement

In this program we will code a basic program to check whether a given number is an armstrong number or not.

Code

#include

bool armstrong(int n)

{

 int num;

 int rem=0,sum=0;

 num=n;

 while(n>0)

 {

   rem=n%10;

   sum=sum+rem*rem*rem;

   n=n/10;

 }

 if(sum==num)

 {

   return true;

 }

 return false;

}

int main()

{

  int n;

  printf("ENTER THE NUMBER : ");

  scanf("%d",&n);

  if(armstrong(n))

  {

    printf("Armstrong");

  }

  else

  {

    printf("Not Armstrong");

  }

  return 0;

}

Output

ENTER THE NUMBER : – 153

Armstrong

Explanation 

In this problem we have to check whether a given number is an armstrong number or not.An Armstrong number is an integer such that the sum of the cubes of its digits is equal to the number itself. For example, 371 is an Armstrong number since 3*3*3 + 7*7*7 + 1*1*1 = 371. To do so we declare a variable ‘sum’,which will store the result value(don’t forget to initialize it with zero).Then,we update our ‘sum’ value by adding cube of remainder value(rem*rem*rem) to it in each iteration(until ‘n’ becomes 0).
After the last iteration,we compare the value of given number and our sum ,if both values are equal then given number is armstrong else it isn’t.

Post a Comment

0 Comments