Skip to content
Related Articles

Related Articles

Improve Article

Spy Number (Sum and Products of Digits are same)

  • Last Updated : 01 Mar, 2021
Geek Week

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
Explanation : 
sum = (1 + 4 + 1 + 2) = 8
product = (1 * 4 * 1 * 2) = 8
since, sum == product == 8
Output : Spy Number

Input : 132
Explanation : 
sum = (1 + 3 + 2) = 6
product = (1 * 3 * 2) = 6
since, sum == product == 6
Output : Spy Number

 

 

C++




// CPP program to check
// a spy number
#include <bits/stdc++.h>
using namespace std;
 
// Function to
// check spy number
bool checkSpy(int num)
{
    int digit, sum = 0,
        product = 1;
    while (num > 0)
    {
        digit = num % 10;
         
        // getting sum of digits
        sum += digit;
         
        // getting product of digits
        product *= digit;
        num = num / 10;
    }
     
    if (sum == product)
        return true;
    else
        return false;
}
 
// Driver code
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




// Java program to
// check Spy number
import java.util.*;
 
class GFG
{
     
    // boolean function to
    // check Spy number
    static boolean checkSpy(int input)
    {
 
        int digit, sum = 0,
               product = 1;
        while (input > 0)
        {
            digit = input % 10;
             
            // getting the
            // sum of digits
            sum += digit;
             
            // getting the product
            // of digits
            product *= digit;
            input = input / 10;
        }
         
        // Comparing the
        // sum and product
        if (sum == product)
            return true;
        else
            return false;
    }
     
    // Driver code
    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




# Python program to check
# Spy number
 
# Function to check
# Spy number
def checkSpy(num):
    sums = 0
    product = 1
    while num>0:
        digit = num % 10
         
        # getting the
        # sum of digits
        sums = sums + digit
         
        # getting the product
        # of digits
        product = product * digit
        num = num // 10
         
    if sums == product:
        return True
    else:
        return False
 
# Driver Code
num = 1412
if (checkSpy(num)):
    print("The number is a Spy Number")
else:
    print("The number is NOT a spy number")

C#




// C# program to check
// Spy number
using System;
 
class GFG
{
 
    // boolean function to
    // check Spy number
    static bool checkSpy(int input)
    {
 
        int digit, sum = 0,
               product = 1;
        while (input > 0)
        {
            digit = input % 10;
 
            // getting the sum
            // of digits
            sum += digit;
 
            // getting the product
            // of digits
            product *= digit;
            input = input / 10;
        }
 
        // Comparing the sum
        // and product
        if (sum == product)
            return true;
        else
            return false;
    }
 
    // Driver code
    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");
    }
}
 
// This code is Contributed by vt_m.

PHP




<?php
// PHP program to check
// a spy number
 
// Function to check
// spy number
function checkSpy($num)
{
    $digit; $sum = 0;
    $product = 1;
    while ($num > 0)
    {
        $digit = $num % 10;
         
        // getting sum
        // of digits
        $sum += $digit;
         
        // getting product
        // of digits
        $product *= $digit;
        $num = $num / 10;
    }
     
    if ($sum == $product)
        return 1;
    else
        return -1;
}
 
// Driver code
$num = 1412;
if (checkSpy($num))
    echo "The number is a ".
          "Spy number","\n";
     
else
    echo "The number is NOT ".
          "a spy number","\n";
 
// This code is contributed by ajit.
?>

Javascript




<script>
 
// Javascript program to check
// a spy number
 
// Function to
// check spy number
function checkSpy(num)
{
    let digit, sum = 0,
        product = 1;
    while (num > 0)
    {
        digit = num % 10;
         
        // getting sum of digits
        sum += digit;
         
        // getting product of digits
        product *= digit;
        num = Math.floor(num / 10);
    }
     
    if (sum == product)
        return true;
    else
        return false;
}
 
// Driver code
 
    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>");
     
 
// This code is contributed by Mayank Tyagi
 
</script>

Output : 
 

The number is a Spy number

This article is contributed by Chinmoy Lenka. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
 

Attention reader! All those who say programming isn’t for kids, just haven’t met the right mentors yet. Join the  Demo Class for First Step to Coding Coursespecifically designed for students of class 8 to 12. 

The students will get to learn more about the world of programming in these free classes which will definitely help them in making a wise career choice in the future.




My Personal Notes arrow_drop_up
Recommended Articles
Page :