Open In App

Spy Number (Sum and Products of Digits are same)

Last Updated : 17 Feb, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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++




// 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

Time Complexity: O(log10n)
Auxiliary Space: O(1)

 



Similar Reads

Maximize profit possible by selling M products such that profit of a product is the number of products left of that supplier
Given an array arr[] consisting of N positive integers, such that arr[i] represents the number of products the ith supplier has and a positive integer, M, the task is to find the maximum profit by selling M products if the profit of a particular product is the same as the number of products left of that supplier. Examples: Input: arr[] = {4, 6}, M
7 min read
Sum of the products of same placed digits of two numbers
Given two positive integers N1 and N2, the task is to find the sum of the products of the same placed digits of the two numbers. Note: For numbers of unequal length, the preceding digits of the smaller number needs to be treated as 0.Examples: Input: N1 = 5, N2 = 67 Output: 35 Explanation: At one's place, we have digits 5 and 7, their product is 35
7 min read
Minimum digits to be removed to make either all digits or alternating digits same
Given a numeric string str, the task is to find the minimum number of digits to be removed from the string such that it satisfies either of the below conditions: All the elements of the string are the same.All the elements at even position are same and all the elements at the odd position are same, which means the string is alternating with the equ
7 min read
Numbers of Length N having digits A and B and whose sum of digits contain only digits A and B
Given three positive integers N, A, and B. The task is to count the numbers of length N containing only digits A and B and whose sum of digits also contains the digits A and B only. Print the answer modulo 109 + 7.Examples: Input: N = 3, A = 1, B = 3 Output: 1 Possible numbers of length 3 are 113, 131, 111, 333, 311, 331 and so on... But only 111 i
15 min read
Count pairs of same parity indexed elements with same MSD after replacing each element by the sum of maximum digit * A and minimum digits * B
Given an array arr[] of N 3-digit integers and two integers a and b, the task is to modify each array element according to the following rules: Find the maximum, say M, and minimum digit, say m, of each array element arr[i].Update the array element arr[i] as (A * M + B * m). The task is to count the number of pairs such that the chosen elements are
12 min read
Minimize the Sum of all the subarrays made up of the products of same-indexed elements
Given two arrays arr[] and arr2[] of length N, the task is to find the minimum sum of all the subarrays made up of the products of the same indexed elements of both the arrays after rearranging the second array. Note: Since the answer can be very large, print the answer modulo 109 + 7. Examples: Input: arr[] = {1, 2}, arr2[] = {2, 3} Output: 14 Exp
7 min read
Minimum number of digits to be removed so that no two consecutive digits are same
Given a number N. The task is to count the minimum number of digits to be removed from the number so that no two consecutive digits are the same.Examples: Input : N = 11344 Output : 2 Explanation : Remove the digit 1 from 2nd place and 4 from the end so that the number becomes 134. Thus no two consecutive digits are same. Hence answer is 2.Input :
5 min read
Smallest number with given sum of digits and sum of square of digits
Given the sum of digits a and sum of the square of digits b . Find the smallest number with the given sum of digits and the sum of the square of digits. The number should not contain more than 100 digits. Print -1 if no such number exists or if the number of digits is more than 100.Examples: Input : a = 18, b = 162 Output : 99 Explanation : 99 is t
23 min read
Count of digits in N and M that are same and are present on the same indices
Given two number N and M, the task is to find the count of digits in N and M that are the same and are present on the same indices. Examples: Input: N = 123, M = 321Output: 1Explanation: Digit 2 satisfies the condition Input: N = 123, M = 111Output: 1 Approach: The problem can be solved using two-pointer approach. Convert N and M for ease of traver
4 min read
Find smallest number with given number of digits and sum of digits under given constraints
Given two integers S and D, the task is to find the number having D number of digits and the sum of its digits as S such that the difference between the maximum and the minimum digit in the number is as minimum as possible. If multiple such numbers are possible, print the smallest number.Examples: Input: S = 25, D = 4 Output: 6667 The difference be
7 min read
Practice Tags :