Write a program using functions to print armstrong series numbers.

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 */
  }
}

/* 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 Max range: 15000
Printing armstrong numbers from 0 - 15000
0, 1, 153, 370, 371, 407,

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