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
Alina [70]
4 years ago
11

Write a Python program that asks the user for a positive, odd value. Once the value is validated determine if the number is Prim

e (i.e., divisible only by itself and 1.)

Computers and Technology
1 answer:
Umnica [9.8K]4 years ago
5 0

Answer:

val = int(input("Enter a positive odd value "))

flag = True # let the number entered is alreay prime

if(val > 2 and val%2 == 1):  # prime numbers start from 2

   half = int(val/2);

   for div in range(2,half):  # dividing the number from 2 to half of its number

       if(val % div == 0): # if completely divisible

           print("Not prime")

           flag = False   # Changing the status of prime number as false

           break

   if(flag == True):

       print(val, "is a prime number")

else:

   print("Invalid input, Please Enter a valid positive odd number")

Explanation:

Steps:

1. Let us take input from the user using input() method.

2.  Initially, let the number is prime.

3. If the number is negative or even, the go to else part and ask the user for a valid input and terminate the program by giving a message to user.

(We actually check for values greater than 2 because 1 is not considered as  a prime number)

4. If the number is positive and odd, then we keep on dividing the number from 2 to half of its number.

(We actually check for values greater than 2 because 1 is not considered as  a prime number)

5. If the number is divisible, we change the status to False and break the loop.

6. If the flag is still True, we print that it is a Prime number else we print that it is not a prime number.

Please refer to the comments section as well and the attached image for proper indentation of the program.

You might be interested in
Tanner has had many different jobs. He previously designed the playscape at the local park with natural rocks, creeks, and veget
zavuch27 [327]
I think it would be Design. 
3 0
3 years ago
Read 2 more answers
In a particular factory, a team leader is an hourly paid production worker who leads a small team. In addition to hourly pay, te
Ray Of Light [21]

Answer:

#include <iostream>

#include <iomanip>

#include <string>

using namespace std;

class Employee

{

private:

string name; // Employee name

string number; // Employee number

string hireDate; // Hire date

public:

Employee()

{ name = ""; number = ""; hireDate = ""; }

Employee(string aName, string aNumber, string aDate)

{ name = aName; number = aNumber; hireDate = aDate; }

void setName(string n)

{ name = n; }

void setNumber(string num)

{ number = num; }

void setHireDate(string date)

{ hireDate = date; }

string getName() const

{ return name; }

string getNumber() const

{ return number; }

string getHireDate() const

{ return hireDate; }

};

class ProductionWorker : public Employee

{

private:

int shift; // The worker's shift

double payRate; // The worker's hourly pay rate

public:

ProductionWorker() : Employee()

{ shift = 0; payRate = 0.0; }

ProductionWorker(string aName, string aNumber, string aDate,

int aShift, double aPayRate) : Employee(aName, aNumber, aDate)

{ shift = aShift; payRate = aPayRate; }

void setShift(int s)

{ shift = s; }

void setPayRate(double r)

{ payRate = r; }

int getShiftNumber() const

{ return shift; }

string getShiftName() const

{ if (shift == 1)

return "Day";

else if (shift == 2)

return "Night";

else

return "Invalid";

}

double getPayRate() const

{ return payRate; }

};

class TeamLeader : public ProductionWorker

{

private:

double monthlyBonus; // Monthly bonus amount

double requiredTraining; // Required training hours

double completedTraining; // Completed training hours

public:

};

void displayInfo(ProductionWorker);

void displayInfo(TeamLeader);

int main()

{

ProductionWorker pw("John Jones", "123", "1/1/2006", 2, 18.00);

displayInfo(pw);

/* remove the comment to enable

TeamLeader leader("John Jones", "123", "1/1/2006", 2, 18.00,

500.0, 20.0, 12.5);

displayInfo(leader);

*/

return 0;

}

void displayInfo(ProductionWorker e)

{

cout << setprecision(2) << fixed << showpoint;

cout << "Name: "

<< e.getName() << endl;

cout << "Employee number: "

<< e.getNumber() << endl;

cout << "Hire date: "

<< e.getHireDate() << endl;

cout << "Shift: "

<< e.getShiftName() << endl;

cout << "Shift number: "

<< e.getShiftNumber() << endl;

cout << "Pay rate: "

<< e.getPayRate() << endl;

}

void displayInfo(TeamLeader e)

{

/* enable this section to print

cout << setprecision(2) << fixed << showpoint;

cout << "Name: "

<< e.getName() << endl;

cout << "Employee number: "

<< e.getNumber() << endl;

cout << "Hire date: "

<< e.getHireDate() << endl;

cout << "Shift: "

<< e.getShiftName() << endl;

cout << "Shift number: "

<< e.getShiftNumber() << endl;

cout << "Pay rate: "

<< e.getPayRate() << endl;

cout << "Monthly bonus: $"

<< e.getMonthlyBonus() << endl;

cout << setprecision(1);

cout << "Required training hours: "

<< e.getRequiredTraining() << endl;

cout << "Completed training hours: "

<< e.getCompletedTraining() << endl;

*/

}

Hope it helps.

8 0
3 years ago
A(n) ______of the audio or video clip makes the content easily accessible to people with hearing disabilities.
defon

subtitles

hope that helped you

7 0
4 years ago
Read 2 more answers
What term defines Internet applications that help people connect with each other through a virtual environment by providing them
andreev551 [17]
D. social networks is your answer
4 0
4 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:
  • The following declaration appears in a program: short totalPay, basePay = 500, bonus = 1000; The following statement appears in
    9·1 answer
  • What is that tool that makes liquid metal? It is used for wires, Ik I might sound dumb but I'm rlly curious​
    7·1 answer
  • Would you recommend the 4a generations of computers to a friend
    5·1 answer
  • A7DF is the hexadecimal representation for what bit pattern?
    8·1 answer
  • The GPS device in an automobile uses which communication channel?
    7·1 answer
  • Would a hurricane form if the humidity is really high and the breeze is very low
    11·1 answer
  • Why must a satellite have distinct uplink and downlink frequencies.​
    6·1 answer
  • What is the benifet of the manager feature
    13·1 answer
  • Which of the four factors of production are hit the hardest when high unemployment occurs ?
    10·1 answer
  • 1. It manages the computer's memory and processes, as well as all of its software and hardware.
    8·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!