Noobie to C::B
 CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com

1. Junior Member
Join Date
Oct 2013
Posts
1

Noobie to C::B

I'm pretty new to programming in general and I have a very basic idea of of how everything works. I'm going through practice questions to help improve my programming and I cannot seem to figure out the following, it's got me stumped.

A prime number is any positive integer that is evenly divisible only by itself and
one. The smallest prime number is two. Devise a method to calculate prime
numbers based on divisibility. Write a program that calculates and displays all
the prime numbers up to a maximum number specified by the user. For
example, if the user wished to see all the primes up to 25, the program would
provide the following list: 2, 3, 5, 7, 11, 13, 17, 19 and 23. The program needs
to display the list in an appropriate way.

Could anyone lend a helping hand? Thanks.

2. Re: Noobie to C::B

How would you do it using a piece of paper and a pencil?

3. Re: Noobie to C::B

Hint. This topic has cropped up before on these codeguru forums with solution programs provided. You should be able to find them using a forum search.

4. Elite Member Power Poster
Join Date
Nov 2003
Location
Florida
Posts
12,518

Re: Noobie to C::B

Probably better to figure it out himself than just copy and paste somebody's solution without learning anything.

Victor is putting you on the right path. List the steps you'd need to take before you try to write any bode.

5. Member +
Join Date
Jul 2013
Posts
576

Re: Noobie to C::B

Originally Posted by millw0715
A prime number is any positive integer that is evenly divisible only by itself and
one.
One convenient way to check whether an integer n is evenly divisible by another integer m is by way of the modulo operator %. The modulo operator gives the rest after integer division. If it's 0 the integers are evenly divisible, like

Code:
```if (n%m == 0) {
// n is evenly divisible by m (so if m is in the range from 2 to n-1 then n cannot be a prime)
}```
Last edited by razzle; October 16th, 2013 at 04:18 PM.

Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•