Page 221 - ComputerScience_Class_11
P. 221

Program 7      Write a program in Java to input a number and print whether the number is a Krishnamurthy
                                 number or not. A Krishnamurthy number is a number whose sum of the factorial of digits is
                                 equal to the number itself.
                   1      import java.util.*;
                   2      class Krishnamurthy

                   3      {
                   4          public static void main(String args[])
                   5          {
                   6              //Declaring variable and taking value from user...

                   7              Scanner sc = new Scanner(System.in);
                   8              int n, temp, f, rem, sum_factorial=0, i;
                   9              System.out.print("Please enter a number : ");
                  10              n = sc.nextInt();
                  11              temp = n;
                  12              // checking condition for Krishnamurthy number

                  13              while(temp>0)
                  14              {
                  15                  rem = temp % 10;
                  16                  f=1;

                  17                  for(i=1; i<= rem; i++)
                  18                  {
                  19                      f = f * i;
                  20                  }
                  21                  sum_factorial = sum_factorial + f;
                  22                  temp = temp / 10;

                  23              }
                  24              // if they are equal number Krishnamurthy number.
                  25              if(sum_factorial == n)
                  26                  System.out.println(n+ " is a Krishnamurthy  Number.");

                  27              else
                  28                  System.out.println(n+" is Not a Krishnamurthy Number.");
                  29          }
                  30      }


                 The output of the preceding program is as follows:
                       BlueJ: Terminal Window - Java

                   Options

                  Please enter a number: 145
                  145 is a Krishnamurthy Number.




                                                                                              Statements and Scope  219
   216   217   218   219   220   221   222   223   224   225   226