In this article we will show you, How to Write C Program For Strong Number Using While Loop, For Loop, and Recursive Function. We will also show you, How to write a C program to print Strong Numbers between 1 to n.

## C Program For Strong Number Using While Loop

This C program allows the user to enter any positive integer. And then, it will check whether the number is Strong Number or Not using the While Loop

/* C Program For Strong Number */ #include <stdio.h> int main() { int Number, Temp, Reminder, Sum = 0, i; long Factorial; printf("\nPlease Enter a Number to Check for Strong Number \n"); scanf("%d", &Number); //Helps to prevent altering the original value Temp = Number; while( Temp > 0) { Factorial = 1, i = 1; Reminder = Temp % 10; while (i <= Reminder) { Factorial = Factorial * i; i++; } printf("\n Factorial of %d = %d\n", Reminder, Factorial); Sum = Sum + Factorial; Temp = Temp /10; } printf("\n Sum of the Factorials of a Given Number %d is = %d\n", Number, Sum); if ( Number == Sum ) printf("\n %d is a Strong Number.\n", Number); else printf("\n %d is not a Strong Number.\n", Number); return 0; }

**OUTPUT**

**ANALYSIS**

This program allows the user to enter any positive integer and will be assigned to Number variable. Next, we are assigning the original value to the Temp variable. This will help us to preserve our original value while we do all the manipulation on Temp variable.

Below While loop will make sure that, the given number is greater than 0. Statements inside the while loop will split the numbers, and find the factorial of individual digits inside the given number. If you don’t understand the logic, Please refer C Program to Count Number Of Digits in a Number article.

Here, First while Loop (Outer one) will split the number into individual digits. Next, inner while loop will find the factorial of that digit. I suggest you to refer C Program to find Factorials of a Given Number article to understand the logic behind the factorial.

while( Temp > 0) { Factorial = 1, i = 1; Reminder = Temp % 10; while (i <= Reminder) { Factorial = Factorial * i; i++; } printf("\n Factorial of %d = %d\n", Reminder, Factorial); Sum = Sum + Factorial; Temp = Temp /10; }

User Entered value: Number = 145 and Sum = 0

Temp = Number

Temp = 145

**First Iteration**

Reminder = Temp % 10

Reminder = 145 % 10 = 5

Now, it will enter into Inner While loop. Here it will calculate the factorial of 5, and that is 120. Please refer C Program to find Factorial article

Sum = Sum +120

Sum = 0 + 120 => 120

Temp = Temp /10

Temp = 145 /10 => 14

**Second Iteration**

From the first Iteration the values of both Temp and Sum has been changed as: Temp = 14 and Sum = 120

Reminder = Temp % 10

Reminder = 14 % 10 = 4

Now, it will enter into Inner While loop. Here it will calculate the factorial of 4, and that is 24.

Sum = 120 + 24

Sum = 144

Temp = Temp /10

Temp = 14 /10 => 1

**Third Iteration**

From the Third Iteration the values of both Temp and Sum has been changed as: Temp = 1 and Sum = 144

Reminder = Temp % 10

Reminder = 1 % 10 = 0

Here, the factorial of 1 is 1

Sum = 144 + 1

Sum = 145

Temp = Temp / 10

Temp = 1 / 10 => 0

**Here **Temp = 0 so, the while loop condition will fail

if ( Number == Sum ) – Condition will check whether the user enter number is exactly equal to Sum number or not. If this condition is True, then it is Strong Number, else it is not Strong Number

Let me check with another value

## C Program For Strong Number Using For Loop

This C program allows the user to enter any positive integer. And then, this program will check whether a number is Strong Number or Not using For Loop

/* C Program For Strong Number */ #include <stdio.h> int main() { int Number, Temp, Reminder, Sum = 0, i; long Factorial; printf("\nPlease Enter a Number to Check for Strong Number \n"); scanf("%d", &Number); //Helps to prevent altering the original value Temp = Number; for(Temp = Number; Temp > 0; Temp = Temp / 10 ) { Factorial = 1; Reminder = Temp % 10; for (i = 1; i <= Reminder; i++) { Factorial = Factorial * i; } printf("\n Factorial of %d = %d\n", Reminder, Factorial); Sum = Sum + Factorial; } printf("\n Sum of the Factorials of a Given Number %d is = %d\n", Number, Sum); if ( Number == Sum ) printf("\n %d is a Strong Number.\n", Number); else printf("\n %d is not a Strong Number.\n", Number); return 0; }

**OUTPUT**

Let me check with another number

We just replaced the While loop in the above example with the For loop. If you don’t understand the for loop then please refer For Loop article here: For Loop

## C Program For Strong Number Using Recursion

This C program allows the user to enter any positive integer. And then, it will check whether a number is Strong Number or Not using Recursion concept. Here, you can also try normal function to calculate the Factorial.

/* C Program For Strong Number */ #include <stdio.h> long Calculate_Factorial(int); int main() { int Number, Temp, Reminder, Sum = 0; long Factorial; printf("\nPlease Enter a Number to Check for Strong Number \n"); scanf("%d", &Number); //Helps to prevent altering the original value Temp = Number; for(Temp = Number; Temp > 0; Temp = Temp / 10 ) { Factorial = 1; Reminder = Temp % 10; // Calling Calculate_factorial Function Factorial = Calculate_Factorial(Reminder); printf("\n Factorial of %d = %d\n", Reminder, Factorial); Sum = Sum + Factorial; } printf("\n Sum of the Factorials of a Given Number %d is = %d\n", Number, Sum); if ( Number == Sum ) printf("\n %d is a Strong Number.\n", Number); else printf("\n %d is not a Strong Number.\n", Number); return 0; } long Calculate_Factorial(int Number) { if (Number == 0 || Number == 1) return 1; else return Number * Calculate_Factorial (Number -1); }

**OUTPUT**

## C Program to Find Strong Numbers between the 1 to n

This C program allows the user to enter minimum and maximum values. This program will find Strong Numbers between the Minimum and Maximum values.

/* C Program to Find Strong Numbers between 0 to 10000 */ #include <stdio.h> int Strong_Number(int); int main() { int Number, Temp, Sum = 0, Minimum, Maximum; printf("\nPlease Enter the Minimum & Maximum Values\n"); scanf("%d %d",&Minimum, &Maximum); for(Number = Minimum; Number <= Maximum; Number++) { Temp = Number; Sum = Strong_Number(Temp); if ( Number == Sum ) printf("\n %d is a Strong Number.\n", Number); } return 0; } long Calculate_Factorial(int Number) { if (Number == 0 || Number == 1) return 1; else return Number * Calculate_Factorial (Number -1); } int Strong_Number(int Number) { int Temp, Reminder, Sum = 0; long Factorial; Temp = Number; for(Temp = Number; Temp > 0; Temp = Temp / 10 ) { Factorial = 1; Reminder = Temp % 10; Factorial = Calculate_Factorial(Reminder); Sum = Sum + Factorial; } return Sum; }

**OUTPUT**

**ANALYSIS**

This program allows the user to enter minimum and maximum values. Next, below show For Loop helps compiler to iterate between Minimum and Maximum Variables, iteration starts at the Minimum and then it will not exceed Maximum variable.

for (Number = Minimum; Number <= Maximum; Number++)

Thank you for Visiting Our Blog