hwk7

1
EL2008 Homework #7 Semester II 2013/14 Due Date: February 28, 2014 Write a C program to find the prime numbers. Have a main FOR loop that runs between 2 and 500. Check to see if each number between these limits is prime. Keep in mind that this exercise will involve integer arithmetic which you can use to your advantage. Print the list of the prime numbers 10 to a row with two spaces between each column. Below that list, print how many prime numbers were found in the above specified range. For example, if I were looking for all primes between 2 and 130, the following output would be printed: 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97 101 103 107 109 113 127 There are 31 prime numbers between 2 and 130. The source code for this problem is very short but takes some thought. I recommend the following steps to help you through this exercise: 1. Pick an integer and work out a step-by-step procedure on paper to check to see if it is prime. Do this with a number of examples until you are sure you understand the algorithm. 2. Write the source code to find ONE prime number. At this stage, do not worry about fancy output or doing all the primes between 2 and 500. Just get the “heart” of the code working. 3. If your code is not running correctly, insert printf statements to check the values stored in variables at various places in the code. 4. Finally, put a FOR loop outside the code written in the previous steps and format the output as shown above. When the code and documentation are complete, submit it to the grader. Extra credit if you implement Sieve of Erasthosthenes.

description

Pr PMC

Transcript of hwk7

Page 1: hwk7

EL2008 Homework #7 Semester II 2013/14

Due Date: February 28, 2014

Write a C program to find the prime numbers. Have a main FOR loop that runs between 2 and 500.Check to see if each number between these limits is prime. Keep in mind that this exercise willinvolve integer arithmetic which you can use to your advantage. Print the list of the prime numbers10 to a row with two spaces between each column. Below that list, print how many prime numberswere found in the above specified range. For example, if I were looking for all primes between 2and 130, the following output would be printed:

2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97 101 103 107 109 113127There are 31 prime numbers between 2 and 130.

The source code for this problem is very short but takes some thought. I recommend the followingsteps to help you through this exercise:1. Pick an integer and work out a step-by-step procedure on paper to check to see if it is prime. Do

this with a number of examples until you are sure you understand the algorithm.2. Write the source code to find ONE prime number. At this stage, do not worry about fancy output

or doing all the primes between 2 and 500. Just get the “heart” of the code working.3. If your code is not running correctly, insert printf statements to check the values stored in

variables at various places in the code.4. Finally, put a FOR loop outside the code written in the previous steps and format the output as

shown above.

When the code and documentation are complete, submit it to the grader.

Extra credit if you implement Sieve of Erasthosthenes.

fatih sigmanova
Highlight
fatih sigmanova
Highlight
fatih sigmanova
Highlight
fatih sigmanova
Highlight