Ticker

6/recent/ticker-posts

Special Number in C

Start Practicing LeetCode with Programming ChaskaI have explained my approach to the problems in simple way. I have also shared the optimized code so that you can learn some new. 

Special Number Program in C

Special Number: A natural number which is equal to the sum of the factorial of each digit is called a Special 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 <stdio.h>

main()

{

    int numbersum = 0;

    printf("Enter number here: ");

    scanf("%d", &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)

        printf("The number %d is a Special Number"number);

    else

        printf("The number %d is not a Special Number"number);
}



Code 2: Recursion approach to check whether a given number is a Special number or not.




#include <stdio.h>

int sumOfFactorial(int n) {

    if (n <= 0) // Base case

        return 0;

    else

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

int factorial(int n)

{

    if (n == 0 || n == 1) // Base case

        return 1;

    else

        return n * factorial(n - 1); // Recursive call
}

void isSpecial(int number)

{

    if (number == sumOfFactorial(number))

        printf("The number %d is a Special Number"number);

    else

        printf("The number %d is not a Special Number"number);
}

main()

{

    int number;

    printf("Enter number here: ");

    scanf("%d", &number);

    isSpecial(number);
}



    Code 3: This code takes a number as a string and then converts each character to an integer, finds factorial and adds it to the sum, and at last converts string to an integer by using atoi function and check if it is equal to the sum or not. 




    #include <stdio.h>

    #include <string.h>

    main()

    {

        int sum = 0;

        char number[10];

        printf("Enter number here: ");

        gets(number);

        int length = strlen(number);

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

        {

            int lastDigit = number[i] - 48;

            int factorial = 1;

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

            {

                factorial = factorial * i;
            }

            sum = sum + factorial;
        }

        if (atoi(number) == sum)

            printf("The number %s is a Special Number"number);

        else

            printf("The number %s is not a Special Number"number);
    }



    Start Practicing LeetCode with Programming ChaskaI have explained my approach to the problems in simple way. I have also shared the optimized code so that you can learn some new. 


    Code 4: This is optimized code to check special numbers. Here we are storing factorial of numbers from 0 to 9 so that we don't have to calculate factorial.

    Time complexity - O(n)


    #include <stdio.h>
    #include <string.h>

    int main()

    {

        long long int sum = 0;
       
        int factorial[] = {1, 1, 2, 6, 24, 120, 720, 5040, 40320, 362880};  

        char number[20];

        printf("Enter number here: ");

        gets(number);

        int length = strlen(number);

        for (int i = 0; i < length; i++) {

            int currentDigit = number[i] - 48; // converting character to integer through ASCII value

            sum += factorial[currentDigit]; // adding factorial of currentDigit to the sum
        }

        if (atoi(number) == sum)

            printf("The number %s is a Special Number", number);

        else

            printf("The number %s is not a Special Number", number);
           
        return 0;
    }


    Start Practicing LeetCode with Programming Chaska. I have explained my approach to the problems in simple way. I have also shared the optimized code so that you can learn some new. 


        If you have any doubts/questions related to the Program, if you want to give any suggestions, or want an explanation of the 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