Question: Recall that an integer is called prime if its only divisors are 1 and the number itself. The rst few primes are: 2, 3, 5,

Recall that an integer is called prime if its only divisors are 1 and the number itself. The rst few primes are: 2, 3, 5, 7, and 11 (1 is not considered to be prime). Write a program that prints (to a le) the rst n primes, where n is input by the user. Your program must utilize the function isprime() shown below. For marking purposes run your program with n = 15. Your program output should look like: count ======= 1 2 3 4 5 6 7 8 9 prime ======= 2 3 5 7 11 13 17 19 23 Note that the tricky part of testing whether an integer is prime has been coded for you in the function isprime. /* File : primes.c This program finds the first n primes where n is input by the user Programmer: * / #include /* the prototype * / int isprime(int n); int main(void) /* you fill in here * / return 0; /* the function definition * / Date: int isprime(int m) /* returns 1 if m is prime or 0 if m is not prime * / int k, limit ; int result ; i f (m == 1) r esult = 0; else if (m == 2) r esult = 1; /* 1 is not prime * / /* 2 is prime * / else if (m%2 == 0) /* even numbers > 2 are not prime * / r esult = 0; else r esult = 1; /* start off assuming that m is prime * / l i mit = m/2; /* only have to check up to 1/2 of the number * / for( k = 3; k <= limit; k = k + 2 ) 2 if( (m%k) ==0 ) /* k divides msom is not prime * / result = 0; break; return result

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Accounting Questions!