# Strong number in C

A strong number, also known as a strong factorial or Krishnamurthy number, is a number that equals the sum of the factorial of its digits. In C, you can write a program to determine whether a given number is a strong number or not using the following approach:

```#include <stdio.h>

int factorial(int n) {
if (n == 0 || n == 1)
return 1;
else
return n * factorial(n - 1);
}

int isStrongNumber(int num) {
int originalNum = num;
int sum = 0;

while (num > 0) {
int digit = num % 10;
sum += factorial(digit);
num /= 10;
}

return (sum == originalNum);
}

int main() {
int num;

printf("Enter a number: ");
scanf("%d", &num);

if (isStrongNumber(num))
printf("%d is a strong number.\n", num);
else
printf("%d is not a strong number.\n", num);

return 0;
}
```

In this program, the `factorial()` function calculates the factorial of a given number. The `isStrongNumber()` function checks whether a number is a strong number by summing the factorials of its individual digits. The `main()` function prompts the user to enter a number, calls the `isStrongNumber()` function, and displays the result accordingly.

If the entered number is a strong number, the program will output that it is a strong number. Otherwise, it will indicate that the number is not a strong number.

### Program to check whether the number is strong or not:

Certainly! Here’s a simplified version of the program that only checks whether a given number is a strong number or not:

```#include <stdio.h>

int factorial(int n) {
if (n == 0 || n == 1)
return 1;
else
return n * factorial(n - 1);
}

int isStrongNumber(int num) {
int originalNum = num;
int sum = 0;

while (num > 0) {
int digit = num % 10;
sum += factorial(digit);
num /= 10;
}

return (sum == originalNum);
}

int main() {
int num;

printf("Enter a number: ");
scanf("%d", &num);

if (isStrongNumber(num))
printf("%d is a strong number.\n", num);
else
printf("%d is not a strong number.\n", num);

return 0;
}
```

You can run this program, and it will prompt you to enter a number. After entering the number, it will determine whether it is a strong number or not and display the result accordingly.

### Program to print the strong numbers from 1 to n:

Sure! Here’s a program in C that prints all the strong numbers from 1 to a given number `n`:

```#include <stdio.h>

int factorial(int n) {
if (n == 0 || n == 1)
return 1;
else
return n * factorial(n - 1);
}

void printStrongNumbers(int n) {
printf("Strong numbers from 1 to %d:\n", n);

for (int i = 1; i <= n; i++) {
int num = i;
int sum = 0;

while (num > 0) {
int digit = num % 10;
sum += factorial(digit);
num /= 10;
}

if (sum == i) {
printf("%d\n", i);
}
}
}

int main() {
int n;

printf("Enter a number: ");
scanf("%d", &n);

printStrongNumbers(n);

return 0;
}
```

This program prompts the user to enter a number `n`. It then calls the `printStrongNumbers()` function, which calculates the strong numbers from 1 to `n` and prints them one by one.

Each number in the range is checked by iterating from 1 to `n`. Inside the loop, the `factorial()` function is used to calculate the sum of factorials of the individual digits of the current number. If the sum equals the current number, it is considered a strong number and printed.

You can run this program, enter a number, and it will display all the strong numbers from 1 to the given number.

### Program to find strong numbers in a given range:

Certainly! Here’s a program in C that finds and prints all the strong numbers within a given range:

```#include <stdio.h>

int factorial(int n) {
if (n == 0 || n == 1)
return 1;
else
return n * factorial(n - 1);
}

void findStrongNumbersInRange(int start, int end) {
printf("Strong numbers from %d to %d:\n", start, end);

for (int i = start; i <= end; i++) {
int num = i;
int sum = 0;

while (num > 0) {
int digit = num % 10;
sum += factorial(digit);
num /= 10;
}

if (sum == i) {
printf("%d\n", i);
}
}
}

int main() {
int start, end;

printf("Enter the start of the range: ");
scanf("%d", &start);

printf("Enter the end of the range: ");
scanf("%d", &end);

findStrongNumbersInRange(start, end);

return 0;
}
```

In this program, the `factorial()` function is used to calculate the factorial of a given number, similar to the previous examples. The `findStrongNumbersInRange()` function takes a starting number `start` and an ending number `end`. It iterates through the range from `start` to `end` and checks each number for being a strong number. If a number is found to be strong, it is printed.

The `main()` function prompts the user to enter the starting and ending numbers of the range. It then calls the `findStrongNumbersInRange()` function with the provided range and displays the strong numbers within that range.

You can run this program, input the range of numbers, and it will output all the strong numbers found in that range.

Categories C