Page 227 - CA_Blue( J )_Class9
P. 227

19                  g--;
                  20              }

                  21              l=(a*b)/g;
                  22              System.out.println("GCD of "+ a +" and "+ b +" is "+ g);

                  23              System.out.println("LCM of "+ a +" and "+ b +" is "+ l);
                  24          }
                  25      }

                 You will get the following output:



















                                 Write a program to accept a number and check and display whether it is a spy number or not.
                  Program 17
                                 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 = 1 + 1 + 2 + 4 = 8 and Product of the digits = 1 × 1 × 2 × 4 = 8
                    1     import java.util.*;

                    2     class prog_spy
                    3     {

                    4         public static void main()
                    5         {

                    6             Scanner sc= new Scanner (System.in);
                    7             int n, s=0 , p=1 , i , r;

                    8             System.out.print("Enter the number: ");
                    9             n = sc.nextInt();
                  10              i=n;

                  11              while(i>0)

                  12              {
                  13                  r = i % 10;
                  14                  s=s+r;

                  15                  p=p*r;




                                                                                       Iterative Constructs in Java  225
   222   223   224   225   226   227   228   229   230   231   232