Question

In: Computer Science

For a given integer n > 1, list all primes not exceeding n. Example: n=10, output:...

For a given integer n > 1, list all primes not exceeding n.

Example:

n=10, output: 2,3,5,7

n=16, output: 2,3,5,7,11,13

In Java please

Solutions

Expert Solution

For a given integer n > 1, list all primes not exceeding n.

Example:

n=10, output: 2,3,5,7

n=16, output: 2,3,5,7,11,13

In Java please.........

import java.util.Scanner; //importing scanner function
class PrimeNumber{ //class name
public static void main(String[] args){ // main method
Scanner s = new Scanner(System.in); //scanner declartion it is the user input to read
System.out.println("enter the max number"); //printing the asking user input range
int n = s.nextInt(); //reading user input
System.out.println("list of the prime number upto given range "+n);
for(int i=2;i<=n;i++){ //for loop for range upto 2 to given range . it will start 2
boolean isPrime = true; //boolean declartion
for (int j=2;j<=i/2;j++){ //inner loop
if(i%j == 0){ //logic for checking whether it prime or not
isPrime = false; //if is 0 its not prime
break;
}
}
if( isPrime == true) //if it not zero its prime
System.out.println(i); // printing the prime number
}
}
}
output:

enter the max number
16
list of the prime number upto given range 16
2
3
5
7
11
13

output1:

enter the max number
10
list of the prime number upto given range 10
2
3
5
7


Related Solutions

For a given positive integer n, output the first n primes. Example: n=3, output: 2,3,5; n=7,...
For a given positive integer n, output the first n primes. Example: n=3, output: 2,3,5; n=7, output: 2,3,5,7,11,13,17. In Java please
Prove: There are infinitely many primes of the form 6n − 1 (n is an integer).
Prove: There are infinitely many primes of the form 6n − 1 (n is an integer).
In Coral. Given a sorted list of integers, output the middle integer .Assume the number of...
In Coral. Given a sorted list of integers, output the middle integer .Assume the number of integers ia odd. Ex: if the input 2 3 4 8 11 -1(a negative indicates end), the output is 4. the maximum number of inputs for any test case should not exceed 9 positive values. If exceeded , output Too many inputs". Hint: Use an array of size 9. First read the data into array.Then,based in the number of items, find the middle item.
Create a mathematical proof to prove the following: Given an integer n, and a list of...
Create a mathematical proof to prove the following: Given an integer n, and a list of integers such that the numbers in the list sum up to n. Prove that the product of a list of numbers is maximized when all the numbers in that list are 3's, except for one of the numbers being either a 2 or 4, depending on the remainder of n when divided by 3.
In C++ Given a sorted list of integers, output the middle integer. A negative number indicates...
In C++ Given a sorted list of integers, output the middle integer. A negative number indicates the end of the input (the negative number is not a part of the sorted list). Assume the number of integers is always odd. Ex: If the input is: 2 3 4 8 11 -1 the output is: Middle item: 4 The maximum number of inputs for any test case should not exceed 9. If exceeded, output "Too many numbers". Hint: First read the...
Given a list of positive integers c[0...n − 1], and a positive integer v, decides whether...
Given a list of positive integers c[0...n − 1], and a positive integer v, decides whether we can use numbers from c[0...n − 1] to make a sum of v, allowing any particular number in the list to be used multiple times. Or, mathematically, this means that there exists non-negative integer coefficients, x0, x1, ..., xn−1, such that v = x0c[0] + x1c[1] + ...xn−1c[n − 1]. For example, given c[0...3] = {2, 4, 6, 10}, and v = 17,...
A Mystery Algorithm Input: An integer n ≥ 1 Output: ?? Find P such that 2...
A Mystery Algorithm Input: An integer n ≥ 1 Output: ?? Find P such that 2 P is the largest power of two less than or equal to n. Create a 1-dimensional table with P +1 columns. The leftmost entry is the Pth column and the rightmost entry is the 0th column. Repeat until P < 0 If 2 P ≤ n then put 1 into column P set n := n − 2 P Else put 0 into column...
Given a positive integer k and an array A[1..n] that contains the quiz scores of n...
Given a positive integer k and an array A[1..n] that contains the quiz scores of n students in ascending order, design a divide and conquer algorithm to efficiently count the number of students that have quiz scores in (100(i − 1)/k, 100i/k] for integers 1 ≤ i ≤ k. Let group i be the set of students with quiz scores in (100(i − 1)/k, 100i/k] for integers 1 ≤ i ≤ k. The counting result should be stored in G[1..k],...
A Mystery Algorithm Input: An integer n ≥ 1 Output: ?? Find P such that 2^p...
A Mystery Algorithm Input: An integer n ≥ 1 Output: ?? Find P such that 2^p is the largest power of two less than or equal to n. Create a 1-dimensional table with P +1 columns. The leftmost entry is the Pth column and the rightmost entry is the 0th column. Repeat until P < 0 If 2^p≤n then put 1 into column P set n := n - 2^p Else put 0 into column P End if Subtract 1...
Statement: For a given integer N, print all the squares of positive integers where the square...
Statement: For a given integer N, print all the squares of positive integers where the square is less than or equal to N, in ascending order. Programming Tasks: Prompt the user to input the value of N Output to the screen all squares of positive integers <= N Tests: Item Test 1 Test 2 Test 3 Inputs: 50 9 100 Outputs: 1 4 9 16 25 36 49 1 4 9 1 4 9 16 25 36 49 64 81...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT