Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

4.13 Lab: Prime Number Checker in C (NOT C++) Write a program that will read in an integer from the user and test to see

4.13 Lab: Prime Number Checker in C (NOT C++)

Write a program that will read in an integer from the user and test to see whether or not it is prime. You are to determine if each integer read is prime or not, and output the result to the screen as shown below. If a number is prime, your output should read:

101 = 1 x 101

101: PRIME Number

For a number that turns out not to be prime, your output should read:

333 = 1 x 333

333 = 3 x 111

333 = 9 x 37 333:

NOT A PRIME Number

After each test allow the user to exit or input the next number to be tested. Example interaction:

PRIME NUMBER CHECKER

Enter Value: 101

101 = 1 x 101

101: PRIME Number

Would you like to enter another value? (1=Yes, 0=No)> 1

Enter Value: 333

333 = 1 x 333

333 = 3 x 111

333 = 9 x 37

333: NOT A PRIME Number

Would you like to enter another value? (1=Yes, 0=No)> 0

Technique: If a number is prime that means that there are no smaller numbers than the number that divide evenly into it, other than one. For example, assume we wish to test if the number 11 is prime. We successively test to see if 2, 3, 4, 5, 6, 7, 8, 9 or 10 divide evenly into 11; clearly, none do, and hence, 11 must be prime. In fact, we did not have to test all the way up to 10, for if 10 were a factor, a smaller number would have already successfully divided evenly. This is easy to see for an example of testing the number 10 to see if its prime: we do not have to test up to 5 since the test for the possible factor 2 already identified the number 5 as a factor. In fact, we only need to check factors from 2 up to the rounded-up square root of the number (i.e., next whole number) we wish to test (this is true only if the number we are testing is greater than 3 i.e., if you want to test 66, you only need to test if 66 is evenly divisible by any of the numbers from 2 to 9). The test for whether one number divides evenly into another can be preformed easily using the modulo operator (recall, modulo gives the remainder; if the remainder is zero, that number must be a factor). In this assignment, we will use this brute-force method to find prime numbers. Before you start writing code: Develop a flowchart for your prime number algorithm. The algorithm is to consider an integer number N and determine if that number is prime while simultaneously writing out all of the possible factors for N. To complete this task, you will need to use flags in your code (and in your flowchart). A flag is a variable that takes on the values of TRUE (typically 1) or FALSE (typically 0). You are to use one flag called is_prime. Initially, whenever a new number N is to be tested to determine whether or not it is prime, the isprime flag is first set to TRUE i.e. isprime=1. If, at any point while testing a possible factor, a zero remainder is found (i.e., N is not prime), the isprime flag is to be cleared to FALSE (flag lingo: when you set a flag, it becomes 1; when you clear a flag, it becomes 0). Once cleared, the flag remains FALSE while all remaining possible factors are tested. When testing is complete, check the state of the isprime flag to know whether to denote N prime or not prime. If a new number is to be tested, the isprime flag must be reset to TRUE in order to repeat the process.

I've tried this problem a plethora of times, however my outside while loop keeps getting messed up and it will repeat too many times.

Thank you for all the help!

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Data Management Databases And Organizations

Authors: Watson Watson

5th Edition

0471715360, 978-0471715368

More Books

Students also viewed these Databases questions

Question

10. Are you a. a leader? b. a follower? _______

Answered: 1 week ago

Question

What did they do? What did they say?

Answered: 1 week ago