SPY NUMBER | ISC COMPUTER 2024 | survnor.blogspot.com
Write a program to find out all spy number present
within a given range. (What is spy number:- If the sum of its digits equal to
the product of its digits it is called spy number).
Example:- Given number:312, Sum=3+1+2=6 &
Product=3x1x2=6. Therefore, 312 is a spy number.
ALGORITHM
STEP 1:- Start.
STEP 2:- Declare spyno as a class. Declare
r1 a, r2 and i as a variable.
STEP 3:- Declare input() as a method of
return type void. Now accept the starting range and ending range from user and
store it into r1 and r2 simultaneously. Now print the starting range
and ending which has been given by user.
STEP 4:- Now close the method input().
Declare calculate() as a method of return type void.
STEP 5:- Start iteration on ‘i’. Starting
range(r1) assigned on ‘i’ and up to r2 as ending range and incremented 1 in
each step. Now assigned ‘i’ on num.
STEP 6:- Declare rem, sum, multi and f as a
variable where sum ß0, fß0, multiß0.
STEP 7:-
Now declare an iteration where checking whether the expression is num
not equal to=0. Divide the number by 10 and store the individual digits of the
numbers and store it into sum. Now multiply by the digits of the number and
store it into ‘multi’. Now update the expression where number divided by and store it into ‘num’.
STEP 8:- Now close the nested loop and check
whether sum of digits of number and multiplication of digits of number is equal
or not.
STEP 9:- If the above condition which is
mentioned in step 8 is satisfy then print the spy numbers horizontally on 'i’
within the range.
STEP
10:- If the above
condition satisfy which is mentioned in step 8 then incremented frequency in
each step. Now close the outer loop. Now print the frequency of spy number
within the range on ‘f’.
STEP
11:- Now close the
method calculate().Declare main function with return type void. Now create
object ob of class spyno.
STEP
12:- Now call the
above method() (input(), calculate()) through object ob.
STEP
13:- Now close main
function and close the class.
STEP
14:- Stop.
SOURCE CODE
import
java.util.*;
class
spyno{
int i,r1,r2,f=0;
Scanner sc=new Scanner(System.in);
void input(){
System.out.println("Enter the
starting and ending range:");
r1=sc.nextInt();
r2=sc.nextInt();
System.out.println("Spp numbers in
the between "+r1+" to "+r2+" are: ");}
void calculate(){
for(i=r1;i<=r2;i++){
int num=i;
int sum=0,multi=1,rem;
while(num!=0){
rem=num%10;
sum+=rem;
multi*=rem;
num/=10;}
if(sum==multi){
System.out.print(i+" ");
f++;}}
System.out.println("\n Frequency
of Spy Number: "+f);}
public static void main(){
spyno ob=new spyno();
ob.input();
ob.calculate();}}
VARIABLE DESCRIPTION
|
S.L.No. |
Name |
Data Type |
Description |
|
1 |
i |
int |
Use as a loop control variable. |
|
2 |
r1 |
int |
Use to store the starting range. |
|
3 |
r2 |
int |
Use to store the ending range. |
|
4 |
num |
int |
Use as an expression of a loop
where store the value of outer loop ‘i’. |
|
5 |
sum |
int |
Use to store the num of digits of
a number. |
|
6 |
multi |
int |
Use to store the multiplication of
digits of a number. |
|
7 |
f |
int |
Use to calculate
the frequency within the range. |
INPUT AND
OUTPUT