C Program to Check Whether Number is Armstrong or Not

Using this tutorial, We are going to execute the script of “C Program to Check Whether Number is Armstrong or Not”. Below, We can see the possibilities to perform the task using this tutorial.

C Program to Check Whether Number is Armstrong or Not

C Program to Check Whether Number is Armstrong or Not

Program Code

#include <stdio.h>
int power(int, int);

int main()
{
  int n, sum = 0, t, remainder, digits = 0;

  printf("Input an integern");
  scanf("%d", &n);

  t = n;
  // Count number of digits
  while (t != 0) {
    digits++;
    t = t/10;
  }

  t = n;

  while (t != 0) {
    remainder = t%10;
    sum = sum + power(remainder, digits);
    t = t/10;
  }

  if (n == sum)
    printf("%d is an Armstrong number.n", n);
  else
    printf("%d isn't an Armstrong number.n", n);

  return 0;
}

int power(int n, int r) {
  int c, p = 1;

  for (c = 1; c <= r; c++)
    p = p*n;

  return p;
}

Conclusion

I hope this article helps you to learn “C Program to Check Whether Number is Armstrong or Not”. If you face any issues please let me know via the comment section. Share this article with other C/C++ developers via social networks.

Share on:

Hi, I'm Ranjith a full-time Blogger, YouTuber, Affiliate Marketer, & founder of Coding Diksha. Here, I post about programming to help developers.

Leave a Comment