Can someone please get back to me quickly on this problem, I have tried numerous
ID: 3565889 • Letter: C
Question
Can someone please get back to me quickly on this problem, I have tried numerous solutions but can not get it to run.
Purpose: Use nested iteration statements and multiple Classes in java.
Problem: Write a program that prompts the user for an integer and then prints out that many prime numbers.
For example, when the user enters 5, the program should print
2
3
5
7
11
Recall that a number is a prime number if it is not divisible by any number except 1 and itself.
Supply a class PrimeGenerator with a method nextPrime. Name your tester class (the class containing
main) PrimeTester. PrimeTester will input the number, invoke PrimeGenerator and nextPrime to get
the next prime, then print each prime number.
Explanation / Answer
import java.util.*;
class PrimeTester
{
public static void main(String args[])
{
int n, status = 1, num = 3;
Scanner in = new Scanner(System.in);
System.out.println("Enter the number of prime numbers you want");
n = in.nextInt();
PrimeGenerator inpnumber = new PrimeGenerator();
inpnumber.nextPrime(n);
}
}
Class PrimeGenerator {
void nextPrime (int n)
if (n >= 1)
{
System.out.println("First "+n+" prime numbers are :-");
System.out.println(2);
}
for ( int count = 2 ; count <=n ; )
{
for ( int j = 2 ; j <= Math.sqrt(num) ; j++ )
{
if ( num%j == 0 )
{
status = 0;
break;
}
}
if ( status != 0 )
{
System.out.println(num);
count++;
}
status = 1;
num++;
}
}
}