ADVERTISEMENT
Inputted Number is Strong or Not
#include<stdio.h>
#include<math.h>
int GetFactorial(int number);
main()
{
int num,i,j,temp,sum=0;;
printf("Enter a number to know whether it is armstrong or not\n");
scanf("%d",&num);
temp=num;
while(num>0)
{
i=num%10;
sum+=GetFactorial(i);
num=num/10;
}
if(sum==temp)
{
printf("Giveb number %d is an Strong number\n",temp);
}
else
{
printf("Giveb number %d is not an Strong number since the sum of factorials of individual digits is %d\n",temp,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 whether it is armstrong or not
28
Giveb number 28 is not an Strong number since the sum of factorials of individual digits is 40322C Programming ExamplesExplanation
Below is an explanation of the code:
- Header Files:
#include<stdio.h>
#include<math.h>
The program includes necessary header files. stdio.h is for standard input and output functions, and math.h is for mathematical functions.
- Function Declaration:
int GetFactorial(int number);
Declares a function named GetFactorial that calculates the factorial of a given number.
- Main Function:
main()
Defines the main function, which is the entry point of execution.
- Variable Declaration:
int num, i, j, temp, sum = 0;
Declares integer variables: num to store the user-inputted number, i for digit extraction, j (unused), temp to store the original number for later comparison, and sum to accumulate the factorials.
- User Input:
printf("Enter a number to know whether it is Armstrong or not\n");
scanf("%d", &num);
Prompts the user to enter a number and stores the input in the variable num.
- Digit Extraction and Factorial Summation:
temp = num;
while(num > 0)
{
i = num % 10;
sum += GetFactorial(i);
num = num / 10;
}
Iterates through each digit of the input number, calculates the factorial of each digit using the GetFactorial function, and accumulates the factorials in the variable sum.
- Armstrong Number Check:
if(sum == temp)
{
printf("Given number %d is an Armstrong number\n", temp);
}
else
{
printf("Given number %d is not an Armstrong number since the sum of factorials of individual digits is %d\n", temp, sum);
}
Compares the sum of factorials (sum) with the original number (temp). If they are equal, it prints a message indicating that the number is an Armstrong number; otherwise, it indicates that the number is not an Armstrong number.
- Factorial Calculation Function:
int GetFactorial(int number)
{
int factorial = 1, i;
for(i = 1; i <= number; i++)
{
factorial = factorial * i;
}
return factorial;
}
Defines the GetFactorial function, which calculates and returns the factorial of a given number using a for loop.