Question

JAVA LANGUAGE!!!! Make a program that displays n prime numbers. n is a user input.   ...

JAVA LANGUAGE!!!!

Make a program that displays n prime numbers. n is a user input.   

For example,

If n is 7, the program displays as follows.

Enter a positive integer: 7

2, 3, 5, 7, 11, 13, 17

0 0
Add a comment Improve this question Transcribed image text
Answer #1
//Main.java
import java.util.Scanner;
public class Main{
  public static void main(String[] args){
    Scanner scanner = new Scanner(System.in);
    System.out.print("Enter a positive integer: ");
    int n = scanner.nextInt();

    if(n>=1){
      System.out.print(2);
      boolean flag = false;
      for(int i = 3;n>=2;i++){
        flag = false;
        for(int j = 2;j<i-1;j++){
          if(i%j == 0){
            flag = true;
          }
        }
        if(!flag){
          System.out.print(", "+i);
          n--;
        }
      }

    }

  }
}


Enter a positive integer: 7
2, 3, 5, 7, 11, 13, 17

Add a comment
Know the answer?
Add Answer to:
JAVA LANGUAGE!!!! Make a program that displays n prime numbers. n is a user input.   ...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a java program to print all the prime numbers below a certain given number. A...

    Write a java program to print all the prime numbers below a certain given number. A prime number is defined as a number that can only be divided by 1 and itself. Requirements: 1. Accept the upper limit from the user as an integer. 2. You can assume the user input will be positive and smaller than INT MAX 3. Go from 1 to the number. If you happen to find a number that is prime, print it. The input...

  • in java the program must contain the main method and example( the number 50 should be...

    in java the program must contain the main method and example( the number 50 should be the input) must be included in the main method to check the correctness of your programs. Create a GUI program to let user enter a positive integer n through the first window, and then your program prints out all the prime integers within the range [1, n) through the second window. Your program should contain two windows in total. Below are two example pictures:...

  • I must create a Loop in C++ that will print all prime numbers less than the...

    I must create a Loop in C++ that will print all prime numbers less than the number entertered by the user. 1. Accept the upper limit from the user (as an integer). (5 points) 2. Make sure the number is positive. If it is not, terminate the program. (5 points). 3. Go from 1 to the number. If you happen to find a number that is prime, print it. (35 points) example: Enter the upper limit: 25 The prime numbers...

  • Write a java program that will print if n numbers that the user will input are...

    Write a java program that will print if n numbers that the user will input are or not within a range of numbers. For that, your program needs to ask first for an integer number called N that will represent the number of times that will ask for other integer numbers. Right after, it should ask for two numbers that will represent the Min and Max for a range. Lastly. it will iterate N number times asking for an integer...

  • Write a C program that takes a positive integer n as input and prints all the...

    Write a C program that takes a positive integer n as input and prints all the prime numbers from 1 to n. Sample Input/Output 1: Enter your n: 20 Prime number(s) from 1 to 20 : 2 3 5 7 11 13 17 19 Sample Input/Output 2: Enter your n:2Prime number(s) from 1 to 2 : 2

  • In C program #include<stdio.h> The first 11 prime integers are 2, 3, 5, 7, 11, 13,...

    In C program #include<stdio.h> The first 11 prime integers are 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, and 31. A positive integer between 1 and 1000 (inclusive), other than the first 11 prime integers, is prime if it is not divisible by 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, and 31. Write a program that prompts the user to enter a positive integer between 1 and 1000 (inclusive) and that outputs whether the number...

  • Write a program that takes a positive integer n as input from the user and displays...

    Write a program that takes a positive integer n as input from the user and displays an n by n checkerboard. (Hint: your main method could do user input then call the constructor for your GUI with n as the argument.) For example, when n = 5, there are 25 alternating white and black squares inside a square boarder. The lower right corner of the overall pattern must be white. Make sure that adjacent squares are different colors regardless of...

  • In this exercise, write a complete Java program that reads integer numbers from the user until...

    In this exercise, write a complete Java program that reads integer numbers from the user until a negative value is entered. It should then output the average of the numbers, not including the negative number. If no non-negative values are entered, the program should issue an error message. You are required to use a do-while loop to solve this problem. Solutions that do not use a do-while loop will be given a zero. Make sure to add appropriate comments to...

  • (prime.cpp) A prime number is a number that cannot be formed by multiplying two smaller numbers...

    (prime.cpp) A prime number is a number that cannot be formed by multiplying two smaller numbers (not including 1). For example, 2, 3, 5 are prime numbers but 4 (2*2) and 6 (2*3) are not. Write a C++ program that receives the start point and end point from user and displays all the prime numbers in this range. The program also receives how many numbers to display per row. No need to validate. Print the results in a tabular format....

  • Write a multithreaded a Java program that outputs prime numbers. This program should work as follows:...

    Write a multithreaded a Java program that outputs prime numbers. This program should work as follows: The user will run the program and will enter a number on the command line, The program will then create a separate thread that outputs all the prime numbers less than or equal to the number entered by the user.

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT