Let's illustrate with a practical demonstration.
- Code to verify if a number is a strong number or not.
Example
#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;
}
In the provided code snippet, the user's input data is first captured, followed by a validation process to determine if the input constitutes a strong number.
Output
Output
Enter a number
Number is a strong
Number is not a strong
- Program to print the strong numbers from 1 to n.
Example
#include<stdio.h>
int main()
{
int fact=1,sum=0;
int n,r;
printf("Enter the 'n' number");
scanf("%d",&n);
printf("\n Strong numbers are :");
for(int i=1;i<=n;i++)
{
int k=i;
while(k!=0)
{
r=k%10;
fact=factorial(r);
k=k/10;
sum=sum+fact;
}
if(sum==i){
printf("%d, ",i);
}
sum=0;
}
return 0;
}
int factorial(int f)
{
int mul=1;
for(int i=1; i<=f;i++)
{
mul=mul*i;
}
return mul;
}
Output
Output
Enter a number
Number is a strong
Number is not a strong
Develop a script to identify strong numbers within a specified range.
Example
#include<stdio.h>
int main()
{
int fact=1,sum=0;
int n1,n2,r;
printf("Enter the first number");
scanf("%d",&n1);
printf("\nEnter the last number");
scanf("%d",&n2);
printf("\nStrong numbers are :");
for(int i=n1;i<=n2;i++)
{
int k=i;
while(k!=0)
{
r=k%10;
fact=factorial(r);
k=k/10;
sum=sum+fact;
}
if(sum==i){
printf("%d, ",i);
}
sum=0;
}
return 0;
}
int factorial(int f)
{
int mul=1;
for(int i=1; i<=f;i++)
{
mul=mul*i;
}
return mul;
}
Output: