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
All of the following are ways to improve the mobile experience for a website visitor EXCEPT:
Jobisdone [24]

Answer:

Adding more links to the page.

Explanation:

User experience design, UED, is a concept or a process of software development application life cycle that graphically presents a clients project to reflect the needs, want and interactivity of the users.

There are certain principles that governs the design of user interactive web and mobile interfaces. A user interface has to be simple, easy to interact with, give a good feel, signing into email, phone, laptop, mobile app etc, should also be made easy.

Covering the screen with link, makes it difficult to navigate without triggering a link.

3 0
3 years ago
Jennifer has written a business report. What should be her last step before she submits the story for publication?
Montano1993 [528]
Revising and editing because it makes the report look sharp and business like.
5 0
3 years ago
Read 2 more answers
What does FIFO mean?
zubka84 [21]
Hello there!

It is First In First Out

Hope This Helps You!
Good Luck :)
6 0
2 years ago
Read 2 more answers
rite a function to prompt the user to enter the information for one movie (title, release date, mpaa rating and number of stars)
vesna_86 [32]

Answer:

import java.util.*;

public class Movie {

   

   public static void main (String [] args) {

       

       System.out.println(movieInfo());

       

   }

   public static String movieInfo() {

       Scanner input = new Scanner(System.in);

       Scanner input2 = new Scanner(System.in);

       

       System.out.print("Enter the title: ");

       String title = input.nextLine();

       System.out.print("Enter the release date: ");

       String releaseDate = input.nextLine();

       System.out.print("Enter the mpaa rating: ");

       double mpaaRating = input2.nextDouble();

       System.out.print("Enter the number of stars: ");

       double numberOfStars = input2.nextDouble();

       

       return "Title: "+title+ "\nRelease Date: "+releaseDate+ "\nMPAA Rating: "+ mpaaRating+ "\nNumber of Stars: "+numberOfStars;

   }

}

Explanation

- Create a function called <em>movieInfo()</em> that returns a string, information about a movie

- Ask the user to enter the title, release date, mpaa rating and number of stars of the movie.

- Return these values at the end of the function

- Inside the main, call the function to see the information about the movie

7 0
3 years ago
_____ are systems of electronic components that generate, modify, and control sound.
expeople1 [14]
<span>Synthesizers. The term synthesizer is derived from the word synthesis which means to combine various elements to create something new. Instruments such as the trumpet, guitar, drums, etc. are molded and shaped to make a specific sound. With the use of electronic synthesizers sounds can be combined or modified to create a unique, new sound.</span>
4 0
3 years ago
Other questions:
  • Define a function group-by-nondecreasing, which takes in a stream of numbers and outputs a stream of lists, which overall has th
    12·1 answer
  • Suppose company A wants to develop a program that duplicates the functionality of a programm by company B. Describe how company
    8·1 answer
  • Does a BIOS reset erase data such as pictures, programs or other things like that?
    13·1 answer
  • If you’re presenting in a darkened room, what text and background should you use to provide maximum readability?1) Light text on
    5·1 answer
  • A(n) ________ moves over the spinning platters to retrieve data from the hard disk.
    6·1 answer
  • HELP 99PTS If Answered
    12·2 answers
  • what term defines inventiveness uncertainty and futuristic ideas typically deals with science and technology
    15·1 answer
  • in a famous experiment a dog drool at a sound that is made just before meals. This is considered a(n)-------------------- respon
    14·1 answer
  • Which statement is true about encoding in Python?
    7·1 answer
  • In a database, what term is used to describe a group of fields that are all associated with and accessed using single primary ke
    8·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!