Question: Beginner Java Programming - scanner, methods, boolean, for loops, if/else loops. I would like to complete a program that displays the first N prime numbers.
Beginner Java Programming - scanner, methods, boolean, for loops, if/else loops.
I would like to complete a program that displays the first "N" prime numbers. In other words, I'd like my program to list the first "N" prime numbers a user inputs. It should look like this:
Welcome to the list of N prime numbers program! =============================================== Please enter the value of N (positive integer): 6
First 6 prime numbers are: 2 3 5 7 11 13
------------------------Here is my current code and I am getting nowhere:-------------------------------------
import java.util.Scanner;
public class Assignment
{
public static void main(String[] args)
{
System.out.println("Welcome to the list of N prime numbers program!");
equalSigns(52);
System.out.println(" Please enter the value of N (positive integer):");
Scanner input=new Scanner(System.in);
int positiveInt=input.nextInt();
if (positiveInt >= 1)
{
System.out.println("The first " + positiveInt + " prime numbers are:");
}
int count;
for (count = 1; count<= positiveInt; count++ )
{
System.out.println(count);
}
}
public static boolean isPrime(int number)
{
Scanner input=new Scanner(System.in);
int positiveInt=input.nextInt();
positiveInt = number;
int upperLimit;
if(number <2)
return false;
upperLimit = (number/2);
System.out.println("primes:" + number);
for(int i =2; i <=upperLimit; i++)
{
if(number%i ==0)
{
return false;
}
}
return true;
}
public static void equalSigns(int n)
{
for( int i=1; i<=n; i++)
{
System.out.print("=");
}
}
}
Please help ! Thank you.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
