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

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


0 views   0 shares

profilepic.png
manpreet 2 years ago

You can use Stream.distinct() in Java 8 just pass your array to an array list and remove its duplicates by using .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()); 

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.