Strong Number in C
C is a general-purpose programming language, founded by Dennis Ritchie. It helps in developing system software, and different software applications. It is a high-level language and it is also referred to as structured programming which means to break a program into parts or blocks.
Strong numbers are referred to those numbers which on the sum of the factorial of the digits lead to be equal to the number. In simple words it can be defined as, if the sum of a factorial of digit of the number is equal to the number itself then we can refer to it as a strong number.
Algorithm for Checking the Strongest Number
The algorithm is:
Step 1: First we input a number from the user and store it in a variable name ‘num’.
Step 2: initialize another variable for the storage sum of the factorial, let it be sum = 0.
Step 3: In the third step, we find the last digit of the given num. After that we store the variable and last digit = num %10.
Step 4: Now we find the factorial of the last digit and store it in a variable name as ‘fact’.
Step 5: In step five we add the factorial as sum = sum + fact.
Step 6: Now we repeat the above steps until num is less than zero, or we can represent it as num > 0.
Step 7: In the last step, we check the condition for the strong number on looping. So after that if the sum == original Num, then we can say that the given number is strong otherwise it will not be considered as a strong number.
There are three ways of checking a strong number and they are mentioned below;
- Using Function
- Using While Loop
- Using for Loop
1. Program to find the strong number in C using functions
The code is given below:
#include <stdio.h>
int main()
{
int n;
int sum=0;
printf("Enter a number");
scanf("%d",&n);
int k=n;
int r;
while(k!=0)
{
r=k%10;
int f=fact(r);
k=k/10;
sum=sum+f;
}
if(sum==n)
{
printf("\nNumber is a strong");
}
else
{
printf("\nNumber is not a strong");
}
return 0;
}
int fact(int r)
{
int mul=1;
for(int i=1;i<=r;i++)
{
mul=mul*i;
}
return mul;
}
Output
2. Program to find the strong numbers from 1 to N number
The program is given below:
#include <stdio.h>
#include <conio.h>
int main ()
{
// to Initialize the variables
int k, l, current, last_digit, tend;
int fact, sum;
// Entering the upper limit taken by the user
printf ("Enter upper limit: ");
scanf ("%d", &tend);
printf ("All Strong numbers between 1 to %d are:\n", tend);
for (k=1; k<=tend; k++)
{
current = k;
sum = 0;
while (current > 0)
{
fact = 1ll;
last_digit = current % 10;
for (l=1; l<=last_digit; l++)
{
fact = fact * l;
}
sum += fact;
current /= 10;
}
if(sum == k)
{
printf ("%d, ", k);
}
}
return 0;
}
Output
3. Program for Strong Number Using FOR Loop
The code is:
#include <stdio.h>
#include<stdio.h>
int main ()
{
// to Initialize the variables
int num, count, fact, last_digit, sum = 0, temp;
// Taking input from the user
printf ("Enter the Number to be checked:\t");
scanf ("%d", &num);
// for the loop to compute the factorial of the last digit of the number
for (temp = num; num > 0; num = num / 10)
{
fact = 1;
last_digit = num % 10;
for (count = 1; count <= last_digit; count++)
{
fact = fact * count;
}
sum = sum + fact;
}
// to check the Condition whether the number is strong or not
if (sum == temp)
{
printf ("%d is a Strong number \n\n", temp);
}
else
{
printf ("%d is not a Strong number \n\n", temp);
}
return 0;
}
Output
4. Program using While LOOP
The code is:
#include<stdio.h>
#include<conio.h>
int main ()
{
// Initialize the different variables
int num, count, fact, last_digit, sum = 0, temp;
// Take the input from the user
printf ("Enter the Number to be checked:\t");
scanf ("%d", &num);
temp = num;
while(num)
{
count = 1, fact = 1;
last_digit = num % 10;
while (count <= last_digit)
{
fact = fact * count;
count++;
}
sum = sum + fact;
num = num / 10;
}
// Condition for checking the number is strong or not
if (sum == temp)
{
printf ("%d is a Strong number\n\n", temp);
}
else
{
printf ("%d is not a Strong number\n\n", temp);
}
return 0;
}
Output
Conclusion
Strong numbers are easily implemented by using various functions, and loops.