Back to: C#.NET Programs and Algorithms
Strong Number Program in C# with Examples
In this article, I am going to discuss How to Implement the Strong Number Program in C# with Examples. Please read our previous article where we discussed the Buzz Number Program in C#. Here, in this article, first, we will learn what is a Strong Number and then we will see how to implement the Strong Number Program in C#. And finally, we will see how to print all the Strong numbers between a range of numbers like between 1 to 100 or 100 to 1000, etc.
What is a Strong Number?
AĀ Strong number is a special number whose sum of the factorial of each digit is equal to the original number.Ā Ā
Examples:Ā Ā 
Input: 145Ā Ā 
Output: Yes, it is a strong numberĀ Ā 
Explanation: 1! + 4! + 5! = 145Ā 
Input: 124Ā Ā 
Output: No, it is not a strong numberĀ Ā Ā 
Explanation: 1! + 2! + 4! = 27 which is not equal to n i.e., 124Ā Ā 
First of all, we have to know what is Factorial number. how it works. What is the basic method to solve this problem?Ā Ā
Factorial number:Ā Factorial of n is the product of all positive descending integers.Ā Ā 
For exampleĀ Ā 
Ā Ā Ā Ā Ā  1) 5! = 5*4*3*2*1 = 120Ā Ā Ā Ā 
Ā Ā Ā Ā Ā  2) 3! = 3*2*1 = 6
Factorial of Number in C#Ā
using System;
public class FactorialExample
{
    public static void Main ()
    {
        int i, fact = 1, n;
        Console.Write ("Enter any Number: ");
        n = int.Parse (Console.ReadLine ());
        for (i = 1; i <= n; i++)
        {
         fact = fact * i;
        }
        Console.Write ("Factorial of " + n + " is: " + fact);
    }
}
Output:

More Details: –Ā https://dotnettutorials.net/lesson/factorial-program-in-csharp/Ā Ā
After understanding how to calculate the factorial of a number letās come to our main question which is a strong number.Ā
How to Implement Strong Number Program in C#?
Let us understand how to check whether a given number is a strong number or not in C#. The following are the steps that we need toĀ follow toĀ solve this problem.Ā Ā
- First, we take the input Number.Ā Ā
- Then we separate the individual number of digits.Ā Ā
- Then we calculate the factorial of the individual number of digits.Ā Ā
- After calculating factorial, we will sum the factorial of the individual digit.Ā
Example: Strong Number in C#
The following sample code shows how to implement the strong number program in C#.Ā
using System;
public class StrongNumberProgram
{
    public static void Main ()
    {
        int number = 0, sum = 0;
        Console.WriteLine ("Enter a number");
        number = Convert.ToInt32 (Console.ReadLine());
        int quot = number;
        int remainder;
        while (quot != 0)
        {
         remainder = quot % 10;
         int fact = CalculateFactorial(remainder);
         quot = quot / 10;
         sum = sum + fact;
        }
        if(sum == number)
        {
         Console.WriteLine (number + " is a Strong Number");
        }
        else
        {
         Console.WriteLine (number + " is not a Strong Number");
        }
    }
    //Calulate Factorial of a number 
    public static int CalculateFactorial(int number)
    {
        int fact = 1;
        for (int i = 1; i <= number; i++)
        {
         fact = fact * i;
        }
        return fact;
    }
}  
Output:

TIMEĀ COMPLEXITY:Ā Time Complexity: O(N)Ā
Print all strong number from 1 toĀ N (input value from user)Ā
Input: Number = 100Ā Ā Ā 
Output: 1 2Ā Ā Ā 
Explanation: Only 1 and 2 are the strong numbers from 1 to 100 becauseĀ Ā Ā 
1! = 1, andĀ Ā Ā 
2! = 2Ā Ā 
Input: N = 1000Ā Ā Ā 
Output: 1 2 145Ā Ā Ā 
Explanation:Ā Only 1, 2 and 145 are the strong numbers from 1 to 1000 becauseĀ Ā Ā 
1! = 1,Ā Ā Ā 
2! = 2, andĀ Ā Ā 
(1! + 4! + 5!) = 145Ā Ā Ā 
The idea is to iterate from [1, Number] and check if any number between that range is a strong number or not. If yes then print that number, else check for the next number.Ā Ā
C# Program to Print all Strong Numbers Between 1 and N
The following C# Program will allow the user to input a number and then it will print all the strong numbers between 1 and that input number.
using System;
public class StrongNumberProgram
{
    public static void Main ()
    {
        int number = 0;
        Console.WriteLine ("Enter a number");
        number = Convert.ToInt32 (Console.ReadLine());
        int remainder;
        Console.WriteLine ("Strong Number Numbers Between 1 and "+ number + " : ");
        for(int i = 1; i<= number; i++)
        {
            int quot = i, sum = 0;
            while (quot != 0)
            {
             remainder = quot % 10;
             int fact = CalculateFactorial(remainder);
             quot = quot / 10;
             sum = sum + fact;
            }
            if(sum == i)
            {
             Console.Write(i + " ");   
            }
        }
    }
    //Calulate Factorial of a number 
    public static int CalculateFactorial(int number)
    {
        int fact = 1;
        for (int i = 1; i <= number; i++)
        {
         fact = fact * i;
        }
        return fact;
    }
}
Output:

In the next article, I am going to discuss the Kaprekar Number Program in C# with Examples. Here, in this article, I try to explain How to Implement the Strong Number Program in C# with Examples and I hope you enjoy this article.
