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");
    }
}

35 comments:

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

    ReplyDelete
    Replies
    1. import java.util.Scanner;
      public class krishnamurthynumber
      {
      public static void main(String args[])
      {
      Scanner in=new Scanner(System.in);
      System.out.print("Enter the number : ");
      int input,b,sum=0;
      input=in.nextInt();
      System.out.print("\n");
      int n=input;
      while(input>0)
      {
      b=input%10;
      int i,fact=1;
      for(i=b;i>=1;i--)
      fact=fact*i;
      sum=sum+fact;
      input=input/10;
      }
      if(sum==n)
      System.out.print(n+" is a krishnamurthy number");
      else
      System.out.print(n+" is Not a krishnamurthy number");
      }
      }

      Delete
  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
    2. Public static int fact(int n)

      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 you do it using boolean

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

    ReplyDelete
  13. Checking if a number is Krishna involves intricate mathematical analysis. My Stream Unwatchable It's an intriguing concept where certain properties are examined.

    ReplyDelete

ShareThis