Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Secondary #844

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions Java/Krishnamurthy.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import java.util.*; // Importing package
class Krishnamurthy
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
int n,c,s=0;
System.out.println("Enter Number");
n=sc.nextInt(); // Taking input
c=n; // Making a copy
while(c>0)
{
int d=c%10; // Extracting digits
s=s+Factorial(d); // Adding factorial of all digits
c=c/10;
}
if(s==n)
System.out.println(n+" is a Krishnamurthy Number."); // Displaying the final output
else
System.out.println(n+" is not a Krishnamurthy Number."); // Displaying the final output
}
// Function to find factorial of a digit
public static int Factorial(int n)
{
int f=1,i;
for(i=1;i<=n;i++)
f=f*i;
return f; // Returning value
}
}

/*
Input:145
Output:145 is a Krishnamurthy Number.
Input:252
Output:252 is not a Krishnamurthy Number.
Time Complexity:O(m*r), where m is number of digits and r is value of digits
Space Complexity:O(1)
*/
2 changes: 2 additions & 0 deletions Java/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,8 @@ Format: -[Program name](package/name of the file)

-[Kosaraju's Algorithm for Strongly Connected Components](Algorithms/Kosaraju.java)

-[Krishnamurthy Number](Krishnamurthy.java)

-[Linear Search](Search/LinearSearch.java)

-[Longest common sequence](Algorithms/Longest_common_subsequence.java)
Expand Down