C program to check strong number; Through this tutorial, we will learn how to check a number is strong or not in c program using for loop, while loop and function.
Programs to Check Strong Number in C
- C Program to Check Strong Number using While Loop
- C Program to Check Strong Number using For Loop
- C Program to Check Strong Number using Function
C Program to Check Strong Number using While Loop
#include <stdio.h> int main() { int Number, Temp, Reminder, Sum = 0, i; long Factorial; printf("\nPlease Enter a Number to Check for Strong Number :- "); 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; }
The output of the above c program; as follows:
Please Enter a Number to Check for Strong Number :- 145 Factorial of 5 = 120 Factorial of 4 = 24 Factorial of 1 = 1 Sum of the Factorials of a Given Number 145 is = 145 145 is a Strong Number.
C Program to Check Strong Number using For Loop
#include <stdio.h> int main() { int Number, Temp, Reminder, Sum = 0, i; long Factorial; printf("\nPlease Enter a Number to Check for Strong Number :- "); 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(" 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; }
The output of the above c program; as follows:
Please Enter a Number to Check for Strong Number :- 145 Factorial of 5 = 120 Factorial of 4 = 24 Factorial of 1 = 1 Sum of the Factorials of a Given Number 145 is = 145 145 is a Strong Number.
C Program to Check Strong Number using Function
#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 :- "); 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("Factorial of %d = %d\n", Reminder, Factorial); Sum = Sum + Factorial; } printf("\nSum 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); }
The output of the above c program; as follows:
Please Enter a Number to Check for Strong Number :- 145 Factorial of 5 = 120 Factorial of 4 = 24 Factorial of 1 = 1 Sum of the Factorials of a Given Number 145 is = 145 145 is a Strong Number.