Program to check if a given number is a strong number or not | faceprep

Program to check if a given number is a strong number or not is discussed here. A strong number is a number in which the sum of the factorial of the digits is equal to the number itself.

check if a given number is strong number or not

// C program to check if a given number is a strong number or not

#include<stdio.h>

int main()
{
int n,i;
int fact,rem;
printf(“\nEnter a number : “);
scanf(“%d”,&n);
printf(“\n”);
int sum = 0;
int temp = n;
while(n)
{
i = 1,fact = 1;
rem = n % 10;

while(i <= rem)
{
fact = fact * i;
i++;
}
sum = sum + fact;
n = n / 10;
}
if(sum == temp)
printf(“%d is a strong number\n”,temp);
else
printf(“%d is not a strong number\n”,temp);

return 0;
}

// C++ program to check if a given number is a strong number or not

#include<iostream>
using namespace std;

int main()
{
int n,i;
int fact,rem;
cout << “\nEnter a number : “;
cin >> n;
cout << endl;
int sum = 0;
int temp = n;
while(n)
{
i = 1,fact = 1;
rem = n % 10;

while(i <= rem)
{
fact = fact * i;
i++;
}
sum = sum + fact;
n = n / 10;
}
if(sum == temp)
     cout << temp << ” is a strong number\n”;
else
     cout << temp << ” is not a strong number\n”;

return 0;
}

 

// Java program to check if a given number is a strong number or not

import java.util.*;
public class Main
{
public static void main(String[] args) {
int n,i;
int fact,rem;
Scanner sc = new Scanner(System.in);
System.out.print(“\nEnter the number : “);
n = sc.nextInt();
int sum = 0;
int temp = n;
while(n != 0)
{
i = 1;
fact = 1;
rem = n % 10;

while(i <= rem)
{
fact = fact * i;
i++;
}
sum = sum + fact;
n = n / 10;
}

if(sum == temp)
System.out.println(temp + ” is a strong number\n”);
else
System.out.println(temp + ” is not a strong number\n”);

System.out.println();
}
}

// Python program to check if a given number is a strong number or not

n = int(input(“Enter the number : “))
sum = 0
temp = n
while (n):
    i = 1
    fact = 1
    rem = int(n % 10)

while(i <= rem):
    fact = fact * i
    i = i + 1
    sum = sum + fact
    n = int(n / 10)

if(sum == temp):
    print(temp,end = “”)
    print(” is a strong number”)
else:
    print(temp,end = “”) 
    print(” is not a strong number”)

Strong number program using command line arguments

The given number N, a positive integer, will be passed to the program using the first command line parameter. If it is a strong number, the output should be “YES”, If it is not a prime number then output should be “NO” to stdout. Other than YES or NO, no other extra information should be printed to stdout.

#include<stdio.h>
#include<stdlib.h>
int main(int a, char *b[])
{
int number, i, temp, sum = 0, factorial = 1;
number = atoi(b[1]);
temp = number;
while(number != 0)
{
int rem = number%10;
for(i=2; i<=rem; i++)
{
factorial = factorial * i;
}
sum = sum + factorial;
number = number/10;
factorial = 1;
}
if(temp == sum)
printf(“YES”);
else
printf(“NO”);
return 0;
}