Page 197 - Cs_withBlue_J_C11_Flipbook
P. 197

3
                 Area of an isosceles triangle : 4.47213595499958


                  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()

                   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      }



                                                                                                                       195
                                                                                                 Statements and Scope  195
   192   193   194   195   196   197   198   199   200   201   202