C and C++ Programs to Check Whether a Number is Strong Number or Not

This post contains C and C++ program to check whether a given number is Strong number or not. A strong number is a number for which the sum of factorials of its digits is equal to the number itself. The first one is C program to check whether the input number is a strong number or not. An example is 145. 1!+4!+5!=1+24+120=145 Therefore 145 is a strong number.

C Program:

#include<stdio.h>
long int factorial(int n)

{

 if (n == 0)

  return 1;

 else

  return (n * factorial(n - 1));

}void main()

{

 int n, t;

 int sum = 0;

 printf("\nEnterthe number");

 scanf("%i", &n);

 t = n;

 while (t != 0)

 {

  sum += factorial(t % 10);

  t /= 10;

 }

 if (sum == n)

  printf("\nIt is a Strong number");

 else

  printf("\nIt is not a Strong number");

}





C++ Program:

#include<iostream.h>
long int factorial(int n)

{

 if (n == 0)

  return 1;

 else

  return (n * factorial(n - 1));

}int main()

{

 int n, t;

 int sum = 0;

 cout<<"\nEnterthe number";

 cin>>n;

 t = n;

 while (t != 0)

 {

  sum += factorial(t % 10);

  t /= 10;

 }

 if (sum == n)

  cout<<"\nIt is a Strong number";

 else

  cout<<"\nIt is not a Strong number";

return 0;

}

No comments :

Post a Comment