Front-end Tutorials

Front-end Tutorials

HTMLCSSSassJavaScriptReactJS
CMS Tutorials

CMS Tutorials

WordPress
Tutorials expand

C# Program to Check Strong Number

Posted in C# Tutorial
Updated on Oct 06, 2024
By Mari Selvan
πŸ‘οΈ 49 - Views
⏳ 4 mins
πŸ’¬ 1 Comment
C# Program to Check Strong Number

Photo Credit to CodeToFun

πŸ™‹ Introduction

In the world of programming, exploring number properties is a fascinating journey. One such property is related to Strong Numbers.

A Strong Number (also known as a Digital Factorial) is a number in which the sum of the factorials of its digits is equal to the number itself.

In this tutorial, we'll delve into a C# program that checks whether a given number is a Strong Number or not.

πŸ“„ Example

Let's take a look at the C# code that checks whether a given number is a Strong Number.

Program.cs
Copied
Copy To Clipboard
using System;

class Program {
  // Function to calculate the factorial of a number
  static int Factorial(int num) {
    return (num == 0 || num == 1) ? 1 : num * Factorial(num - 1);
  }

  // Function to check if a number is a Strong Number
  static bool IsStrongNumber(int num) {
    int originalNum = num;
    int sum = 0;

    while (num > 0) {
      int digit = num % 10;
      sum += Factorial(digit);
      num /= 10;
    }

    return (sum == originalNum);
  }

  // Driver program
  static void Main() {
    // Replace this value with your desired number
    int number = 145;

    // Call the function to check if the number is a Strong Number
    if (IsStrongNumber(number)) {
      Console.WriteLine($"{number} is a Strong Number.");
    } else {
      Console.WriteLine($"{number} is not a Strong Number.");
    }
  }
}

πŸ’» Testing the Program

To test the program with different numbers, simply replace the value of number in the Main method.

Output
145 is a Strong Number.

Run the program to check if the number is a Strong Number.

🧠 How the Program Works

  1. The program defines a function Factorial to calculate the factorial of a number. Another function IsStrongNumber is defined to check if a given number is a Strong Number.
  2. Another function IsStrongNumber is defined to check if a given number is a Strong Number.
  3. Inside the IsStrongNumber function, it iterates through each digit of the number, calculates the factorial, and adds it to the sum.
  4. The result is compared with the original number to determine if it is a Strong Number.

πŸ“ Between the Given Range

Let's take a look at the C# code that checks for strong numbers in the range of 1 to 200.

Program.cs
Copied
Copy To Clipboard
using System;

class Program {
  // Function to calculate factorial of a number
  static int Factorial(int n) {
    if (n == 0 || n == 1)
      return 1;
    else
      return n * Factorial(n - 1);
  }

  // Function to check if a number is a strong number
  static bool IsStrongNumber(int num) {
    int originalNum = num;
    int sum = 0;

    while (num > 0) {
      int digit = num % 10;
      sum += Factorial(digit);
      num /= 10;
    }

    return sum == originalNum;
  }

  // Driver program
  static void Main() {
    Console.WriteLine("Strong Numbers in the Range 1 to 200:");

    // Iterate through the range and check for strong numbers
    for (int i = 1; i <= 200; i++) {
      if (IsStrongNumber(i)) {
        Console.Write(i + " ");
      }
    }

    Console.WriteLine();
  }
}

πŸ’» Testing the Program

Output
Strong Numbers in the Range 1 to 200:
1 2 145

Run the program to see the strong numbers in the range of 1 to 200.

🧠 How the Program Works

  1. The program defines a function Factorial to calculate the factorial of a number.
  2. It defines a function IsStrongNumber to check if a number is a strong number.
  3. The Main method iterates through the range from 1 to 200 and checks each number for being a strong number using the IsStrongNumber function.
  4. The program outputs the strong numbers found in the specified range.

🧐 Understanding the Concept of Strong Number

To determine if a number is a Strong Number, we need to calculate the factorial of each digit of the number and then sum these factorials. If the sum is equal to the original number, then it is a Strong Number.

For example, let's consider the number 145:

  • The factorial of 1 is 1.
  • The factorial of 4 is 24.
  • The factorial of 5 is 120.

The sum of these factorials is 1 + 24 + 120 = 145, which is the original number. Hence, 145 is a Strong Number.

πŸŽ‰ Conclusion

Understanding and implementing programs that check for mathematical properties, such as Strong Numbers, enhances problem-solving skills in programming. Feel free to modify and expand upon this code for your specific needs. Happy coding!

πŸ‘¨β€πŸ’» Join our Community:

To get interesting news and instant updates on Front-End, Back-End, CMS and other Frameworks. Please Join the Telegram Channel:

Author

author
πŸ‘‹ Hey, I'm Mari Selvan

For over eight years, I worked as a full-stack web developer. Now, I have chosen my profession as a full-time blogger at codetofun.com.

Buy me a coffee to make codetofun.com free for everyone.

Buy me a Coffee

Share Your Findings to All

Subscribe
Notify of
guest
1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Mari Selvan
Mari Selvan
10 months ago

If you have any doubts regarding this article (C# Program to Check Strong Number), please comment here. I will help you immediately.

We make use of cookies to improve our user experience. By using this website, you agree with our Cookies Policy
AgreeCookie Policy