-
Notifications
You must be signed in to change notification settings - Fork 0
/
Chapter5-Q27-a.c
48 lines (36 loc) · 1.3 KB
/
Chapter5-Q27-a.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
/*
Author is : Ibrahim Halil GEZER
5.27 (Prime Numbers) An integer is said to be prime if it’s divisible by only 1 and itself. For example,
2, 3, 5 and 7 are prime, but 4, 6, 8 and 9 are not.
a) Write a function that determines if a number is prime.
b) Use this function in a program that determines and prints all the prime numbers between
1 and 10,000. How many of these 10,000 numbers do you really have to test before
being sure that you have found all the primes?
c) Initially you might think that n/2 is the upper limit for which you must test to see if a
number is prime, but you need go only as high as the square root of n. Why? Rewrite
the program, and run it both ways. Estimate the performance improvement.
*/
#include <stdio.h>
int prime ( int a ) ;
int main ( void ) {
int num ;
printf ( "Enter an integer : " ) ;
scanf ( "%d" ,&num) ;
prime ( num ) ;
return 0 ;
} // function main end
int prime ( int a ) {
int i ;
int prime = 1 ; // flag
if ( a <= 1 )
printf ( "This number is not prime. \n" );
for (i = 2 ; i < a - 1 ; i++ ) {
if ( a % i == 0)
prime = 0 ;
}
if ( prime == 1 )
printf ( "%d is a prime number.. \n ", a );
else
printf ( "%d is not a prime number. \n ", a );
return 0 ;
} // end function prime