Write a C or C++ program that outputs prime numbers. This program should work as follows:

The user will run the program and will enter a number on the command line.

The program will then create another process using fork() that outputs all the prime numbers less than or equal to the number entered by the user.

Recommended Answers

All 2 Replies

Dumping an assignment to a post without at least saying that is what you are doing isn't the most polite way to start a conversation, especially if you don't show any of your own effort to solve the problem.

It also isn't the safest approach, since plenty of professors regularly search the web to catch students doing things such as this (or more often, they get their TAs and grad students to do it for them).

There are websites where they will do your homework for you, at least for a fee. This isn't one of those sites.

So, try to solve it yourself, and if you get stuck, let us know what you've tried and we'll see if we can help you then. This may be difficult, given the rather minimal and poorly-specified assignment, but it is your assignment, not any of ours.

I will give you one hint, though, in that the problem is really aimed at demonstrating the use of fork(). The solution for the second part, emitting the list of primes, would be trivially solved using the algorithm known as the Sieve of Eratosthenes. Chances are, if you are in a course which covers fork() (usually a Systems Programming or Operating Systems course), then you should already be familiar with the Sieve from previous coursework.

You aren't following protocol. The way it worked when I was a lad was:

  • first you steal my lunch money
  • then you tell me to do your homework
commented: - then they steal your girl. +0
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.