-
October 16th, 2013, 09:34 AM
#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.
-
October 16th, 2013, 09:43 AM
#2
Re: Noobie to C::B
How would you do it using a piece of paper and a pencil?
Victor Nijegorodov
-
October 16th, 2013, 10:11 AM
#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.
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
October 16th, 2013, 11:26 AM
#4
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.
-
October 16th, 2013, 04:01 PM
#5
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|