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
Viefleur [7K]
3 years ago
9

Project 5: Prime Numbers. Write a program that reads in an integer that is greater than 2 (let's call it k) and finds and prints

all of the prime numbers between 3 and k. A prime number is a number such that 1 and itself are the only numbers that evenly divide it (for example, 3, 5, 7, 11, 13, 17, ...). One way to solve this problem is to use a doubly nested loop. The outer loop can iterate from 3 to k while the inner loop checks to see if the counter value for the outer loop is prime. One way to see if number n is prime is to loop from 2 to n-1 and if any of these numbers evenly divides n, then n cannot be prime. If none of the values from 2 to n-1 evenly divides !n, then n must be prime. (Note that there are several easy ways to make this algorithm more efficient.)
Computers and Technology
2 answers:
Blizzard [7]3 years ago
6 0

Answer:

#section 1

def is_prime_v1(n):

   for d in range(2, n):

       if n % d == 0:

           return False

   return True

#section 2

while True:

   try:

       num = int(input('Enter a number greater than 2: '))

       if num > 2:

           break

       else:

           raise

   except:

       print('{} is not greater than 2'.format(num))

#section 3

for n in range(3, num):

   is_prime_v1(n)

   a=is_prime_v1(n)

   if a==True:

       print (n)

Explanation:

The programming language used is Python 3.

#section 1

In this section we create a function that will check if a number is a prime number. The function returns True for prime numbers and False for normal numbers.

#section 2

In this section we make use of a WHILE loop and a try and except block to ensure that the input by the user is greater than two, if the input is less than 2, an exception is raised and the except block prompts the user to enter the appropriate value.

when the appropriate value is entered the loop breaks and the value is carried to the last section.

#section 3

In this final section, the value collected from the user in #section 2, is inserted as the upper-limit in a range function with the lower limit set at 3.

The function created in #section 1 is called and each value in the range is passed to it and an IF statement checks if the output is True or False. If the IF block evaluated to be True, it implies that the number is a prime number and the result is printed to the screen.

alexandr1967 [171]3 years ago
5 0

Answer:

#include <iostream>

using namespace std;

int main() {

   int k;

   cin>>k;

   cout<<"2 "; //printing the first prime number.

   for(int i=3;i<=k;i++)

   {

       int divisor=2;//divisor to check the prime numbers.

       while(divisor<i)

       {

           if(i%divisor==0)//if i is divisible by divisor then it is not prime.

           {

               break;

           }

           divisor++;

       }

       if(divisor==i)

       {

           cout<<divisor<<" ";

       }

   }

return 0;

}

Input:-

24

Output:-

2 3 5 7 11 13 17 19 23

Explanation:

The above-written code is in c++.It prints all the prime numbers upto the integer entered k.To achieve this I have nested loops for and while.For loop runs over 3 to n because 2 is a prime number so we are printing it beforehand.For every value of the for loop.It will be checked it is prime or not.If it is prime then the integer is printed else it is not printed.

You might be interested in
PLEASE HELP PROGRAMMING WILL GIVE BRAINLIEST
DaniilM [7]

Answer:

1. would be B.

2. would be D

3. May I ask if it can be multiple answers?

Explanation:

1. In coding a if statement has to follow if something is true or not. If we are talking about a if and then statement it will skip that block and move to the next block of the "then" part of the "if then" Statement if the first part is reported false

2. This is a easy one, so it has to be either a 3.25 GPA or higher or they have to have either 100 hours or more.

3. waiting for a comment back :)

4 0
4 years ago
What is the importance of using Onedrive in Windows 10 and how knowledge of it will have an impact in today's workplace?
SashulF [63]

The importance of one drive in windows 10 is that it helps the user to synchronize files in their computer.

Onedrive is a cloud storage system that is useful for the storage of files in a secured manner. A person can easily access their files whenever they want to.

In todays workplace a knowledge of onedrive has a great impact because

  • Onedrive offers an unlimited access to files whenever they are needed
  • The files can be available and accessed from anywhere
  • It helps with the organization of files in the work place.

One drive allows for this to be done even when offline. When online, there is an automatic synchronization of the made changes.

Read more at brainly.com/question/17163678?referrer=searchResults

7 0
3 years ago
Read 2 more answers
Best free vpn to bypass geo tagging
Mamont248 [21]

Answer:

Nord VPN

Explanation:

5 0
4 years ago
A light or optical microscope most commonly used is called a(n) _____. bright-field microscope dark-field microscope electron mi
puteri [66]
I think a bright field is most commonly used
5 0
4 years ago
Read 2 more answers
JAVA
musickatia [10]

Answer:

class Main {  

 public static void main(String args[]) {

   int a = 5;

   int delta = 5;

   for(int i=0; i<10; i++) {

       System.out.printf("%d, ", a);

       a += delta;

       delta += 2;

   }

 }

}

6 0
3 years ago
Other questions:
  • What does the term Gestalt mean?<br> A image<br> B graph<br> C big<br> D part <br> E whole
    11·1 answer
  • All phones must have a way of making and then terminating a connection to the phone network; however the exact procedure for doi
    6·1 answer
  • What is an advantage of sharing documents in PDF format instead of Word format?
    6·2 answers
  • What will be the values of ans, x, and y after the following statements are executed? int ans = 35, x = 50, y =50; if ( x &gt;=
    7·2 answers
  • Why should you avoid the use of sarcasm, clichés, and idioms in business letters?
    11·2 answers
  • Which of the following are breach prevention best practices?Access only the minimum amount of PHI/personally identifiable inform
    11·1 answer
  • Program: ASCII art (Python 3)1) Output this tree. 2) Below the tree (with two blank lines), output this cat. (3 pts) /\ /\ o o =
    8·1 answer
  • Which programming language represents data in the form of a series of zeros and ones​
    7·1 answer
  • What is the purpose of using variables in programming?
    11·1 answer
  • Give two examples of situations or appliance where electrical circuits are used​
    9·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!