Question

In: Computer Science

Java program Prime Numbers A prime number is a natural number which has exactly two distinct...

Java program

Prime Numbers

A prime number is a natural number which has exactly two distinct natural number divisors: 1 and itself. For example, the first four prime numbers are: 2, 3, 5 and 7.

Write a java program which reads a list of N integers and prints the number of prime numbers in the list.

Input: The first line contains an integer N, the number of elements in the list.

N numbers are given in the following lines.

Output: Print the number of prime numbers in the given list.

Constraints:

1 ≤ N ≤ 10000

2 ≤ an element of the list ≤ 108

Sample Input 1

5
2
3
4
5
6

Sample Output 1

3

Sample Input 2

11
7
8
9
10
11
12
13
14
15
16
17

Sample Output 2

4

Solutions

Expert Solution

Solution for the given question are as follows -

Code :

import java.util.Scanner;
public class Main
{
// check for duplicate element
public static boolean checkDuplicate(int a[] , int n) {
int c = 0;
for(int i = 0; i < a.length; i++)
{
if (a[i] == n) {
c++;
}
}
if (c == 1) {
return false;
}
return true;
}
public static void main(String[] args)
{
// local variable declaration
int n ,count = 0;
boolean flag = false;
Scanner s = new Scanner(System.in);
// get input from user
System.out.print("How many elements you want in array:");
n = s.nextInt();
int a[] = new int[n];
System.out.println("Enter all the elements:");
for(int i = 0; i < n; i++)
{
a[i] = s.nextInt();
// check duplicate elements
boolean duplicate = checkDuplicate(a,a[i]);
if (duplicate) {
continue;
}
// check element is prime or not
for(int j = 2; j <= a[i]/2; ++j)
{
if(a[i] % j == 0)
{
flag = true;
break;
}
}
// increment the prime count
if (!flag) {
count ++ ;
}
flag = false;
}
// print the count
System.out.println("Total Prime Numbers:"+count);
}
}

Code Screen Shot :

Output :

1)

2)


Related Solutions

in java Write a program that reads in ten numbers and displays the number of distinct...
in java Write a program that reads in ten numbers and displays the number of distinct numbers and the distinct numbers separated by exactly one space (i.e., if a number appears multiple times, it is displayed only once). (Hint: Read a number and store it to an array if it is new. If the number is already in the array, ignore it.) After the input, the array contains the distinct numbers. Here is the sample run of the program: Enter...
A prime number (or prime) is a natural number greater than 1 that has no posítive...
A prime number (or prime) is a natural number greater than 1 that has no posítive divisors other than 1 and itself. Write a Python program which takes a set of positive numbers from the input and returns the sum of the prime numbers in the given set. The sequence will be ended with a negative number.
Using Java Prime numbers. Write a program that prompts the user for an integer and then...
Using Java Prime numbers. Write a program that prompts the user for an integer and then prints out all prime numbers up to that integer. For example, when the user enters 20, the program should print 2 3 5 7 11 13 17 19 Recall that a number is a prime number if it is not divisible by any number except 1 and itself. Use a class PrimeGenerator with methods nextPrime and isPrime. Supply a class PrimePrinter whose main method...
Let p and q be any two distinct prime numbers and define the relation a R...
Let p and q be any two distinct prime numbers and define the relation a R b on integers a,b by: a R b iff b-a is divisible by both p and q. For this relation R: Prove that R is an equivalence relation. you may use the following lemma: If p is prime and p|mn, then p|m or p|n. Indicate in your proof the step(s) for which you invoke this lemma.
Write a program to find the prime numbers IN JAVA Ask user to input the integer...
Write a program to find the prime numbers IN JAVA Ask user to input the integer number test the number whether it is a prime number or not Then, print “true” or “false” depending on whether the number is prime or isn’t. Hint: number is prime when is has exactly 2 factors: one and itself. By this definition, number 1 is a special case and is NOT a prime. Use idea of user input, cumulative sum, and loop to solve...
write java program that prompt the user to enter two numbers .the program display all numbers...
write java program that prompt the user to enter two numbers .the program display all numbers between that are divisible by 7 and 8 ( the program should swap the numbers in case the secone number id lower than first one please enter two integer number : 900 199 Swapping the numbers 224 280 336 392 448 504 560 616 672 728 784 840 896 i need it eclipse
write java program that prompt the user to enter two numbers .the program display all numbers...
write java program that prompt the user to enter two numbers .the program display all numbers between that are divisible by 7 and 8 ( the program should swap the numbers in case the secone number id lower than first one please enter two integer number : 900 199   Swapping the numbers 224 280 336 392 448 504 560 616 672 728 784 840 896 i need it eclipse
use Python The assertion that every even number is the sum of two prime numbers is...
use Python The assertion that every even number is the sum of two prime numbers is called Goldbach’s conjecture. You will write a program that asks the user for an integer number, then checks if the integer is even, and finally determines two prime numbers that sum to the user’s entered integer number. Assignment Requirements Three functions are required: get_input(): This function takes no parameters, but will ask the user for an integer number. It will return a valid integer....
[PYTHON] Two natural number p,q are called coprime if there are no common prime factors in...
[PYTHON] Two natural number p,q are called coprime if there are no common prime factors in their prime factorization. E.g. 15 and 20 are not coprime because 5 is a common prime number in their prime factorization, while 20 and 9 are coprime. The Euler’s phi function, φ(n), counts the number of all natural numbers ≤ n which are coprime to n. E.g. φ(9) = 6, as all natural numbers ≤ 9 are 1,2,3,4,5,6,7,8,9. And out of these, the numbers...
Write a SPIM program to find weather two numbers are relatively prime. Two integers are said...
Write a SPIM program to find weather two numbers are relatively prime. Two integers are said to be relatively prime if there is no integer greater than one that divides them both. Sample I/O: Enter the first number: 14 Enter the second number: 15 The entered numbers are relatively prime. Sample I/O: Enter the first number: 12 Enter the second number: 15 The entered numbers are not relatively prime.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT