ADVERTISEMENT
N Strong Numbers
#include<stdio.h>
#include<math.h>
int GetFactorial(int number);
main()
{
int num,i,j,temp1,temp2,sum=0;;
printf("Enter a number to know all Strong number between them\n");
scanf("%d",&num);
printf("Strong numbers are:\n");
for(i=1;i<=num;i++)
{
sum=0;
temp1=i;
temp2=i;
while(temp1>0)
{
j=temp1%10;
sum+=GetFactorial(j);
temp1=temp1/10;
}
if(sum==temp2)
{
printf("%d\n",sum);
}
}
}
int GetFactorial(int number)
{
int factorial=1,i;
for(i=1;i<=number;i++)
{
factorial=factorial*i;
}
return factorial;
}C Programming ExamplesEnter a number to know all Strong number between them
100000
Strong numbers are:
1
2
145
40585C Programming ExamplesCertainly! Let’s go into more detail about each part of the code:
Certainly! Let’s go into more detail about each part of the code:
- Header Files:
#include<stdio.h>
#include<math.h>
These lines include the necessary header files. stdio.h is included for input and output operations, and math.h is included for mathematical functions. Although the math.h file is included, it is not currently being used in the code.
- Function Declaration:
int GetFactorial(int number);
This line declares a function named GetFactorial that takes an integer argument number and returns an integer. This function is later defined to calculate the factorial of a given number.
- Main Function:
main()
{
The program starts executing from the main function.
- Variable Declarations:
int num, i, j, temp1, temp2, sum = 0;
Here, variables are declared to store the user input (num), loop counters (i, j), and temporary values (temp1, temp2, sum).
- User Input:
printf("Enter a number to know all Strong numbers between them\n");
scanf("%d", &num);
The user is prompted to enter a number, and the input is stored in the variable num.
- Display Message:
printf("Strong numbers are:\n");
This line prints a message to inform the user that the program will display the Strong numbers.
- Loop to Find Strong Numbers:
for(i = 1; i <= num; i++)
{
A for loop is initiated, running from 1 to the user-input number (num). This loop iterates over each number within the specified range.
- Initialization:
sum = 0;
temp1 = i;
temp2 = i;
Variables are initialized for the current iteration of the loop. sum is set to 0, and temp1 and temp2 are set to the current loop variable i.
- Calculate Factorial Sum:
while(temp1 > 0)
{
j = temp1 % 10;
sum += GetFactorial(j);
temp1 = temp1 / 10;
}
A while loop is used to extract each digit (j) from temp1, calculate its factorial using the GetFactorial function, and add it to the sum. This loop continues until all digits are processed.
- Check for Strong Number:
if(sum == temp2)
{
printf("%d\n", sum);
}
After the loop, the program checks if the calculated sum (sum) is equal to the original number (temp2). If true, it means the current number is a Strong number, and it is printed.
- Factorial Calculation Function:
int GetFactorial(int number)
{
int factorial = 1, i;
for(i = 1; i <= number; i++)
{
factorial = factorial * i;
}
return factorial;
}
This function, GetFactorial, takes an integer number as an argument and calculates its factorial using a for loop. The calculated factorial is then returned.
The program continues iterating through the loop, checking and printing Strong numbers, until it completes for all numbers in the specified range.