Programming Problem 1 Write a program to print all the 5-digit palindrome number
ID: 3764938 • Letter: P
Question
Programming Problem 1
Write a program to print all the 5-digit palindrome numbers. A number with the same digits from either end is called a palindrome.
Examples: 16261, 93439, 20102 are palindromes.
Programming Problem 2
For this problem, follow the instructions for Program #14 in Chapter 6 Exercises of your textbook. Keep the following points in mind:
Create a class named Purchase. Each Purchase contains an invoice number, amount of sale, and amount of sales tax. Include set methods for the invoice number and sale amount. Within the set () method for the sale amount, calculate the sales tax as 5% of the sale amount. Also include a display method that displays a purchase’s details. Save the file as Purchase. java
Create an application that declares a Purchase object and prompts the user for purchase details. When you prompt for an invoice number, do not let the user proceed until a number between 1,000 and 8,000 has been entered. When you prompt for a sale amount, do not proceed until the user has entered a non-negative value. After a valid Purchase object has been created, display the object’s invoice number, sale amount, and sales tax. Save the file as CreatePurchase.java
Please submit the source code for your program along with a word document that includes screen shots of the compiled results and executed output. The page margins on the top, bottom, left side, and right side should be 1 inch each.
Submit the source code along with the Word document to the appropriate dropbox. See the Course Calendar for due dates. You will be penalized for late submission.
Explanation / Answer
Solution:
package com.chegg.nancy.solutions;
public class Palandrome {
//Logic for palindrome number is written over here.
public static boolean isPalindromic(Integer x) {
String s = x.toString();
int len = s.length();
for (int i = 0;i<len;i+=3) {
if (s.charAt(i) != s.charAt(len-i-1)) return false;
}
return true;
}
public static void main(String[] args) {
for (Integer x = 10000; x <= 99999; x++) {
//Here checking for the palindrome number if the function returns true then corresponding number will print.
if (isPalindromic(x))
System.out.println(x);
}
}
}
Sample Output:
10001
10101
10201
10301
10401
10501
10601
10701
......
99699
99799
99899
99999
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.