Friday, January 1, 2016

C program to check whether a number is an Armstrong number

/* Also known as narcissistic numbers,
 * Armstrong numbers are the sum of their own digits
 * to the power of the number of digits.
 * Example: 407 = 4^3 + 0^3 + 7^3 = 407
 */
#include <stdio.h>
#include <math.h>
int main(void) {
  int number_to_check = 153;
  int copy_number = number_to_check;
  int digits[10];
  int i = 0, sum = 0, number_of_digits;
  while(number_to_check!=0){
    digits[i] = number_to_check % 10;
    number_to_check = number_to_check/10;
    i++;
  }
  number_of_digits = i;
  for(i = number_of_digits-1; i >= 0; i--){
    sum = sum + pow(digits[i], number_of_digits);
  }
  if(copy_number == sum)
    printf("The supplied number is an armstrong number.");
  else
    printf("The supplied number is NOT an armstrong number.");
  return 0;
}

1 comment:

  1. I need GATE MCQ's on mobile computing and wireless networks.
    Thanks in advance.
    Please update link on your page urgently.

    ReplyDelete