ICSE Java Programming Spy Number 2017 Q5 Solved
Example : consider the number 1124.
Sum of the digits = 1 + 1+ 2 + 4 = 8
Product of the digits = 1×1 x 2 x 4 = 8
Thus 1124 is a Spy Number
Java Code
import java.util.Scanner;
public class ICSEJava {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int sum_of_digits = 0;
int product_of_digits = 1;
int number, digit, temp;
System.out.println("Enter a positive integer:");
number = in.nextInt();
temp = number;
while (temp > 0) {
digit = temp % 10;
temp = (int) (temp / 10);
sum_of_digits += digit;
product_of_digits *= digit;
}
if (sum_of_digits == product_of_digits)
System.out.println(number + " is a spy number.");
else
System.out.println(number + " is not a spy number.");
}
}
Python Code
sum_of_digits = 0
product_of_digits = 1
number = int(input("Enter a postive integer:"))
temp = number
while temp > 0:
digit = temp%10
temp = int(temp/10)
sum_of_digits += digit
product_of_digits *= digit
if sum_of_digits == product_of_digits:
print(number," is a spy number.")
else:
print(number," is not a spy number.")
No comments:
Post a Comment