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 user informs you that he or she never deletes any files he or she creates. How might this affect the operating system? It will
Juliette [100K]

Answer:

Depending on the amount of files there are, the operating system will run slower because these files take up space on the hard drive, slowing it down.

Explanation:

6 0
3 years ago
Help Pls<br>I need about 5 advantages of E-learning​
AnnyKZ [126]

Answer:

Explanation:

E-learning saves time and money. With online learning, your learners can access content anywhere and anytime. ...

E-learning leads to better retention. ...

E-learning is consistent. ...

E-learning is scalable. ...

E-learning offers personalization.

7 0
3 years ago
Read 2 more answers
What movie would be greatly improved if it was made into a musical?
Lina20 [59]
Sixteen candles would be even better if it was a musical
3 0
3 years ago
I'm looking for a new laptop for school. Which laptop would be the best. Right now I have a chromebook and it is broken. 
Y_Kistochka [10]
Any HP laptop would be good! What is your price range?
Also, if it is for school, have you considered getting an iPad? It is great for taking notes and looking stuff up and being portable. My friends currently use it
3 0
3 years ago
Read 2 more answers
1. The advancement of media and information bring society countless opportunities such<br>as...​
rjkz [21]

Answer:

The advancement of media and information bring society countless opportunities such as quick access to information that helps people during their daily lives and problems.

Thus, for example, nowadays the use of the internet and social networks allows individuals very fast access to different methods of solving everyday problems: a clear example is the tutorial videos where it is explained how to carry out a certain activity or arrangement of a break on a specific asset, providing users with a quick solution to their daily problems.

5 0
3 years ago
Other questions:
  • Microsoft Paint can be described as a digital sketch pad ?
    8·1 answer
  • Which programming language is good for 14 year old like me​
    12·2 answers
  • Which data type requires the greatest number of bytes in computer memory? A. character B. integer C. single-precision number D.
    14·1 answer
  • Print a countdown from n to 1 The function below takes one parameter: an integer (begin). Complete the function so that it print
    8·1 answer
  • I think these might be the answers but im not to sure, so am I right? or wrong? please help
    11·2 answers
  • Your friend wants to purchase a new hard drive. She wants a drive that has fast access because she will use it to edit videos on
    8·1 answer
  • What is the danger of open-source software when it comes to military robotics?
    13·1 answer
  • What messages do we get about ourselves from rap and hip hop
    7·1 answer
  • I have no idea what I’m doing and this is due in 45 minutes
    7·1 answer
  • Low-level formatting ____. Group of answer choices is different from physical formatting is usually performed by the purchaser o
    11·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!