Spy Number (Sum and Products of Digits are same)
A number is said to be a Spy number if the sum of all the digits is equal to the product of all digits.
Examples :
Input : 1412
Output : Spy Number
Explanation :
sum = (1 + 4 + 1 + 2) = 8
product = (1 * 4 * 1 * 2) = 8
since, sum == product == 8
Input : 132
Output : Spy Number
Explanation :
sum = (1 + 3 + 2) = 6
product = (1 * 3 * 2) = 6
since, sum == product == 6
C++
#include <bits/stdc++.h>
using namespace std;
bool checkSpy( int num)
{
int digit, sum = 0,
product = 1;
while (num > 0)
{
digit = num % 10;
sum += digit;
product *= digit;
num = num / 10;
}
if (sum == product)
return true ;
else
return false ;
}
int main()
{
int num = 1412;
if (checkSpy(num))
cout << "The number is "
<< "a Spy number"
<< endl;
else
cout << "The number is "
<< "NOT a spy number"
<< endl;
return 0;
}
|
Java
import java.util.*;
class GFG
{
static boolean checkSpy( int input)
{
int digit, sum = 0 ,
product = 1 ;
while (input > 0 )
{
digit = input % 10 ;
sum += digit;
product *= digit;
input = input / 10 ;
}
if (sum == product)
return true ;
else
return false ;
}
public static void main(String args[])
{
int input = 1412 ;
if (checkSpy(input))
System.out.println( "The number is " +
"a Spy number" );
else
System.out.println( "The number is " +
"NOT a Spy number" );
}
}
|
Python3
def checkSpy(num):
sums = 0
product = 1
while num> 0 :
digit = num % 10
sums = sums + digit
product = product * digit
num = num / / 10
if sums = = product:
return True
else :
return False
num = 1412
if (checkSpy(num)):
print ( "The number is a Spy Number" )
else :
print ( "The number is NOT a spy number" )
|
C#
using System;
class GFG
{
static bool checkSpy( int input)
{
int digit, sum = 0,
product = 1;
while (input > 0)
{
digit = input % 10;
sum += digit;
product *= digit;
input = input / 10;
}
if (sum == product)
return true ;
else
return false ;
}
public static void Main()
{
int input = 1412;
if (checkSpy(input))
Console.WriteLine( "The number is " +
"a Spy number" );
else
Console.WriteLine( "The number is " +
"NOT a Spy number" );
}
}
|
PHP
<?php
function checkSpy( $num )
{
$digit ; $sum = 0;
$product = 1;
while ( $num > 0)
{
$digit = $num % 10;
$sum += $digit ;
$product *= $digit ;
$num = $num / 10;
}
if ( $sum == $product )
return 1;
else
return -1;
}
$num = 1412;
if (checkSpy( $num ))
echo "The number is a " .
"Spy number" , "\n" ;
else
echo "The number is NOT " .
"a spy number" , "\n" ;
?>
|
Javascript
<script>
function checkSpy(num)
{
let digit, sum = 0,
product = 1;
while (num > 0)
{
digit = num % 10;
sum += digit;
product *= digit;
num = Math.floor(num / 10);
}
if (sum == product)
return true ;
else
return false ;
}
let num = 1412;
if (checkSpy(num))
document.write( "The number is "
+ "a Spy number"
+ "<br>" );
else
document.write( "The number is "
+ "NOT a spy number"
+ "<br>" );
</script>
|
Output
The number is a Spy number
Time Complexity: O(log10n)
Auxiliary Space: O(1)
Last Updated :
17 Feb, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...