113k views
3 votes
Flowgorithm homework

I need a program that, when you insert a number by your choice, it tells you if that number is a prime number.
I need to use Flowgorithm.

Thanks a lot.

User Saljuama
by
5.2k points

1 Answer

4 votes

Answer:

START

PROMPT "Enter a number: "

GET number

IF number < 2 THEN

DISPLAY "This is not a prime number."

STOP

ELSE

SET is_prime = True

SET divisor = 2

WHILE divisor < number

IF number MOD divisor = 0 THEN

SET is_prime = False

BREAK

END IF

SET divisor = divisor + 1

END WHILE

IF is_prime THEN

DISPLAY "This is a prime number."

ELSE

DISPLAY "This is not a prime number."

END IF

END IF

STOP

Step-by-step explanation:

Here is a breakdown of what this Flowgorithm diagram does:

  1. Prompt the user to enter a number.
  2. Read in the number.
  3. If the number is less than 2, it is not a prime number, so the program displays a message and stops.
  4. If the number is greater than or equal to 2, the program sets a variable is_prime to True and sets a variable divisor to 2.
  5. The program enters a while loop that will run as long as divisor is less than number.
  6. Inside the loop, the program checks if number is evenly divisible by divisor using the modulus operator (%). If it is, the program sets is_prime to False and exits the loop using the break statement.
  7. If number is not evenly divisible by divisor, the program increments divisor by 1 and the loop continues.
  8. When the loop ends, the program checks the value of is_prime. If it is True, the program displays a message saying that the number is a prime number. If it is False, the program displays a message saying that the number is not a prime number.

I hope this helps! Let me know if you have any questions.

User Rhitz
by
5.3k points