Go to Top

Friday, 9 September 2011

To check whether a number is a Krishna Murthy number or not

Question 44 : Write a program in Java to check whether the number given by the user is a Krishna Murthy number or not.

Krishna Murthy Number : It is a number which is equal to the sum of the factorials of all its digits.

For example : 145 = 1! + 4! + 5! = 1 + 24 + 120 = 145

Java Program : 

import java.io.*;
class krishnamurthy
{
 int fact(int n)
 {
     int i,p=1;
     for(i=n;i>=1;i--)
     p=p*i;
     return p;
    }
    void krishna()throws IOException
    {
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        int a,b,s=0;
        System.out.print("Enter the number : ");
        a=Integer.parseInt(br.readLine());
        System.out.print("\n");
        int n=a;
        while(a>0)
        {
            b=a%10;
            s=s+fact(b);
            a=a/10;
        }
        if(s==n)
        System.out.print(n+" is a krishnamurthy number");
        else
        System.out.print(n+" is not a krishnamurthy number");
    }
}

16 comments:

  1. Working Fine... Just make the factorial part within the main...

    ReplyDelete
  2. krisnamurti numbers btween 1 t 1000

    ReplyDelete
  3. Hey can you please help me with String programs for example finding the frequency of each character in the string entered by the user

    ReplyDelete
    Replies
    1. Use length statement as
      public void main (String s)
      int l=s.length();

      Delete
  4. This the exact Logic I used in making a friends assignment last week
    I also used a seperate function for the calculation of factorial
    :) :)

    ReplyDelete
  5. This is same as that of the special number right???

    ReplyDelete
    Replies
    1. no!! it is diff... special no. is for ex.135...then 1+3+5=9 and 1*3*5*9=135
      but krishna murthy is sum of the factorials of the numbers

      Delete
  6. thank you very much as it has helped me a lot

    ReplyDelete
  7. this means special no. do not name with different to confuse us

    ReplyDelete
  8. It would be better if you did the whole under the main function...

    ReplyDelete
  9. made the program very tuff.......

    ReplyDelete
  10. very tuff to understand
    make it a little simple

    ReplyDelete
  11. Can u do it using scanner class because it is very hard to understand

    ReplyDelete

ShareThis