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 are the major functions of a computer
murzikaleks [220]
A computer is a device whose main function is to process large amounts of information quickly and accurately, and this procedure is done thanks to hardware and software.
7 0
3 years ago
A(n) ________ is a chart based on PivotTable data.
Mrrafil [7]
Well since it’s a chart based on a PivotTable prettyyyy sure it’s gonna be a PibltChart
7 0
2 years ago
What term describes the amount you must pay in order to be considered “up-to-date” with your credit card payments?
yuradex [85]

Answer:

Minimum payment.

Explanation:

A loan can be defined as an amount of money that is being borrowed from a lender and it is expected to be paid back at an agreed date with interest.

Generally, the financial institution such as a bank lending out the sum of money usually requires that borrower provides a collateral which would be taken over in the event that the borrower defaults (fails) in the repayment of the loan.

A credit score can be defined as a numerical expression between 300 - 850 that represents an individual's financial history and credit worthiness. Therefore, a credit score determines the ability of a borrower to obtain a loan from a lender.

This ultimately implies that, the higher your credit score, the higher and better it is to obtain a loan from a potential lender. A credit score ranging from 670 to 739 is considered to be a good credit score while a credit score of 740 to 799 is better and a credit score of 800 to 850 is considered to be excellent.

Additionally, credit card can be defined as a small rectangular-shaped plastic card issued by a financial institution to its customers, which typically allows them to purchase goods and services on credit based on the agreement that the amount would be paid later with an agreed upon interest rate.

Minimum payment is a term which describes the amount you must pay in order to be considered “up-to-date” with your credit card payments. A credit card holder that meets the minimum payment and has a good credit score is eligible for more credits and loans.

4 0
2 years ago
Characteristics of first generation computers​
Marrrta [24]
* Main Electron a component- vacuum tube.

* Main memory- Magnetic drums and magnetic tapes.

* Programming language- Machine language.

* Power- Consume a lot of electricity and generate a lot of heat.

* were expensive to operate
5 0
3 years ago
is skill in using productivity software, such as word processors, spreadsheets, database management systems, and presentation so
Digiron [165]

Answer:

It is general knowledge

Explanation:

What you covered is general knowledge and the entrance to computer science.

4 0
3 years ago
Other questions:
  • At the coffee shop where they work, Jen is the safety officer, Richard is in charge of administering first aid, Ashley is respon
    13·2 answers
  • What are the two ways that assets are
    5·1 answer
  • Which term refers to the blank areas surrounding a document page? *
    15·1 answer
  • A. Calculate the diffusion coefficient for magnesium in aluminum at 450°C.
    14·1 answer
  • Each row in a database is a set of unique information called a(n)
    8·2 answers
  • Which of the following is not part of the four ways you can avoid problems with email communication?
    12·1 answer
  • How are Earth's plates made?
    14·1 answer
  • What is the value of numX when this program is executed? if 3 &lt; 5 and 8 != 3: numX = 3 else: numX = 7
    13·2 answers
  • BADM-Provide a reflection of at least 500 words (or 2 pages double spaced) of how the knowledge, skills, or theories of this cou
    7·1 answer
  • Which pickaxe in minecraft to use?
    14·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!