1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
andrew11 [14]
3 years ago
15

A prime number is a number that is divisible only by itself and 1. Write a program that asks a user for an integer value and the

n displays all prime numbers less than or equal to that number. For example, if the user enters 17, the program should display: Prime numbers less than or equal to 17:
Computers and Technology
1 answer:
Rashid [163]3 years ago
8 0

Answer:

The Solution Code is written in Java.

  1. public class Main {
  2.    public static void main(String[] args) {
  3.        System.out.print("Please enter an integer: ");
  4.        Scanner input = new Scanner(System.in);
  5.        int number = input.nextInt();
  6.        System.out.print("Prime numbers less than or equal to " + number + " : ");
  7.        for(int i=2; i <= number; i++){
  8.            if(checkPrime(i)){
  9.                System.out.print(i + " ");
  10.            }
  11.        }
  12.    }
  13.    public static Boolean checkPrime(int num){
  14.        for(int i=2; i < num; i++)
  15.        {
  16.            if(num % i == 0){
  17.                return false;
  18.            }
  19.        }
  20.        return true;
  21.    }
  22. }

Explanation:

Firstly, we create a function to check if a number is prime (Line 18 - 27).

  • This function will take one input value which is an integer, num.
  • To check if the input num is a prime, we can use modulus operator, %, to confirm if the num is divisible by any number starting from 2 to num - 1 (Line 19 - 24).
  • If the num is divisible by any number expect 1 and itself, it should equal to zero and return false to indicate it is not a prime number.
  • If all the num (except 1 and itself) is not divisible, the function will return true (Line 25).

Next, in our main program part (Line 3 - 16)

  • Prompt the user to input a number (Line 5 - 7)
  • Using a for-loop, we can keep calling the checkPrime() function by passing a current number (starting from 2 to input number) as argument (Line 12). The checkPrime() function will run and return true it the current number is prime, return false if it is not prime.
  • If the checkPrime() function return true, it will print the current number before proceed to the iteration of the for-loop to repeat the same check prime procedure (Line 13)
  • At the end all the prime numbers less than or equal to the input number will be printed out in the terminal
You might be interested in
A town decides to publicize data it has collected about electricity usage around the city. The data is freely available for all
tigry1 [53]
The answer is C.open data
8 0
3 years ago
In which scenario would instant messaging be more useful than other forms of communication?
ivann1987 [24]
When you're busy doing things and/or be too sick for over the phone communication
7 0
3 years ago
____ map a set of alphanumeric characters and special symbols to a sequence of numeric values that a computer can process.
Neporo4naja [7]
The answer is : coding schemes
4 0
3 years ago
Assume you are part of the systems development team at a medium-sized organization. You have just completed the database design
nexus9112 [7]

Answer:

Please see the attached file for the complete answer.

Explanation:

Download pdf
5 0
3 years ago
29
denis23 [38]

The correct answer is A it transfers control to the next loop in the program.


The most valid answer is that the break statement Exits the loop and continues executing the program, but assuming that there are other loops, the control of the program will go to them since the first loop is broken out of.


3 0
3 years ago
Other questions:
  • What two images did you have to add to the wds server to handle the windows installation?
    7·1 answer
  • You want to place a video from the internet to your desktop. what process do you use?
    15·1 answer
  • Cloud kicks recently completed the implementation of sales cloud. Cloud kicks has trained the users to use the mobile app to acc
    12·1 answer
  • A class defined by a user (i.e., you, the programmer programmer) ... Group of answer choices might contain no data members -- on
    13·1 answer
  • Can you make copies of classified documents provided you alert others?
    6·1 answer
  • Word can store a maximum of how many documents in its Recent Documents area? five ten fifteen twenty
    8·1 answer
  • The engineering firm you work for is submitting a proposal to create an amphitheater at a state park. Proposals must be submitte
    15·1 answer
  • Does -8 = -21?<br>[this is NOT a trick question]<br>Yes....?<br>No!​
    12·2 answers
  • Who invented the Bluetooth device​
    12·1 answer
  • The __________ statement allows you to check for
    7·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!