Welcome...

Monday, 6 January 2014

C program to find whether a given number is a strong number or not

A strong number is a number whose sum of factorials of the individual numbers is equal to the number itself. For example, consider the number 145. In this number 1! + 4! + 5! is equal to the number 145 itself. So, 145 is an example for strong number.

/*Program to find whether a given number is strong number or not*/
#include<stdio.h>
#include<conio.h>
main()
{
    int num,tnum,sum;
    clrscr();
    printf("Enter a positive integer:");
    scanf("%d",&num);
    tnum = num;
    sum = 0;
    while(num!=0)
    {
        sum += fact(num%10);
        num = num/10;
    }
    if(sum==tnum)
    {
        printf("Given number is a strong number");
    }
    else
    {
        printf("Given number is not a strong number");
    }
    getch();
}
/*Logic to calculate the factorial of individual numbers*/
int fact(int n)
{
    int f;
    if(n==1)
    f=1;
    else
    f = n*fact(n-1);
    return f;
}


Sample input and output:

Enter a positive integer: 145
Given number is a strong number