Embark on a journey of knowledge! Take the quiz and earn valuable credits.
Take A QuizChallenge yourself and boost your learning! Start the quiz now to earn credits.
Take A QuizUnlock your potential! Begin the quiz, answer questions, and accumulate credits along the way.
Take A QuizKindly log in to use this feature. We’ll take you to the login page automatically.
LoginGeneral Tech Bugs & Fixes 3 years ago
User submissions are the sole responsibility of contributors, with TuteeHUB disclaiming liability for accuracy, copyrights, or consequences of use; content is for informational purposes only and not professional advice.
Try breaking down the problem into smaller chunks. I recommend making a few methods which each handle a small portion of the program. You can then test each of them individually. First make a method to check if a number is prime boolean isPrime(int i), then make method to reverse the number, int reverse(int i). Create a method boolean isReversePrime(int i) which calls both isPrime(i) and isPrime(reverse(i)), etc etc. Breaking down a problem into smaller steps may look like more work, but it makes your code much easier to read and also much easier to verify the correctness.
You need to use TreeSet - which will contain only distinct ref="https://forum.tuteehub.com/tag/elements">elements and give result in sorted form. You can refer to following code-
Set<Integer> set = new TreeSet<>();
for(ref="https://forum.tuteehub.com/tag/int">int i = 0; i < a.length; i++) {
boolean isPrime = true;
if(isPrime(a[i]) && isPrime(r[i]))
set.add(a[i]);
}
Also create a ref="https://forum.tuteehub.com/tag/function">function for checking prime numbers -
private static boolean isPrime(ref="https://forum.tuteehub.com/tag/int">int num) {
for(ref="https://forum.tuteehub.com/tag/int">int i = 2; i <= num/2; ++i)
{
if(num % i == 0)
{
ref="https://forum.tuteehub.com/tag/return">return false;
}
}
ref="https://forum.tuteehub.com/tag/return">return true;
}
No matter what stage you're at in your education or career, TuteeHUB will help you reach the next level that you're aiming for. Simply,Choose a subject/topic and get started in self-paced practice sessions to improve your knowledge and scores.
Kindly log in to use this feature. We’ll take you to the login page automatically.
Login
Ready to take your education and career to the next level? Register today and join our growing community of learners and professionals.
Your experience on this site will be improved by allowing cookies. Read Cookie Policy
Your experience on this site will be improved by allowing cookies. Read Cookie Policy
manpreet
Best Answer
3 years ago
Write a program to read n ref="https://forum.tuteehub.com/tag/numbers">numbers. The first number specified as input will be n. Next, the program should read n integer ref="https://forum.tuteehub.com/tag/numbers">numbers.
The program should check for each number if it is prime as well as if its reverse is prime.
Display all such ref="https://forum.tuteehub.com/tag/numbers">numbers in ascending order.
Consider below example for input and output:
Input: 7 11 12 23 19 7 113 101
Output:
7 11 101 113
My code