krishnamoorthy数是一个数字的阶乘和等于该数本身的数(例:145=1!+4! + 5!) 我目前的程序可以找到一个数字是否是克里希纳穆尔西数,但我尝试接受10个数字,并显示其中哪些是克里希纳穆尔西数。
import java.util.Scanner;
public class krishnamoorthy {
static int fact(int number)
{
int f = 1;
while (number != 0) {
f = f * number;
number--;
}
return f;
}
static boolean checkNumber(int number)
{
int sum = 0;
int tempNumber = number;
while (tempNumber != 0) {
sum = sum + fact(tempNumber % 10);
tempNumber = tempNumber / 10;
}
if(sum == number)
return true;
else
return false;
}
public static void main(String[] args)
{
int n;
Scanner sc = new Scanner(System.in);
System.out.println("Enter any number:");
n = sc.nextInt();
if (checkNumber(n))
System.out.println(n + " is a krishnamurthy number");
else
System.out.println(n + "is not a krishnamurthy number");
}
}
1条答案
按热度按时间yhuiod9q1#