+ 2
Wap to print prime numbers between given range?
for example range from 1 to 100 . i.e prime numbers between 1 to 100. NOTE range is taken as input from user it can be any range. programming language Java.
7 Answers
+ 2
Please write a more specific question, and then other people will be able to answer you, also you need to specify the programming language for which people will search for the answer
+ 2
@DâŹâAnand Gutte
I've already typed up a solution for you and have it waiting on standby.
As I said in my original reply, what have you tried so far? Since I won't be taking the interview for you, it's worth at least making an attempt at solving your issue first since you'll have to explain/answer questions from the potential employer. See what I mean? If you fail in your attempt, I'll help you fix it and help you understand why it didn't work as you thought.
First thing I would do is simply map out the flow:
- Import our Scanner class for our input
- Declare our scanner object
- Create number variables for startingRange and endingRange (for our input)
- Prompt user for the input for both
- Use a loop to go through your range and check if it's prime or not.
- Print it if it's prime.
+ 1
What have you tried so far? More than happy to help you correct your code.
+ 1
Alexander Sokolov why u dont understand a question? Its as simple as that. just print prime numbers between given range.. thats it.
+ 1
@DâŹâAnand Gutte
If you post it up within the next hour, I can help you out today. If not, I'll be signing off until tomorrow.
+ 1
Here is the code that I typed up for you yesterday. As mentioned, if you're needing this for an interview, then good luck to you if you're not willing to learn what you needed. They'll make you explain it and test how well you understand what you're saying you know how to do. Considering you weren't even willing to make an attempt on it yourself, in order to help yourself, then good luck being a programmer in the corporate world. Companies are good at weeding people out, especially since you'll probably be working on important stuff for clients and/or stuff that's significantly more complicated than basic math. As well, they'll more than likely further test you beyond something like this, especially once you're there in person.
Either way, I wish you the best with what you're trying to do and I hope it works out smoothly.
https://code.sololearn.com/cHeB1zdB8ge1/#java
import java.util.Scanner;
public class Program
{
public static boolean getPrime(int number){
for (int i = 2; i <= number / 2; ++i) {
if(number % i == 0)
return false;
}
return true;
}
public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);
int startRange = 0;
int endRange = 0;
System.out.println("Input starting range: ");
startRange = scnr.nextInt();
System.out.println("Input ending range: ");
endRange = scnr.nextInt();
for( int i = startRange; i <= endRange; ++i){
if(getPrime(i)){
System.out.println(i);
}
}
}
}
:::::: OUTPUT ::::::::
Input starting range:
Input ending range:
1
2
3
5
7
11
13
17
19
23
29
31
37
41
43
47
53
59
61
67
71
73
79
83
89
97
0
Thank you Fata1 Err0r đ€