HW5.3

 1 import java.util.Scanner;
 2
 3 public class Solution
 4 {
 5     public static void main(String[] args)
 6     {
 7         Scanner input = new Scanner(System.in);
 8         System.out.print("Enter a number: ");
 9         int number = input.nextInt();
10
11         input.close();
12
13         System.out.println(number + " is a palindrome: " + isPalindrome(number));
14     }
15
16     public static int reverse(int number)
17     {
18         String strOfNumber = "";
19         if(number < 10)
20             strOfNumber += number + "";
21         else
22             strOfNumber += "" + number % 10 + reverse(number / 10);
23         return Integer.parseInt(strOfNumber);
24     }
25
26     public static boolean isPalindrome(int number)
27     {
28         int backupNumber = reverse(number);
29         if(number == backupNumber)
30             return true;
31         else
32             return false;
33     }
34 }
时间: 2024-11-10 08:10:22

HW5.3的相关文章

HW5.1

1 public class Solution 2 { 3 public static void main(String[] args) 4 { 5 int count = 0; 6 7 for(int i = 1; i <= 100; i++) 8 { 9 System.out.print(getPentagonalNumber(i) + " "); 10 count++; 11 12 if(count == 10) 13 { 14 System.out.println();

HW5.14

1 public class Solution 2 { 3 public static void main(String[] args) 4 { 5 System.out.printf("%s\t%s\n", "i", "m(i)"); 6 7 for(int i = 10; i <= 100; i += 10) 8 System.out.printf("%d\t%f\n", i, getMi(i * 1.0)); 9

HW5.2

1 import java.util.Scanner; 2 3 public class Solution 4 { 5 public static void main(String[] args) 6 { 7 Scanner input = new Scanner(System.in); 8 System.out.print("Enter the number: "); 9 long number = input.nextLong(); 10 11 input.close(); 12

HW5.13

1 public class Solution 2 { 3 public static void main(String[] args) 4 { 5 System.out.printf("%s\t%s\n", "i", "m(i)"); 6 7 for(int i = 1; i <= 20; i++) 8 System.out.printf("%d\t%f\n", i, getMi(1.0 * i)); 9 } 10 1

HW5.10

1 public class Solution 2 { 3 public static void main(String[] args) 4 { 5 int count = 0; 6 7 for(int i = 1; i < 10000; i++) 8 if(isPrime(i)) 9 count++; 10 11 System.out.println("The count of the prime number smaller than 10000 is: " + count)

HW5.30

1 public class Solution 2 { 3 public static void main(String[] args) 4 { 5 for(int i = 3; i <= 1000; i++) 6 if(isDoublePrime(i)) 7 System.out.println("(" + i + ", " + (i + 2) + ")"); 8 } 9 10 public static boolean isDouble

HW5.4

1 import java.util.Scanner; 2 3 public class Solution 4 { 5 public static void main(String[] args) 6 { 7 Scanner input = new Scanner(System.in); 8 System.out.print("Enter a number: "); 9 int number = input.nextInt(); 10 11 input.close(); 12 13 r

HW5.32

1 public class Solution 2 { 3 public static void main(String[] args) 4 { 5 int n1 = (int)(Math.random() * 5 + 1); 6 int n2 = (int)(Math.random() * 5 + 1); 7 int winCount = 0; 8 9 for(int i = 0; i < 10000; i++) 10 { 11 if(diceGame(n1, n2)) 12 winCount

HW5.9

1 public class Solution 2 { 3 public static void main(String[] args) 4 { 5 System.out.printf("%s\t%s\t%s\t%s\n", "Inch", "Meter", "Meter", "Inch"); 6 7 for(double i = 1, j = 20; i <= 10 && j <