Q:

Write C program to print all strong numbers between 2 numbers

0

Write C program to print all strong numbers between 2 numbers

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

I have used Code::blocks 12 compiler for debugging purpose. But you can use any C programming language compiler as per your availability.

#include <stdio.h>
 
// Function declarations
long long fact(int num);
void printStrongNumbers(int start, int end);
 
 
int main()
{
    int start, end;
 
    /* Input start and end range */
    printf("Enter the lower limit: ");
    scanf("%d", &start);
    printf("Enter the upper limit: ");
    scanf("%d", &end);
 
    printf("List of strong numbers between %d to %d are: \n", start, end);
    printStrongNumbers(start, end);
 
    return 0;
}
 
 
 
//Printing all strong numbers in a given range
void printStrongNumbers(int start, int end)
{
    long long sum;
    int num;
 
    // Iterates from start to end
    while(start != end)
    {
        sum = 0;
        num = start;
 
        // Calculating sum of factorial of digits
        while(num != 0)
        {
            sum += fact(num % 10);
            num /= 10;
        }
 
        // If sum of factorial of digits equal to current number
        if(start == sum)
        {
            printf("%d ", start);
        }
 
        start++;
    }
}
 
//Recursively find factorial of any number
long long fact(int num)
{
    if(num == 0)
        return 1;
    else
        return (num * fact(num-1));
}

Result:

Enter the lower limit: 1

Enter the upper limit: 150

List of strong numbers between 1 to 150 are: 

1 2 145

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Write C program to find prime numbers in given ran... >>
<< Write C Program to convert decimal number to binar...