Share
Explore BrainMass

C program to print first n prime numbers

Write a program that prints out the first n primes, and n is input by the user. Example as follows:

How many do you want to see? 3000

1: 2
2: 3
....

25: 97
26: 101
....

2999: 27437
3000: 27449

Program should be written in three files:

primes.h
main.c
is_primes.c

Attached are parts of two files (primes.h, is_primes.c)

Explain how is_prime() works. Complete the program by writting main() in main.c. There are 168 primes that are less than 1000. Does the program confirm this? How many primes are there that are less than 10,000?

Attachments

Solution Preview

Functionality of is_prime function is expressed in following steps.

1. Check whether the argument number (n) is 2. If it is 2 (first prime number) then return 1.

2. Check if n is a multiple of 2. If "n modulus 2" is 0, then n is a multiple of 2 and so it can not be a prime number. Function returns 0 in this ...

Solution Summary

C code in solution assumes fair input to the program and has been tested for compilation with "gcc -Wall -ansi main.c is-prime.c", and for execution with inputs in the set (0-5, 168-170, 1230, 10000).

$2.19