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
Here is the code for prime test using √n approach
static boolean isPrime(nt">int n){ //corner case if (n <= 1) return false; if (n <= 3) return true; //middle 5 number if (n % 2 == 0 || n % 3 == 0) return false; for (nt">int i = 5; i * i <= n; i = i + 6) if (n % i == 0 || n % (i + 2) == 0) return false; return true; }
Use can use set for remove duplicate element
set
You can use Stream.distinct() in Java 8 just pass your array to an array list and remove its duplicates by using .distinct()
.distinct()
import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.stream.Collectors; //list with duplicates List<Integer> list = new ArrayList<>( Arrays.asList(3, 10, 3, 3, 4, 5, 5)); //new list without duplicates List<Integer> newList = list.stream() .distinct() .collect(Collectors.toList());
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 10 Answers
General Tech 7 Answers
General Tech 3 Answers
General Tech 9 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.