C Program to Generate Strong Numbers in an Interval

Strong numbers are those numbers whose sum of factorial of each digits is equal to the original number. For example 1 is strong number because 1!=1, 2 is strong number i.e. 2! = 2, 145 is strong number i.e. 1! + 4! + 5! = 1 + 24 + 120 = 145 etc.

In this C program, we are going to generate all strongs number in a given ranges from minimum to maximum. Values of minimum and maximum are inputted by user.

Program


#include<stdio.h>
#include<conio.h>

int main()
{
	 long int minimum, maximum, number, sum, fact, rem, i, j;
	 clrscr();
	 printf("Enter minimum: ");
	 scanf("%ld", &minimum);
	 printf("Enter maximum: ");
	 scanf("%ld", &maximum);
	
	 /* Generating Strong Numbers */
	 for(i=minimum; i<=maximum; i++)
	 {
	
		  number = i;
		  sum = 0;
		  while(number != 0)
		  {
			   rem = number%10;
			
			   /* Finding Factorial */
			   fact = 1;
			   for(j=1;j<=rem; j++)
			   {
			    	fact = fact * j;
			   }
			
			   sum = sum + fact;
			
			   number = number/10;
		  }
		
		  /* Making Decision */
		  if(sum == i)
		  {
		   	printf("%ld\t", i);
		  }
	 }
	 
	 getch();
	 return(0);
}

Output of the above program :

Enter minimum: 1 ↲
Enter maximum: 99999 ↲
1   2   145    40585

Note: ↲ indicates enter is pressed.