Ticker

6/recent/ticker-posts

Special Number in C++

Special Number : A natural number which is equal to the sum of factorial of each digit is called a Perfect Number.

For Example : Number - - > 145
= 1                       4                           5
= !1        +           !4            +           !5
= 1         +        4*3*2*1      +      5*4*3*2*1
= 1         +           24           +           120
= 145 = Number
So, 145 is a Special Number.
Some of the Special numbers are 1,2,145 and 40585.

Algorithm to check whether a given number is a Special number or not:

Step 1: Accept a number.
Step 2: Find the factorial of each digit of a number and store it in a variable say sum.
Step 3: If the number is equal to the sum then display "Special number" else display "Not Special Number".
Step 4: Stop

The above Algorithm can be implemented in C++ Programming in several ways. Some of the C++ Program codes are given below:

Code 1: This is the general code to check for a Special number in C++.




#include <iostream>

using namespace std;

main()

{

    int numbersum = 0;

    cout << "Enter number here: ";

    cin >> number;

    int numberCopy = number;

    while (numberCopy > 0)

    {

        int lastDigit = numberCopy % 10;

        int factorial = 1;

        for (int i = 1i <= lastDigiti++)

        {

            factorial = factorial * i;
        }

        sum = sum + factorial;

        numberCopy = numberCopy / 10;
    }

    if (number == sum)

        cout << "The number " << number << " is a Special Number";

    else

        cout << "The given number " << number << " is not a Special Number";
}



Code 2: This code divides the work into a number of functions to check whether a given number is a Special number or not.




#include <iostream>

using namespace std;

class special

{

    int sumOfFactorial(int n) // It will be private by default

    {

        if (n <= 0)

            return 0;

        else

        {

            return factorial(n % 10) + sumOfFactorial(n / 10);
        }
    }

    int factorial(int n) // It will be private by default as well

    {

        if (n == 0 || n == 1)

            return 1;

        else

            return n * factorial(n - 1);
    }

public:
    void isSpecial(int number)

    {

        if (number == sumOfFactorial(number))

            cout << "The number " << number << " is a Special Number";

        else

            cout << "The given number " << number << " is not a Special Number";
    }
};

int main()

{

    int number;

    cout << "Enter number here: ";

    cin >> number;

    special ob;

    ob.isSpecial(number);

    return 0;
}



    Code 3: This code takes a number as a string and then convert characters to integer and find factorial and add it to sum and at last convert string to an integer by using stoi function and check if it is equal to the sum or not. 




    #include <iostream>

    #include <string>

    using namespace std;

    main()

    {

        int sum = 0;

        string number;

        cout << "Enter number here: ";

        cin >> number;

        int length = number.length();

        for (int i = 0i < lengthi++)

        {

            int lastDigit = number.at(i) - 48; // character-48 to convert & reduce ascii value of charcter to integer

            int factorial = 1;

            for (int i = 1i <= lastDigiti++)

            {

                factorial = factorial * i;
            }

            sum = sum + factorial;
        }

        if (stoi(number) == sum) //stoi function is used to convert string data type to integer data type

            cout << "The number " << number << " is a Special Number";

        else

            cout << "The number " << number << " is not a Special Number";
    }




      If you have any doubt/question related to Program, or if you want to give any suggestions, or want explanation of codefeel free to comment below. I hope that my code is understandable, if not then you can tell me in the comments so that I will try to make it easier to understand.

      Post a Comment

      0 Comments