Write a program to identify if the number is Strong number or not

14 May 2022

Write a program to identify if the number is Strong number or not


If you are from 2023 batch student, Join our Telegram group for placement preparation and coming placement drive updates : https://t.me/talentbattle2023

Description

Get a number as input from user and then check whether that number is a strong number or not.

A number is said to be strong number if the sum of the factorial of each digit in the number is same as that of the number.

E.g.  let the number be 145

Here 1! + 4! + 5! is 1 + 24 + 120  which is equal to 145 itself.

Input

121

Output

Not a strong number

Input

2

Output

Strong number

 C Program

Method 1

#include <stdio.h>

int main()

{

    int num,rem,fact=1,sum=0;

    printf("Enter a number: ");

    scanf("%d",&num);

    int copy=num;

   if(num==0)

   sum=sum+fact;

  else

{

    while(copy!=0)

    {

        rem=copy%10;

        fact=1;

        for(int i=1;i<=rem;i++)

        {

            fact=fact*i;

        }

        sum=sum+fact;

        copy=copy/10;

    }}

    if(sum==num)

    printf("Strong number");

    else

    printf("Not a strong number");

    return 0;

}

Method 2

#include <stdio.h>

int factorial(int n)

{

    if(n!=0)

    return n * factorial(n-1);

    else 

    return 1;

}

int main()

{

    int num,rem,fact=1,sum=0;

    printf("Enter a number: ");

    scanf("%d",&num);

    int copy=num;

    if(num==0)

    sum=sum+fact;

    else

    {

    while(copy!=0)

    {

        rem=copy%10;

        fact=factorial(rem);

        sum=sum+fact;

        copy=copy/10;

    }}

    if(sum==num)

    printf("Strong number");

    else

    printf("Not a strong number");

    return 0;

}

C++ Program

Method 1

#include <iostream>

using namespace std;

int main()

{

    int num,rem,fact,sum=0;

    cout<<"Enter a number: ";

    cin>>num;

    int copy=num;

   if(num==0)

    sum=sum+fact;

    else

    {

    while(copy!=0)

    {

        rem=copy%10;

        fact=factorial(rem);

        sum=sum+fact;

        copy=copy/10;

    }}

    if(sum==num)

    cout<<"Strong number";

    else

    cout<<"Not a strong number";

    return 0;

}

Method 2

#include <iostream>

using namespace std;

int factorial(int n)

{

    if(n!=0)

    return n * factorial(n-1);

    else 

    return 1;

}

int main()

{

    int num,rem,fact=1,sum=0;

    cout<<"Enter a number: ";

    cin>>num;

    int copy=num;

    if(num==0)

    sum=sum+fact;

    else

    {

    while(copy!=0)

    {

        rem=copy%10;

        fact=factorial(rem);

        sum=sum+fact;

        copy=copy/10;

    }}

    if(sum==num)

    cout<<"Strong number";

    else

    cout<<"Not a strong number";

    return 0;

}

Java

import java.util.Scanner;

public class Main

{

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

System.out.print("Enter a number: ");

int num=sc.nextInt();

int fact=1,sum=0;

int copy=num;

if(num==0)

sum=sum+fact;

else{

while(copy!=0)

{

fact=1;

int rem=copy%10;

for(int i=1;i<=rem;i++)

fact=fact*i;

sum=sum+fact;

copy=copy/10;

}}

if(sum==num)

System.out.println("Strong Number");

else

System.out.println("Not a Strong Number");

}

}

Python

Method 1

num=int(input("Enter a number: "))

fact=1

Sum=0

copy=num

if(num==0):

    Sum=Sum+fact

else:

    while(copy!=0):

        fact=1

        rem=copy%10

        for i in range(1,rem+1):

            fact=fact*i

        Sum=Sum+fact

        copy=copy//10

if(Sum==num):

    print("Strong number")

else:

    print("Not a strong number")

Method 2

import math

num=int(input("Enter a number: "))

Sum=0

fact=1

copy=num

if(num==0):

    Sum=Sum+fact

else:

    while(copy!=0):

        rem=copy%10

        fact=math.factorial(rem)

        Sum=Sum+fact

        copy=copy//10

if(Sum==num):

    print("Strong number")

else:

    print("Not a strong number")


If you are from 2023 batch student, Join our Telegram group for placement preparation and coming placement drive updates : https://t.me/talentbattle2023
Related Articles

Ask Us Anything !