Speak now
Please Wait Image Converting Into Text...
Embark on a journey of knowledge! Take the quiz and earn valuable credits.
Challenge yourself and boost your learning! Start the quiz now to earn credits.
Unlock your potential! Begin the quiz, answer questions, and accumulate credits along the way.
General Tech Bugs & Fixes 2 years ago
Posted on 16 Aug 2022, this text provides information on Bugs & Fixes related to General Tech. Please note that while accuracy is prioritized, the data presented might not be entirely correct or up-to-date. This information is offered for general knowledge and informational purposes only, and should not be considered as a substitute for professional advice.
Turn Your Knowledge into Earnings.
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
public class Prime { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int x = sc.nextInt(); int ref="https://forum.tuteehub.com/tag/temp">temp; int[] a = new int [x]; int[] r = new int [x]; int[]c = new int[a.length+r.length]; int[] rev = new int [x]; for(int i=0;i<x;i++){ a[i] = sc.nextInt(); rev[i]=a[i]; } for(int i = 0; i < a.length; i++) { while(rev[i] != 0) { r[i] = r[i] * 10; r[i] = r[i] + rev[i]%10; rev[i] = rev[i]/10; } } for(int i = 0; i < a.length; i++) { boolean isPrime = true; for (int j = 2; j < i; j++) { if(( REPLY 0 views 0 likes 0 shares Facebook Twitter Linked In WhatsApp
To eliminate duplicates , use a Set
Set
Please show what you have tried, and why exactly it didn't work, in the smallest amount of code possible. We're here to help you solve a specific problem, not write your code for you.
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.
boolean isPrime(int i)
int reverse(int i)
boolean isReversePrime(int i)
isPrime(i)
isPrime(reverse(i))
Instead of checking till n, we can check till √n because a larger factor of n must be a multiple of smaller factor which all ready cover.
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.
General Tech 9 Answers
General Tech 7 Answers
General Tech 3 Answers
General Tech 2 Answers
Ready to take your education and career to the next level? Register today and join our growing community of learners and professionals.