Java 如何检查一个数字是否为帝国数
如果在反转一个质数后得到另一个质数,则该数字被称为帝国数。
在这篇文章中,我们将看到如何使用Java编程语言来检查一个数字是否为帝国数。
为了向您展示一些示例
示例-1
输入数字是13。
让我们通过使用帝国数逻辑来检查它。
13是一个质数。
如果我们反转13,则得到= 31
我们注意到这里反转后的数字和输入数字都是质数。
因此,13是一个帝国数。
示例-2
输入数字是79。
让我们通过使用帝国数逻辑来检查它。
143是一个质数。
如果我们反转79,则得到= 97
我们注意到这里反转后的数字和输入数字都是质数。
因此,79是一个帝国数。
示例-3
输入数字是53。
让我们通过使用帝国数逻辑来检查它。
53是一个质数。
如果我们反转53,则得到= 35
我们注意到这里反转后的数字不是质数。
因此,53不是一个帝国数。
步骤
步骤1 - 通过初始化或用户输入获取输入数字。
步骤2 - 检查输入的数字是否为质数。
步骤3 - 然后采用用户定义的方法来检查输入数字是否为帝国数。
步骤4 - 在方法内部,获取所需的反转数字,并检查该数字是否为质数。
步骤5 - 如果两个数字都是质数,那么原始数字称为帝国数,否则不是。
多种方法
我们以不同的方法提供了解决方案。
- 通过使用用户定义的方法与静态输入值
-
使用用户输入值与用户定义的方法
让我们逐个看程序及其输出。
方法1:通过使用静态输入值与用户定义的方法
在这个方法中,我们声明一个带有静态输入的变量,然后通过使用算法,我们可以检查数字是否为帝国数。
示例
import java.util.*;
public class Main {
public static void main (String args[]) {
//declare a variable and pass the static input
int inp= 13;
//checking for empire number
if (checkEmpire(inp) == true)
System.out.println(inp + " is an empire number.");
else
System.out.println(inp + " is not an empire number.");
}
//user defined function to find the prime number
public static boolean checkPrime (int n) {
if (n <= 1)
return false;
//initiate the loop to check the prime number
for (int i = 2; i < n; i++)
if (n % i == 0)
//if condition true then return false
return false;
//otherwise return true
return true;
}
//function that checks if the given number is empire or not
public static boolean checkEmpire(int inputNumber) {
//check whether the input number is prime number or not
if (checkPrime (inputNumber) == false)
return false;
//declare a variable to store the reverse of input number
int reverse = 0;
//initiate a loop to calculate the reverse number
while (inputNumber != 0) {
//collect the last digit
int digit = inputNumber % 10;
//store the reversed value
reverse = reverse * 10 + digit;
//remove the last digit from input number
inputNumber = inputNumber / 10;
}
//calling the user-defined function that checks the reverse number is prime or not
return checkPrime(reverse);
}
}
输出
13 is not an empire number.
方法2:使用用户输入值和用户定义的方法
在这种方法中,我们要求用户输入一个数字作为输入值,并将这个数字作为参数传递给用户定义的方法,然后在方法内部,通过使用算法来检查这个数字是否是一个帝国数。
示例
import java.util.*;
public class Main {
public static void main (String args[]) {
//create object of Scanner class
Scanner sc=new Scanner(System.in);
//ask user to give the input
System.out.print("Enter a number: ");
//declare a variable and store the input value
int inp=sc.nextInt();
//checking for empire number
if (checkEmpire(inp) == true)
System.out.println(inp + " is an empire number.");
else
System.out.println(inp + " is not an empire number.");
}
//user defined function to find the prime number
public static boolean checkPrime(int n) {
if (n <= 1)
return false;
//initiate the loop to check the prime number
for (int i = 2; i < n; i++)
if (n % i == 0)
//if condition true then return false
return false;
//otherwise return true
return true;
}
//function that checks if the given number is empire or not
public static boolean checkEmpire(int inputNumber) {
//check whether the input number is prime number or not
if (checkPrime (inputNumber) == false)
return false;
//declare a variable to store the reverse of input number
int reverse = 0;
//initiate a loop to calculate the reverse number
while (inputNumber != 0) {
//collect the last digit
int digit = inputNumber % 10;
//store the reversed value
reverse = reverse * 10 + digit;
//remove the last digit from input number
inputNumber = inputNumber / 10;
}
//calling the user-defined function that checks the reverse number is prime or not
return checkPrime(reverse);
}
}
输出
Enter a number: 79
79 is an empire number.
在本文中,我们探讨了如何使用不同的方法在Java中检查一个数字是否是Empire数字。