This blog is under construction

Tuesday 9 July 2013

C program to check whether a number is strong or not

What's strong number?
If the sum of factorial of the digits of a number equals the number itself, then the number is called strong number.

40585 = 4! + 0! + 5! + 8! + 5! = 40585 is a strong number.

Write a C program to check whether a number is strong or not.


  #include <stdio.h>

  /* returns factorial for the the given number */
  int fact(int num) {
        int res = 1;

        while (num > 0) {
                res = res * num;
                num--;
        }

        return res;
  }

  int main() {
        int value, res = 0, temp, digit;

        /* get the input value from the user */
        printf("Enter your input value:");
        scanf("%d", &value);

        temp = value;

        /* find sum of factorial of digits of the given no */
        while (value > 0) {
                digit = value % 10;
                res = res + fact(digit);
                value = value / 10;
        }

        /* check whether the given no is strong or not */
        if (res == temp) {
                printf("Number %d is strong\n", temp);
        } else {
                printf("Number %d is not strong\n", temp);
        }
        return 0;
  }



  Output:
  jp@jp-VirtualBox:~/$ ./a.out
  Enter your input value:40585
  Number 40585 is strong



No comments:

Post a Comment