Prime number check java

General Tech Bugs & Fixes 2 years ago

0 8 0 0 0 tuteeHUB earn credit +10 pts

5 Star Rating 1 Rating

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.

Take Quiz To Earn Credits!

Turn Your Knowledge into Earnings.

tuteehub_quiz

Answers (8)

Post Answer
profilepic.png
manpreet Tuteehub forum best answer Best Answer 2 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

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((
                                                
                                                
0 views
0 shares
profilepic.png
manpreet 2 years ago

To eliminate duplicates , useSet


0 views   0 shares

profilepic.png
manpreet 2 years ago

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.


0 views   0 shares

profilepic.png
manpreet 2 years ago

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 numberint 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.


0 views   0 shares

profilepic.png
manpreet 2 years ago

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.


0 views   0 shares

profilepic.png
manpreet 2 years ago

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;
    }

0 views   0 shares

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.