# Find out all Spy numbers present within a given range

A Number is a Spy number if the sum of its digits equal to the product of its digits.

Example:

Given Number=312
Sum of digits =3+1+2=6
product of digits=3*1*2=6

Problem statement:-  Program to Find out all Spy numbers present within a given range

Data requirement:-

Input Data:- range1, range2

Output Data:- i, range1, range2

Additional Data:- num, sum, rem, mult

Program in C

Here is the source code of the C Program to Find out all Spy numbers present within a given range.

Code:

//spy Number in range
#include<stdio.h>
int main()
{
int range1,range2,i;
printf("Enter a range:");
scanf("%d %d",&range1,&range2);
printf("Spy numbers between %d and %d are: ",range1,range2);
for(i=range1;i<=range2;i++)
{
int num=i;
int sum=0,mult=1,rem;
while(num!=0)
{
rem=num%10;
sum+=rem;
mult*=rem;
num/=10;
}

if(sum==mult)
printf("%d ",i);
}
}

Input/Output:
Enter a range:5
1000
Spy numbers between 5 and 1000 are: 5 6 7 8 9 22 123 132 213 231 312 321

Program in C++

Here is the source code of the C++ Program to Find out all Spy numbers present within a given range.

Code:

#include<iostream>
using namespace std;
int main()
{
int range1,range2,i;
cout<<"Enter a range:";
cin>>range1;
cin>>range2;
cout<<"Spy numbers between "<<range1<<" and "<<range2<<" are: ";
for(i=range1;i<=range2;i++)
{
int num=i;
int sum=0,mult=1,rem;
while(num!=0)
{
rem=num%10;
sum+=rem;
mult*=rem;
num/=10;
}

if(sum==mult)
cout<<i<<" ";
}
}

Input/Output:
Enter a range:100
2000
Spy numbers between 100 and 2000 are: 123 132 213 231 312 321 1124 1142 1214 1241 1412 1421

Program in Java

Here is the source code of the Java Program to Find out all Spy numbers present within a given range.

Code:

import java.util.Scanner;
public class FindSpyNumbersInRange {

public static void main(String[] args) {
Scanner cs=new Scanner(System.in);
int range1,range2;
System.out.println("Enter a range:");
range1=cs.nextInt();
range2=cs.nextInt();
System.out.println("Spy numbers between "+range1+" and "+range2+" are: ");
for(int i=range1;i<=range2;i++)
{
int num=i;
int sum=0,mult=1,rem;
while(num!=0)
{
rem=num%10;
sum+=rem;
mult*=rem;
num/=10;
}

if(sum==mult)
System.out.print(i+" ");
}
cs.close();
}
}

Input/Output:
Enter a range:
10000
12000
Spy numbers between 10000 and 12000 are:
11125 11133 11152 11215 11222 11251 11313 11331 11512 11521

Program in Python

Here is the source code of the Python Program to Find out all Spy numbers present within a given range.

Code:

print("Enter a range:")
range1=int(input())
range2=int(input())
print("Spy numbers between ",range1," and ",range2," are: ")
for i in range(range1,range2+1):
num=i
sum = 0
mult = 1
while num != 0:
rem = num % 10
sum += rem
mult *= rem
num //= 10

if sum == mult:
print(i,end=" ")

Input/Output:
Enter a range:
1
1000
Spy numbers between  1  and  1000  are:
1 2 3 4 5 6 7 8 9 22 123 132 213 231 312 321

More:-