CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Thread: Noobie to C::B

  1. #1
    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. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: Noobie to C::B

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

  3. #3
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    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)

  4. #4
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    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. #5
    Join Date
    Jul 2013
    Posts
    576

    Re: Noobie to C::B

    Quote Originally Posted by millw0715 View Post
    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
  •  





Click Here to Expand Forum to Full Width

Featured