PLAY WITH CODING

Home Basic Program If & Else Loops Conversion Pattern logo

Program Based on Loops

Program to check whether a number is strong number or not

Input - Output

Input
Enter the number : 145

Output
145 is a strong number

Algorithm

Step 1: START

Step 2: Declare variable num,fact,rem,sum=0,temp

Step 3: input from the user and store into num.

Step 4. temp=num

Note: STRONG number Defination - The sum of the factorial of the individual digits is equal to the number

Step 5: calculate factorial of each digit of a number and store it into sum variable.

Step 6: Now compare the sum with temp variable

Step 7: END

CODE


/* Check whether a number is strong number or not */

#include<stdio.h>
int main()
{
      int num,fact,r,sum=0,temp;
      printf(" Enter the number : ");
      scanf("%d",&num);
      temp = num;
      while(num)
    {
        int i=1,fact=1;
        r = num % 10;
        while(i <= r)
      {
          fact = fact * i;
          i++;
      }
        sum = sum + fact;
        num = num/10;
    }
      if(sum == temp)
        printf(" %d is a strong number",temp);
      else
        printf(" %d is not a strong number",temp);
    return 0;
}


/* Check whether a number is strong or not */

#include<iostream>
using namespace std;
int main()
{
      int num,fact,r,sum=0,temp;
      cout<<" Enter the number : ";
      cin>>num;
      temp = num;
      while(num)
    {
        int i=1,fact=1;
        r = num % 10;
        while(i <= r)
      {
          fact = fact * i;
          i++;
      }
        sum = sum + fact;
        num = num/10;
    }
      if(sum == temp)
        cout<<" "<<temp<<" is a strong number";
      else
        cout<<" "<<temp<<" is not a strong number";
    return 0;
}

OutPut

Enter the number : 145

145 is a strong number