如何在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中检查一个数是否为帝国数。
广告
数据结构
网络
关系数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP