Strong Number Program in C

Strong number in C

In this program we will find whether a number is strong number in C programming. A Strong Number is a number whose sum of factorial digits is equal to the number itself.

Ex:- number is 145

1! + 4! + 5! = 145 

So it is a strong number.
c program to check strong number or not

Methods discussed

  • Method 1 iterative way of calculation of factorial
  • Method 2 recursive way of calculation of factorial
Strong Number in C

Method 1

For input num

  • Fetch individual digits of the number
  • Calculate factorial of each individual digit and add them
  • If sum == num then its strong
  • Else its not strong number

C Program:-

Run
#include <stdio.h>

// function to calculate factorial
int getFactorial(int n){
    int fact = 1;
    
    for(int i = 1; i <= n; i++)
            fact = fact * i;
    
    return fact;
}

int checkStrong(int num){
    
    int digit, sum = 0;
    int temp = num;
    
    // calculate 1! + 4! + 5!
    while(temp!=0){
        digit = temp % 10;
        
        sum = sum + getFactorial(digit);
        temp /= 10;
    }
    
    // returns 1 if both equal else 0
    return sum == num;
    
}
int main ()
{
    int num = 145;
    
    if(checkStrong(num))
        printf("%d is Strong Number", num);
    else
        printf("%d is Not Strong Number", num);

}
// Time complexity: O(N^2)
// Space complexity: O(1)

Output:-

145 is Strong Number

Method 2

In this method, we will use a recursive way to calculate the factorial number.

For input num

  • Fetch individual digits of the number
  • Calculate factorial (using Recursion) of each individual digit and add them 
  • If sum == num then its strong
  • Else its not strong number

C Program:-

Run
#include <stdio.h>

// function to calculate factorial
int getFactorial(int num)
{
    if(num == 0)
        return 1;
        
    return num * getFactorial(num-1);
}

int checkStrong(int num){
    
    int digit, sum = 0;
    int temp = num;
    
    // calculate 1! + 4! + 5!
    while(temp!=0){
        digit = temp % 10;
        
        sum = sum + getFactorial(digit);
        temp /= 10;
    }
    
    // returns 1 if both equal else 0
    return sum == num;
    
}
int main ()
{
    int num = 145;
    
    if(checkStrong(num))
        printf("%d is Strong Number", num);
    else
        printf("%d is Not Strong Number", num);

}
// Time complexity: O(N^2)
// Space complexity: O(1)
// Auxialiary Space complexity: O(N) bcz of function call stack

Output:-

145 is Strong Number

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Strong Numbers in a given Range

C Program:-

Run
#include <stdio.h>  
int main()  
{  
    int n = 100000;
    
    printf("\nStrong numbers in range 1 to %d :\n", n);  
    for(int i=1;i<=n;i++)  
    {
        int sum = 0, digit;
        int num = i;  
        while(num != 0)  
        {  
            digit = num % 10;  
            int fact = factorial(digit);  
             
          
            num = num/10;  
            sum = sum + fact;  
        }  
        
        if(sum == i){
            printf("%d, ",i);
        }  
    }
    return 0;  
}

int factorial(int n){  
    int mul = 1;
    
    for(int i = 1; i <= n; i++)  
        mul = mul * i;  

    return mul;  
}

Output:-

Strong numbers in range 1 to 100000 :
1, 2, 145, 40585,

5 comments on “Strong Number Program in C”


  • Starboyy

    #include

    int main()
    {
    int number = 145;
    int temp=number;
    int product=1;
    int sum=0;
    while(number!=0)
    {
    int remainder = number % 10;
    for(int i=1;i<=remainder;i++)
    {
    product=product*i;
    }
    sum=sum+product;
    product=1;
    number =number/10;
    }

    if(sum==temp)
    {
    printf("strong number ");
    }
    else{
    printf("Weak numebr");
    }
    }


  • rabin

    #python_code_of_strong_number
    import math
    num=int(input(“:”))
    temp=num
    sum=0
    while num>0:
    digit=int(num%10)
    num=num//10
    x=math.factorial(digit)
    sum=sum+x

    if temp==sum:
    print(“strong number”)
    else:
    print(“not strong”)


  • Srikanth

    # strong number
    from math import factorial
    num=int(input(“Enter a number : “))
    temp = num
    total = 0
    while num > 0:
    dig=num % 10
    num=num//10
    a=factorial(dig)
    total = total + a
    if total==temp:
    print(f”the given number {temp} is a strong number “)
    else:
    print(f”the given number {temp} is not a strong number “)


  • Mohammed Monis

    Strong number for Python :
    Code :-
    num = int(input(“Enter a number : “))
    temp = num
    num = [int(d) for d in str(num)]

    strng_n = 0

    def fact(n):
    factorial = 1
    for i in range(1, n+1):
    factorial = factorial * i
    return factorial

    for i in range(0, len(num)):
    strng_n = strng_n + fact(num[i])

    if temp == strng_n:
    print(“It is a strong number”)
    else:
    print(“It is not a strong number”)


  • Archibrata

    Python Solution
    import math
    a = int(input())
    summ = 0
    l = [int(d) for d in str(a)]
    for i in l:
    summ+=(math.factorial(i))
    if summ == a:
    print(“strong”)
    else:
    print(“not”)