Write a program to accept a number and check and display whether it is a spy number or not.
(A number is a spy number if the sum of its digits equals the product of its digits.)
Example: Consider the number 1124
Sum of digits=1+1+2+4= 8
Product of the digits = 1x1x2x4 = 8
Answers
The following cσdes have been written using Python.
n = int(input("Enter a number: "))
print()
s = 0
for i in str(n):
s = s + int(i)
print("The sum of the digits is: ", s)
p = 1
for i in str(n):
p = p*int(i)
print("The product of the digit is: ", p)
print()
if s == p:
print(n, "is a Spy Number.")
else:
print(n, "is not a Spy Number.")
Answer:
This is done in JAVA
//Java Program to check if a number is a spy number or not
package Programmer;
import java.util.Scanner;
public class Spy {
public static void main(String[] args) {
Scanner sc=new Scanner (System.in);
System.out.println("Enter a number");
int n=sc.nextInt();
int s=0,p=1;
for(;n!=0;n/=10){
s+=n%10;
p*=n%10;
}
System.out.println((s==p)?"Spy Number":"Not a Spy Number");
}
}
__
Variable Description:-
- s:- to Calculate sum of the digits
- p:- to calculate product of the digits
- n:- to accept the number from the user