C program to print all Strong numbers from 1 to n

Category: C Program

Learn how to write a C program to print all Strong numbers from 1 to n. This guide includes an explanation of Strong numbers, a step-by-step algorithm, and complete code with examples.

A Strong number, also known as a Krishnamurthy number, is a special number whose sum of the factorials of its digits equals the number itself. For example, 145 is a Strong number because 1! + 4! + 5! = 1 + 24 + 120 = 145.

In this article, we will write a C program to find and print all Strong numbers from 1 to a user-specified number n. We'll start by understanding the concept of Strong numbers, outline the algorithm, and provide the complete C program with explanations.

Understanding Strong Numbers

A Strong number satisfies the following condition:

Sum of the factorials of its digits=Number

For example:

  • 145 is a Strong number because 1! + 4! + 5! = 145.

You might also like our article on checking if a number is a Strong number. It provides more detailed insights and another practical example of Strong numbers.

Algorithm to Find Strong Numbers from 1 to n

  1. Input the number n: Get the upper limit from the user.
  2. Iterate through each number from 1 to n.
  3. Find factorial of each digit: Iterate through each digit of the number and find its factorial.
  4. Sum the factorials: Calculate the sum of the factorials of all digits.
  5. Check if the sum is equal to the original number.
  6. Print the number if it is a Strong number.

Write a C program to print all Strong numbers from 1 to n

Here is the complete C program to print all Strong numbers from 1 to n:

#include <stdio.h>

// Function to calculate the factorial of a given number
int factorial(int n) {
    int fact = 1;
    for (int i = 1; i <= n; i++) {
        fact *= i;
    }
    return fact;
}

int main() {
    int n, num, originalNum, remainder, sum;

    // Input the upper limit from the user
    printf("Enter an upper limit: ");
    scanf("%d", &n);

    printf("Strong numbers between 1 and %d are:\n", n);

    // Iterate through each number from 1 to n
    for (num = 1; num <= n; num++) {
        originalNum = num;
        sum = 0;

        // Calculate the sum of the factorials of each digit
        while (originalNum > 0) {
            remainder = originalNum % 10;
            sum += factorial(remainder);
            originalNum /= 10;
        }

        // Check if the sum of the factorials is equal to the original number
        if (sum == num) {
            printf("%d\n", num);
        }
    }

    return 0;
}

Output

Enter an upper limit: 500
Strong numbers between 1 and 500 are:
1
2
145

Explanation of the Code

  1. Input the Upper Limit:
    • scanf("%d", &n); reads the upper limit n from the user.
  2. Iterate through Each Number:
    • The for loop iterates through each number from 1 to n.
  3. Calculate the Sum of the Factorials of Each Digit:
    • originalNum = num; stores the original number to compare later.
    • The while loop iterates through each digit of the number.
    • remainder = originalNum % 10; extracts the last digit.
    • The for loop calculates the factorial of the digit.
    • sum += factorial(remainder); adds the factorial of the digit to the sum.
    • originalNum /= 10; removes the last digit from the number.
  4. Check for Strong Number:
    • The condition if (sum == num) checks if the sum of the factorials is equal to the original number. If true, it prints that the number is a Strong number.

Recommended Posts