Question

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 and output should match the sample run.

Please try and restrict yourself to loops, selection statements, and calls to library methods

Sample Run:

Enter the upper limit: 25

The prime numbers are:

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

0 0
Add a comment Improve this question Transcribed image text
Answer #1
//Primes.java
import java.util.Scanner;
public class Primes {
    private static boolean isPrime(int n) {
        for (int i = 2; i < n; i++) {
            if (n % i == 0) {
                return false;
            }
        }
        return true;
    }

    public static void main(String args[]) {
        int n;
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter a number: ");
        n = scanner.nextInt();

        System.out.println("The prime numbers are:");
        for(int i = 2;i<=n;i++){
            if(isPrime(i)){
                System.out.print(i+" ");
            }
        }
        System.out.println();
    }
}
Add a comment
Know the answer?
Add Answer to:
Write a java program to print all the prime numbers below a certain given number. A...
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 powers of 2 below a certain number and...

    Write a java program to print all the powers of 2 below a certain number and calculate their sum Requirements: 1. Accept the upper limit from the user as an integer 2. Make sure the sum variable is of the “long” type. You can assume that the test output will be less than LONG MAX. 3. Print all the numbers as a running sum, and finally print their sum. Please try and restrict yourself to loops, selection statements, and calls...

  • write a program in java to print all the powers of 2 below a certain number...

    write a program in java to print all the powers of 2 below a certain number and calculate their sum. Make sure your program conforms to the following requirements. 1. Accept the upper limit from the user (as an integer). 2. These numbers grow very fast. make sure the sum variable is of the "long" type. You can assume that the test output will be less that LONG MAX. 3.Print all numbers as a running sum, and finally print their...

  • 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 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.

  • Your program  write a program to test whether a number is prime or not. Your program should...

    Your program  write a program to test whether a number is prime or not. Your program should ask user to input an integer and respond: "Prime" or "Not prime". Don't use any library function that tests for prime numbers. The program should continue to ask for input, till it sees a 0, when it exits. Please submit printed pseudocodeshould ask user to input an integer and respond: "Prime" or "Not prime". Don't use any library function that tests for prime numbers....

  • Write a C program which computes the count of the prime and perfect numbers within a...

    Write a C program which computes the count of the prime and perfect numbers within a given limit Land U, representing the lower and upper limit respectively. Prime numbers are numbers that have only 2 factors: 1 and themselves (1 is not prime). An integer number is said to be perfect number if its factors, including 1 (but not the number itself), sum to the number. Sample Inputi: 1 100 Sample Outputs: Prime: 25 Perfect: 2 Sample Input2: 101 10000...

  • JAVA please! Prime numbers are interesting numbers. A prime number is one that is only divisible...

    JAVA please! Prime numbers are interesting numbers. A prime number is one that is only divisible by 1 and itself. For hundreds of years mathematicians have looked for the largest prime number. In the year 1456 the largest known prime was 8191. By the year 1588 it was a 6-digit number: 131,071. The famous mathematician Leonhard Euler discovered the 10-digit number 2,147,483,647 in 1772. Over the years larger and larger primes have been found. There are contests to find the...

  • Write a Java program to convert octal (integer) numbers into their decimal number equivalents (exactly the...

    Write a Java program to convert octal (integer) numbers into their decimal number equivalents (exactly the opposite of what you have done in Assignment 4). Make sure that you create a new Project and Java class file for this assignment. Your file should be named “Main.java”. You can read about octal-to-decimal number conversions on wikepedia Assignment 4 is at the bottom Objective Your program should prompt the user to enter a number of no greater than 8 digits. If the...

  • Use C programming Make sure everything works well only upload Write a program that takes an...

    Use C programming Make sure everything works well only upload Write a program that takes an integer from standard input and prints all prime numbers that are smaller than it to standard output. Recall that a prime number is a natural number greater than 1 whose only positive divisors are 1 and itself. At the start of the program, prompt the user to input a number by printing "Input a number greater than 2:\n". If the user's input is less...

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