Question

Assignment 3 Chapter 2: Algorithm Discovery and Design More about Pseudocode Design an algorithm that is...

Assignment 3

Chapter 2: Algorithm Discovery and Design

More about Pseudocode

  1. Design an algorithm that is given a positive integer N and determines whether N is a prime number, that is, not evenly divisible by any value other than 1 and itself. The output of your algorithm is either the message ‘not prime’, along with a factor of N, or the message ‘prime ‘
  2. Many excellent simulations of sorting algorithms are available, examine them if they have questions about this algorithm. You can use the link given in the class.
  3. Write an algorithm that gets the price for item A plus the quantity purchased. The algorithm prints the total cost, including a 6% sales tax.
  4. Write an if/then/else primitive to do each of the following operations:
  • Compute and display the value x÷y if the value of y is not 0. If y does have the value 0, then display the message “Unable to perform the division”.
  • Compute the area and circumference of a circle given the radius r if the radius is greater than or equal to 1.0; otherwise, you should compute only the circumference. Area = 3.14 * r2 , Circumference = 2 * 3.14 * r

Homework Answers

Answer #1

ALGORITHM 1 (PRIME NUMBER)

check_prime()

{

read N

flag=1

for i =2 to n/2 , do

{

if N%i=0, then

{

flag=0

break

}

}

if N=1,then flag=0

if flag=1, then print "prime"

else

{

print "not prime"

print "The factors of N are:"

for i=1 to N, do

{

if N%i==0, then print "i"

}

}

}

ALGORITHM 2 (TOTAL COST)

total_cost()

{

read Price

read Quantity

cost=Price*Quantity*1.06

print "cost"

}

ALGORITHM 3 (DIVISION)

division()

{

read x

read y

if y=0, then

{

print "Unable to perform the division."

}

else

{

result= x/y

print "result"

}

}

ALGORITHM 4(CIRCLE)

circle()

{

read R

Circum = 2* 3.14 * R

if R>=1.0, then

{

Area= 3.14 * R * R

print "Area"

print "Circum"

}

else

{

print "Circum"

}

}

Hope my answer is relevant to the question you asked and is easy to understand. Feel free to ask any doubts in the comments section.

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT