C program to print strong numbers between 1 to n

Previous Program Next Program

Write a C program to print all Strong numbers between 1 to n. C program to generate all strong numbers between a given range. Logic to generate all strong numbers in a given range in C program.

Example

Input

Input upper limit: 1000

Output

Strong numbers between 1-1000: 
1, 2, 145

Required knowledge

Basic C programming, If else, For loop

Must have programming knowledge for this program.

What is Strong number?

Strong number is a special number whose sum of factorial of digits is equal to the original number. For example: 145 is strong number. Since, 1! + 4! + 5! = 145

Logic to print Strong numbers between 1 to n

Below is the step by step descriptive logic to print strong numbers from 1 to n.

  1. Read upper limit to print strong numbers from user. Store it in some variable say end.
  2. Run a loop from 1 to end, incrementing 1 in each iteration. Structure of the loop should be for(i=1; i<=end; i++). Inside this loop, we will check each element for strong number.
  3. Inside the loop print the current element as strong number if it satisfies strong number condition. Means you need to embed the logic to check strong numbers inside the loop.

Let us convert the above logic to a C program.

Program to print strong numbers between 1 to n

/**
 * C program to print all Strong Numbers between 1 to n
 */

#include <stdio.h>

int main()
{
    int i, j, cur, lastDigit, end;
    long fact, sum;

    /* Read upper limit from user */
    printf("Enter upper limit: ");
    scanf("%d", &end);

    printf("All Strong numbers between 1 to %d are:\n", end);
    
    /* Iterate from 1 to end */
    for(i=1; i<=end; i++)
    {
        /* Number to check for strong number */
        cur = i;

        sum = 0;

        /*
         * Find the sum of factorial of digits
         */ 
        while(cur > 0)
        {
            fact = 1;
            lastDigit = cur % 10;

            /* Find factorial of last digit of current num. */
            for( j=1; j<=lastDigit; j++)
            {
                fact = fact * j;
            }

            sum = sum + fact; 

            cur = cur / 10;
        }
        
        /*
         * Check if current number is strong number or not
         */  
        if(sum == i)
        {
            printf("%d, ", i);
        }
    }

    return 0;
}

Once you got the logic of printing strong numbers between 1 to n. You can easily modify the logic to find strong numbers in any given range. Below program illustrates how to print strong numbers in a given range.

Program to print strong numbers in given range

/**
 * C program to print Strong numbers in given range
 */

#include <stdio.h>

int main()
{
    int i, j, cur, lastDigit, start, end;
    long fact, sum;

    /* Read lower and upper limit from user */
    printf("Enter lower limit: ");
    scanf("%d", &start);
    printf("Enter upper limit: ");
    scanf("%d", &end);

    printf("All Strong numbers between %d to %d are:\n", start, end);
    
    /* Iterate from 1 to end */
    for(i=start; i<=end; i++)
    {
        /* Number to check for strong number */
        cur = i;

        sum = 0;

        /*
         * Find the sum of factorial of digits
         */ 
        while(cur > 0)
        {
            fact = 1;
            lastDigit = cur % 10;

            /* Find factorial of last digit of current num. */
            for( j=1; j<=lastDigit; j++)
            {
                fact = fact * j;
            }

            sum = sum + fact; 

            cur = cur / 10;
        }
        
        /*
         * Check if current number is strong number or not
         */  
        if(sum == i)
        {
            printf("%d, ", i);
        }
    }

    return 0;
}
Output
Enter lower limit: 1
Enter upper limit: 100000
1, 2, 145, 40584, 

Happy coding ;)

Recommended posts

Previous Program Next Program

Labels: , ,