Write a program to find whether the given number is an armstrong number.

Source Code

#include <stdio.h>

int is_armstrong (int num)
{
  int sum = 0, temp, remainder;
  temp = num;

  while( temp != 0 )
  {
    /* Get the digit */
    remainder = temp % 10;
    /* calculate cube and sum up */
    sum = sum + (remainder * remainder * remainder);
    /* Shift right by one digit */
    temp = temp / 10;
  }

  if ( num == sum ) {
    return 1; /* armstrong number */
  } else {
    return 0; /* not armstrong number */
  }
}

/* Program to check if the number is an armstrong number */
/* Enable only one main() during compilation */
int main(int argc, char *argv[])
{
  int number;

  printf ("Enter an integer: ");
  scanf ("%d",&number);

  if ( is_armstrong(number) ) {
    printf ("%d is an armstrong number.\n", number);
  } else {
    printf ("%d is not an armstrong number.\n", number);
  }
  return 0;
}

/* Printing Armstrong number series */
int main(int argc, char *argv[])
{
  int range;
  printf ("Enter Max range: ");
  scanf ("%d", &range);
  printf ("Printing armstrong numbers from 0 - %d\n", range);
  for (number = 0; number <= range; number++) {
    if ( is_armstrong(number) ) {
      printf ("%d, ", number);
    }
  }
  return 0;
}

Output

Enter an integer: 153
153 is an armstrong number.

Find More from our code collection
Armstrong number, binary number to a decimal number, bubble sort, decimal number to binary number, factorial of the given number factors, fibonacci numbers, HCF and LCM, matrix, mergesort, salary of the employee. palindrome, quadratic equation, star patterns, series etc. and much more...
#Return to Example Source Code