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
What is Server Message Block (SMB) used for in Windows and can a hacker still damage a network using SMB?
Aleksandr [31]

Answer:

The absolute most decimating ransomware and Trojan malware variations rely upon vulnerabilities in the Windows Server Message Block (SMB) to proliferate through an association's system. Windows SMB is a convention utilized by PCs for record and printer sharing, just as for access to remote administrations.  

A fix was discharged by Microsoft for SMB vulnerabilities in March 2017, yet numerous associations and home clients have still not applied it. So now, the unpatched frameworks permit dangers that exploit these vulnerabilities inside, helping dynamic malware crusades spread like Californian rapidly spreading fire.  

SMB vulnerabilities have been so effective for danger entertainers that they've been utilized in the absolute most unmistakable ransomware episodes and refined Trojan assaults of the most recent two years. Truth be told, our item telemetry has recorded 5,315 discoveries of Emotet and 6,222 of TrickBot in business systems—two Trojan variations that are utilizing the SMB vulnerabilities—over the most recent 30 days alone.

7 0
3 years ago
Read 2 more answers
Beside homework, what products can you use for personal use? Identify three uses and briefly describe.
Dominik [7]
<span>Beside homework, what products can you use for personal use? Identify three uses and briefly describe.

</span>
3 0
3 years ago
Read 2 more answers
What can help prevent issues related to downloading content from the internet?
vodka [1.7K]

Answer:

D)

Explanation:

3 0
2 years ago
Jose has 3/5 kilogram of peppermints and 2/3 kilogram of candy canes. How many kilograms of candy does he have?
yarga [219]

Answer:

\frac{19}{15}\\

≅ 1.267

Explanation:

\frac{3}{5} +\frac{2}{3} \\\\= \frac{9}{15} + \frac{10}{15} \\\\= \frac{9+10}{15} \\\\= \frac{19}{15} \\\\= 1.267

6 0
2 years ago
Jane wants to add a chart to her presentation so she’ll click the Insert tab and in the Images group, she’ll click the Chart but
kap26 [50]

(A. Stevie only) because You need to go the illustrations tab in order to click on the chart button.

Jane clicked on images tab so she was wrong.

Mark as brainlest plz :)

3 0
3 years ago
Other questions:
  • Dr. Joon asks her students how to insert a table in an Excel workbook. The students record the steps a chart. Which students lis
    15·1 answer
  • What is a taskbar?
    5·1 answer
  • Sometimes you're the dog, sometimes the fire hydrant, your thoughts?
    15·1 answer
  • What are some (free) good animation websites (for PC/Microsoft) for a short informational video about homelessness?
    15·1 answer
  • Which is a sentence fragment?
    6·1 answer
  • What are the different steps while solving a problem using computer? explain​
    7·1 answer
  • You are creating a story map about Mexico. After configuring the web app template, you launch the app to test it. When the app o
    13·1 answer
  • Write a one page report describing the computer the client used, who else had access to it and other relevant findings. Referenc
    7·1 answer
  • What are the inputs that the model uses to make decisions?
    7·1 answer
  • What are the importance of computer software​
    9·2 answers
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!