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
Num = int(input("Enter a number: "))
Pavlova-9 [17]

So, 5 is intered.

Line 1: num will recieve the integer 5

Line 2: Blank

Line 3: num will recieve 1, wich is the remainer of the division of 5 by 4.

Line 4: will return true, since num is indeed equal to 1.

Line 5: Will print "A"

Line 6: Will return false

Line 7: Gets jumped

Line 8: Will return false

Line 9: Gets jumped

Line 10: Will return false

Line 11: Gets jumped

Line 12: Will return false

Line 13: Gets jumped

The only OUTPUT is "A".

5 0
3 years ago
Write a complete program that declares an integer variable, reads a value from the keyboard into that variable, and writes to st
vovikov84 [41]

Answer:

#include <iostream>

using namespace std;

int main ()

{

int num;

cin>>num;

cout << num<< " " << 2 num << " " << numnum;

return 0;

}

Explanation:

See answer

5 0
3 years ago
5
Vilka [71]
Is there like a graph or something I can look at because then I can help you
5 0
3 years ago
Where in PowerPoint should a user navigate to complete the tasks listed below?
lana [24]

Answer:

Quick Access toolbar

Ribbon

Quick Access toolbar

Ribbon

Explanation:

edge2021

7 0
3 years ago
Write a program to calculate the area of a triangle whose base is 10cm and height is 6cm
Tcecarenko [31]

Answer:

30cm

Explanation:

Height X Base

6cm X 10cm

60cm

Then take half because a triangle is half a square and we are using a square formula.

60cm divided by 2

=30cm

7 0
3 years ago
Other questions:
  • Where is the typical location of a touchpad inside of a laptop?
    14·1 answer
  • Which element of the word program window contains buttons for saving a document and for undoing, redoing, and repeating a change
    5·1 answer
  • For this exercise, you are given a phrase. Return the number of time the word “dog” appears in the phrase.
    7·1 answer
  • A device has an IP address of 10.1.10.186 and a subnet mask of 255.255.255.0. What is true about the network on which this devic
    9·1 answer
  • A USB is used for _________.[ pick the best answer that makes sense]
    14·2 answers
  • Does anyone know to get Google updated if it failed to update?
    12·1 answer
  • Examine the following algorithm.
    9·1 answer
  • The light source for a typical silhouette is generally___________ the subject.
    12·2 answers
  • A pizza delivery restaurant decides to stop hiring drivers and start hiring cyclers to deliver its pizza. The restaurant thinks
    15·1 answer
  • PLZ HELP!! How does HTML help solve the problem of telling a computer what a web page looks like, not just the content that is o
    15·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!