InterviewSolution
Saved Bookmarks
| 1. |
Write a program to accept a number and check and display whether it is a spy number or not. (A number is spy if the sum of its digits equals the product of its digits.)Example : consider the number 1124, Sum of the digits = l + l+ 2 + 4 = 8 Product of the digits = 1×1 x2x4 = 8 |
|
Answer» class Spy { public static void main(int n) { int sum = 0; int multiple = 1; int a; int p = n; // a stores each digit extracted and p creates a backup of input. while(n ! = 0) { a = n % 10; , sum = sum + a; multiple = multiple * a; n = n/10; } System.out.println(“The sum of ” +p +” is ”+sum); System.out.println(“The product of “+p +” is ” + multiple); if(sum = = multiple) { System.out.println(“Aha, ” + “It is a Spy Number Where Sum = Product”); } else { System.out.println(” It is NOT a Spy Number Where Sum ft Product”); } } } |
|