Practical 4
You should first complete any remaining work from last week.
Exercise 1
The Prime Numbers are the numbers greater than 1 with no integer divisor other than one and the number itself.
For example, 7 is a prime number because,
7%2 = 1
7%3 = 1
7%4 = 3
7%5 = 2
7%6 = 1
but 9 is not prime because 9%3 = 0.
-7, 0 and 1 are not prime numbers because they are not more than one.
The first few prime numbers are 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31 and 37.
- Write a program to test if a number is prime
- If the number is not prime you should give an example of the number it divides by
Enter a number to test: 7
7 is prime
Enter a number to test: 9
9 is not prime (divides by 3)
Exercise 2
- Write a program to generate and print the sequence of prime numbers (2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, ...)
- Try to make your program run as quickly as possible
- Run your program for two minutes - what is the largest prime number you can find in this time?
Exercise 3
- Write a program to store, display and edit the marks of ten students
- Allow the user to exit the loop (eg by entering -1 as the student number)
- Do not allow the user to enter invalid student numbers
You may follow the format of the program in the practicals or use your own design. You can extend the program as you wish (try adding student names).