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).
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 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.
in C++ Description: For a given integer n > 1, the smallest integer d > 1...
in C++ Description: For a given integer n > 1, the smallest integer d > 1 that divides n is a prime factor. We can find the prime factorization of n if we find d and then replace n by the quotient of n divided by d, repeating this until n becomes 1.             Write a program that determines the prime factorization of n in this manner, but that displays the prime factors in descending order. (When you find a...
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,...
Write a Scheme function that takes a positive integer n and returns the list of all...
Write a Scheme function that takes a positive integer n and returns the list of all first n positive integers in decreasing order: (decreasing-numbers 10) (10 9 8 7 6 5 4 3 2 1) Please explain every step
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],...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT