Strong Number or Not in C Program
ADVERTISEMENTS
Strong number or not in the C program.
In this article, you will learn how to check whether a given number is strong.
What is the strong number?
The sum of the factorial of the digits of the input number is equal to the number itself.
Example-1 A Strong Number
145! = 1! + 4! + 5!
= 1 + 24 + 120
= 145
Sum of the factorial of the digits is equal to 145
= 1 + 24 + 120
= 145
Sum of the factorial of the digits is equal to 145
Example-2 Not a Strong Numer
123! = 1! + 2! + 3!
= 1 + 2 + 6
= 9
Sum of the factorial of the digits is not equal to 123
= 1 + 2 + 6
= 9
Sum of the factorial of the digits is not equal to 123
You should know about the following topics in C programming to understand this program:
- If-else statement
- While loop in C
- Do-while loop in C
- For loop in C
1 - Strong Number or Not in C using While loop
// Strong Number or Not in C using While loop
#include <stdio.h>
// It's the main function of the program
int main()
{
int num, factorial, reminder, sum = 0;
// Step-1 Take input from the User
printf("INPUT A NUMBER: ");
scanf("%d", &num);
// Step-2 Check input number should be positive
if (num > 0)
{
int original = num, i;
// Step-3 Use a loop to check the number
while (num)
{
i = 1;
factorial = 1;
reminder = num % 10;
while (i <= reminder)
{
factorial = factorial * i;
i++;
}
sum = sum + factorial;
num /= 10;
}
// Step-4 Compare the value of the sum of digits with the original input value
if (sum == original)
printf("%d IS A STRONG NUMBER.\n", original);
else
printf("%d IS NOT A STRONG NUMBER.\n", original);
}
else
printf("Enter a positive number!\n");
return 0;
}
Output
INPUT A NUMBER: 145
145 IS A STRONG NUMBER.
2 - Strong Number or Not in C using Do-while loop
// Strong Number or Not in C using Do-while loop
#include <stdio.h>
// It's the main function of the program
int main()
{
int num, factorial, reminder, sum = 0;
// Step-1 Take input from the User
printf("INPUT A NUMBER: ");
scanf("%d", &num);
// Step-2 Check input number should be positive
if (num > 0)
{
int original = num, i;
// Step-3 Use a loop to check the number
do
{
i = 1;
factorial = 1;
reminder = num % 10;
do
{
factorial = factorial * i;
i++;
} while (i <= reminder);
sum = sum + factorial;
num /= 10;
} while (num);
// Step-4 Compare the value of the sum of digits with the original input value
if (sum == original)
printf("%d IS A STRONG NUMBER.\n", original);
else
printf("%d IS NOT A STRONG NUMBER.\n", original);
}
else
printf("Enter a positive number!\n");
return 0;
}
Output
INPUT A NUMBER: 45
45 IS NOT A STRONG NUMBER.
3 - Strong Number or Not in C using For loop
// Strong Number or Not in C using For loop
#include <stdio.h>
// It's the main function of the program
int main()
{
int num, factorial, reminder, sum = 0;
// Step-1 Take input from the User
printf("INPUT A NUMBER: ");
scanf("%d", &num);
// Step-2 Check input number should be positive
if (num > 0)
{
// Step-3 Use a loop to check the number
for (int k = num; k > 0; k /= 10)
{
factorial = 1;
reminder = k % 10;
for (int i = 1; i <= reminder; i++)
factorial = factorial * i;
sum = sum + factorial;
}
// Step-4 Compare the value of the sum of digits with the original input value
if (sum == num)
printf("%d IS A STRONG NUMBER.\n", num);
else
printf("%d IS NOT A STRONG NUMBER.\n", num);
}
else
printf("Enter a positive number!\n");
return 0;
}
Output
INPUT A NUMBER: 40585
40585 IS A STRONG NUMBER.