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.
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...
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.
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....
5. Let n = 60, not a product of distinct prime numbers. Let Bn= the set...
5. Let n = 60, not a product of distinct prime numbers. Let Bn= the set of all positive divisors of n. Define addition and multiplication to be lcm and gcd as well. Now show that Bn cannot consist of a Boolean algebra under those two operators. Hint: Find the 0 and 1 elements first. Now find an element of Bn whose complement cannot be found to satisfy both equalities, no matter how we define the complement operator.
Show that among all collections with 2n-1 natural numbers in them there are exactly n numbers...
Show that among all collections with 2n-1 natural numbers in them there are exactly n numbers whose sum is divisible by n.
write a java program that takes three numbers from the user and print the greatest number...
write a java program that takes three numbers from the user and print the greatest number (using if-statement). sample output: input the first number:35 input the second number:28 input the third number:87 the greatest number:87
A number is prime if it can be divided exactly only by 1 and itself. Here...
A number is prime if it can be divided exactly only by 1 and itself. Here is an algorithm for checking if an input number is prime: function out = isprime(m)             for j:=2 to m-1             if mod(m,j) ==0 then             return “input is not a prime”             endif             endfor             return ”input is a prime” Let n denote the size of the input m, i.e. the number of bits in the binary expression for m. What is...
Consider the statement, “For all natural numbers n,n, if nn is prime, then nn is solitary.”...
Consider the statement, “For all natural numbers n,n, if nn is prime, then nn is solitary.” You do not need to know what solitary means for this problem, just that it is a property that some numbers have and others do not. Write the converse and the contrapositive of the statement, saying which is which. Note: the original statement claims that an implication is true for all n,n, and it is that implication that we are taking the converse and...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT