C program to check if a number is a Strong number or not

C program to check if a number is a strong number or not:

In this post, we will learn how to check if a number is a strong number or not. This program will take one number as input from the user and print out a message if it is a strong number or not. We will also write a program to find all strong numbers in a given range.

Before we start to write the program, let’s learn what is a strong number and how to find if a number is strong or not.

What is a strong number:

A number is called a strong number if the sum of factorials of each digit of the number is equal to the number itself.

If abc is a strong number, a! + b! + c! is equal to abc.

For example, 145 is a strong number because the sum of factorials of its digits 1, 4, and 5 is 1! + 4! + 5! = 1+ 24 + 120 = 145.

Algorithm:

We need two algorithms: one is to find the factorial of a number and another is to pick the digits of a number.

To find the factorial of a number:

  • Initialize one variable as 1 to hold the factorial.
  • Run one while loop till the number becomes 0.
  • Multiply the current number with the factorial variable and change the number to number - 1
  • Once the loop will end, the factorial variable will hold the value of 1 * 2 * … number, i.e. the factorial of the number.

We can also run a for loop from 1 to number and multiply each value to find the factorial.

To pick each digit of a number:

  • Use modulo operator number % 10 to get the last digit of a number.
  • Change the number to number/10, i.e. remove the last digit.
  • Keep doing this till the number becomes 0.
  • If the sum of factorial of each digit of a number is equal to the number, we can say that this is a strong number.

C program to check if a number is strong or not:

Let’s write down a C program to check if a number is a strong number or not:

#include <stdio.h>

int getFactorial(int num)
{
    int factorial = 1;

    while (num > 0)
    {
        factorial *= num;
        num -= 1;
    }

    return factorial;
}

int main()
{
    int num, sumFactorial = 0, tempNum;

    printf("Enter a number: ");
    scanf("%d", &num);
    tempNum = num;

    while (tempNum > 0)
    {
        sumFactorial += getFactorial(tempNum % 10);
        tempNum /= 10;
    }

    if (sumFactorial == num)
    {
        printf("%d is a strong number\n", num);
    }
    else
    {
        printf("%d is not a strong number\n", num);
    }

    return 0;
}

Here,

  • getFactorial is a method to get the factorial of a number. It takes one integer number and returns the factorial of that number.
  • Inside main, num variable is to store the user input number, sumFactorial is to hold the sum of factorials of each digit of the number num and tempNum is to store a copy of num.
  • It asks the user to enter a number and read it in the num variable.
  • It assigns the num valur to tempNum.
  • The while loop picks the last digit of the number and finds the factorial. It adds that value to sumFactorial. It also removes the last digit. This loop is changing the value of tempNum to 0 and this is the reason we are using a copy of the actual number because we need to compare the actual number with the sum.
  • The last if block compares the sum of factorials with the original number. If both are equal, it prints that it is a strong number. Else, it prints that it is not.

If you run this program, it will give output as like below:

Enter a number: 145
145 is a strong number

Enter a number: 146
146 is not a strong number

Recursive C program to check if a number is strong or not:

We can use a recursive function to find the factorial. We can also create a different function to check if a number is strong or not:

#include <stdio.h>

int getFactorial(int num, int factorial)
{
    if (num == 0)
    {
        return factorial;
    }

    return getFactorial(num - 1, factorial * num);
}

int isStrong(int num)
{
    int sumFactorial = 0;
    int tempNum = num;
    while (tempNum > 0)
    {
        sumFactorial += getFactorial(tempNum % 10, 1);
        tempNum /= 10;
    }

    return sumFactorial == num;
}

int main()
{
    int num;

    printf("Enter a number: ");
    scanf("%d", &num);

    if (isStrong(num))
    {
        printf("%d is a strong number\n", num);
    }
    else
    {
        printf("%d is not a strong number\n", num);
    }

    return 0;
}

Here,

  • getFactorial is a recursive function. It calls itself again and again to recursively find the factorial of a number.
  • isStrong method checks if a number is a strong number or not. It uses getFactorial to find the factorial of each digits of a number. It returns 1 if the number is a strong number, else it returns 0.
  • We are calling isStrong from the main method and based on its return value, it prints a message.

It will give output as like below:

Enter a number: 145
145 is a strong number

Enter a number: 146
146 is not a strong number

Using a for loop to check for a strong number in C:

We can also use a for loop to check if a number is strong or not.

#include <stdio.h>

int getFactorial(int num)
{
    int factorial = 1;
    for (int i = 1; i <= num; i++)
    {
        factorial *= i;
    }

    return factorial;
}

int isStrong(int num)
{
    int sumFactorial = 0;
    int tempNum = num;
    while (tempNum > 0)
    {
        sumFactorial += getFactorial(tempNum % 10);
        tempNum /= 10;
    }

    return sumFactorial == num;
}

int main()
{
    int num;

    printf("Enter a number: ");
    scanf("%d", &num);

    if (isStrong(num))
    {
        printf("%d is a strong number\n", num);
    }
    else
    {
        printf("%d is not a strong number\n", num);
    }

    return 0;
}

The for loop is used to find the factorial of a number. It runs from 1 to number and multiplies each value with a final result value which is initialized as 1.

If you run this program, it will give similar output.

Find out all strong numbers between 1 to 1000:

We can use a loop and find out all the strong numbers in a range.

#include <stdio.h>

int getFactorial(int num)
{
    int factorial = 1;
    for (int i = 1; i <= num; i++)
    {
        factorial *= i;
    }

    return factorial;
}

int isStrong(int num)
{
    int sumFactorial = 0;
    int tempNum = num;
    while (tempNum > 0)
    {
        sumFactorial += getFactorial(tempNum % 10);
        tempNum /= 10;
    }

    return sumFactorial == num;
}

int main()
{
    for (int i = 1; i <= 1000; i++)
    {
        if (isStrong(i))
        {
            printf("%d ", i);
        }
    }
    return 0;
}
  • The for loop inside main runs from 1 to 1000.
  • For each value of i in the loop, it checks if it is a strong number or not. If yes, it prints its value.

It will print this output:

1 2 145

You might also like: