Thread: prime number

  1. #1
    Registered User
    Join Date
    Aug 2011
    Posts
    3

    prime number

    hi.. i'v just started my c programming and i have a problem to solve after 2 lessons.
    i'm so bad at this.. i need to creat a program that askes for a number
    0 < n <= 80
    and the result is- n lines of *
    each line is equal to a prime number like if you enterd 13:
    you will see

    **
    ***
    *****
    *******
    ***********
    *************

    please can someone cam give me a clue to how to start
    i know it has someting to do with loops of for or while.. and that i need to check every number till its squre

    please help me

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    One approach is to start by writing a program that has similiar requirements for input, but which prints out a list of prime numbers.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Not up to it's square! Up to it's square root.

    Things like this are usually done either by calling a function, or using a pair of nexted loops.

    Code:
    handle 2 here, then
    
    for each odd number higher than 2
       get the next odd number
       for each number from 3 to square root of the number
          assume the number is prime
          test it to see if it's able to divide evenly into the number being tested
          (mod == 0 is a common test for that)
          if any number divides evenly into the number being tested, then
          set the variable to not prime, and break out of this loop.
       }
       if the test variable is still good here, the number is prime, so save it
       and or print your lines, etc. 
    
    }
    If you just save the primes you find, then you can draw your lines in another function, which helps to break up the problem into more manageable pieces.

  4. #4
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Quote Originally Posted by bvsa View Post
    please can someone cam give me a clue to how to start
    Sit down and think about the problem. As tater likes to say, "until you understand what the problem is there is no way for you to come up with a solution".

    Quote Originally Posted by bvsa View Post
    i know it has someting to do with loops of for or while.. and that i need to check every number till its squre

    please help me
    Some items you may want to look over:

    1. Lesson 3: Loops
    2. Lesson 4: Functions
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  5. #5
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Do the program in incremental steps:

    1. Start with simple program that asks user for a number "n" and writes that number back out to the screen.
    2. Modify above to write all numbers up through "n" to the screen using a loop.
    3. Create an IsPrime function (this will likely involve more loops) that returns true if a number passed to the function is prime (return false if the passed in number is not prime).
    4. Modify the program to write out the numbers to the screen like before but now also write whether each number is a prime or not (call your IsPrime function).
    5. Modify the program to only write the number to the screen if it is a prime.
    6. Now modify the program to write out correct amount of asterisks for each prime number instead of the number itself (this is another loop).


    A big problem beginners have is they see the whole problem written down at once and get overwhelmed... then they give up because it is too hard. If you take things one step at a time, break the one big/hard problem into more (but smaller/easier) problems, then the task becomes much easier. This is Problem Solving 101.
    Last edited by hk_mp5kpdw; 08-08-2011 at 02:04 PM.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. largest number prime number that can be produced...
    By ElemenT.usha in forum C Programming
    Replies: 8
    Last Post: 02-17-2008, 01:44 AM
  2. Replies: 3
    Last Post: 03-29-2005, 04:24 PM
  3. prime number.
    By tdoctasuess in forum C Programming
    Replies: 13
    Last Post: 05-13-2004, 08:03 AM
  4. prime number
    By sg786 in forum C# Programming
    Replies: 2
    Last Post: 10-18-2002, 12:27 AM
  5. Prime Number?
    By cprogramnewbie in forum C Programming
    Replies: 8
    Last Post: 03-16-2002, 12:18 AM