BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

Spy Number in Java with example

By Chaitanya Singh | Filed Under: Java Examples

In this tutorial, we will write a java program to check whether a given number is Spy number or not. We will also write a program to find all the Spy numbers in a given range.

What is a Spy Number?

A number is called spy number if the sum of its digits is equal to the product of its digits. For example 123 is a Spy number because:

number = 123
sum of its digits = 1+2+3 = 6
product of its digits = 1*2*3 = 6
sum of digits = product of digits
Hence 123 is a Spy number

Similarly 1124 is a Spy number:

number = 1124
sum of its digits = 1+1+2+4 = 8
product of its digits = 1*1*2*4 = 8
sum of digits = product of digits
Hence 1124 is a Spy number

Lets take a random number 256 and check for Spy number:

number = 256
sum of its digits = 2+5+6 = 13
product of its digits = 2*5*6 = 60
sum of digits != product of digits
Hence 256 is NOT a Spy number

Spy Number Program in Java

Steps to check for Spy number:

  1. Read the number entered by user using Scanner class.
  2. Declare and initialize two variable sumProductDigits and sumNumberDigits.
  3. Find the last digit (number%10) of the input number by using the modulo operator.
  4. Add the last digit found in step 3 to the variable sumProductDigits and multiply the last digit with the sumNumberDigits.
  5. Divide the number by 10 to remove the last digit so that next time the second last digit gets used in the addition and multiplication.
  6. Repeat steps from step 3 to step 6 until the given number number becomes 0.
  7. If the value of sumProductDigits and sumProductDigits are equal, then display number is a spy number, else display number is not a spy number.
import java.util.Scanner;
public class JavaExample
{
  public static void main(String args[])
  {
    int num, sumProductDigits=1, sumNumberDigits=0, temp;
    Scanner scanner = new Scanner(System.in);
    System.out.print("Enter any number: " );
    num=scanner.nextInt();
    scanner.close();
    //copying the value of num in another variable
    //to perform operations on it.
    int number  = num;
    while(number>0)
    {
      temp=number%10;
      sumNumberDigits=sumNumberDigits+temp;
      sumProductDigits=sumProductDigits*temp;
      number=number/10;
    }
    if(sumNumberDigits==sumProductDigits)
      System.out.println(num+" is a spy number.");
    else
      System.out.println(num+" is not a spy number.");
  }
}

Output:

Enter any number: 123
123 is a spy number.

Output 2:

Enter any number: 235
235 is not a spy number.

Java Program to find all the Spy numbers in a given range

Here we are finding all the spy numbers in a given range. We have created a user-defined method checkSpyNumber(), which has the same logic that we have used in above program for checking if a number is Spy number.

The user is asked to enter start and end range, a loop runs from start range till end range and check for each number in between for spy number using checkSpyNumber() method. If the method returns true program displays the number else it skips the number. That’s how all the Spy number between start range and end range gets displayed.

import java.util.Scanner;
public class JavaExample
{
  private static boolean checkSpyNumber(int num)
  {
    int lastDigit;
    int sumNumberDigits = 0, sumProductDigits = 1;
    while(num != 0)
    {
      lastDigit = num % 10;
      sumNumberDigits = sumNumberDigits + lastDigit;
      sumProductDigits = sumProductDigits * lastDigit;
      num = num / 10;
    }
    if(sumNumberDigits == sumProductDigits)
      return true;
    return false;
  }
  public static void main(String args[])
  {
    int startRange, endRange;
    Scanner scanner = new Scanner(System.in);
    System.out.print("Enter the starting range: ");
    startRange = scanner.nextInt();
    System.out.print("Enter the ending range: ");
    endRange = scanner.nextInt();
    scanner.close();
    System.out.println("All Spy numbers between "+ startRange + " and "+ endRange+" are: ");
    for(int i=startRange; i<=endRange; i++)
    {
      if(checkSpyNumber(i))
        System.out.print(i +" ");
    }
  }
}

Output:

Enter the starting range: 1
Enter the ending range: 100
All Spy numbers between 1 and 100 are: 
1 2 3 4 5 6 7 8 9 22 

Related Java Programs

  1. Neon Number in Java
  2. Peterson Number in Java
  3. Fascinating Number in Java
  4. Automorphic Number in Java
❮ Java TutorialJava Programs ❯

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Programs

  • C Programs
  • Java Programs
  • C++ Programs

Java Examples

  • Check Odd-even
  • Linear Search
  • Binary Search
  • Floyd's Triangle
  • Reverse number
  • Random Number
  • first n prime numbers
  • Disp prime Numbers
  • Check Prime number
  • Palindrome String
  • Find factorial
  • Sum of elements of Array
  • Area of rectangle
  • Area of Square
  • Area of Triangle
  • Circle

Tutorials

  • Java Tutorial
  • OOPs Concepts
  • Java String
  • Exception handling
  • Java Multithreading
  • Java I/O
  • Java Serialization
  • Java Regex
  • Java AWT
  • Java Swing
  • Java Enum
  • Java Annotations

Copyright © 2012 – 2022 BeginnersBook . Privacy Policy . Sitemap