如何在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