EQuestionAnswers.com Computer/Electronics Questions and Answers
C, C++, VC++, COM/DCOM, DLL and more
#Login #Sign up  Facebook Twitter TGoogle+
 

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 )
  {
10      /* Get the digit */
11      remainder = temp % 10;
12      /* calculate cube and sum up */
13      sum = sum + (remainder * remainder * remainder);
14      /* Shift right by one digit */
15      temp = temp / 10;
16    }
17 
18    if ( num == sum ) {
19      return 1; /* armstrong number */
20    } else {
21      return 0; /* not armstrong number */
22    }
23  }
24 
25  /* Program to check if the number is an armstrong number */
26  /* Enable only one main() during compilation */
27  int main(int argc, char *argv[])
28  {
29    int number;
30 
31    printf ("Enter an integer: ");
32    scanf ("%d",&number);
33 
34    if ( is_armstrong(number) ) {
35      printf ("%d is an armstrong number.\n", number);
36    } else {
37      printf ("%d is not an armstrong number.\n", number);
38    }
39    return 0;
40  }
41 
42  /* Printing Armstrong number series */
43  int main(int argc, char *argv[])
44  {
45    int range;
46    printf ("Enter Max range: ");
47    scanf ("%d", &range);
48    printf ("Printing armstrong numbers from 0 - %d\n", range);
49    for (number = 0; number <= range; number++) {
50      if ( is_armstrong(number) ) {
51        printf ("%d, ", number);
52      }
53    }
54    return 0;
55  }
56 

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