Thread: Prime Numbers

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    18

    Prime Numbers

    I have to make a program in which
    take two numbers as input
    print prime numbers between them
    take sum of them
    and print the prime numbers is ascending order.
    Is it possible to make it without loops?

  2. #2
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    In theory, yes, just write a statement for each possible case.
    In practice, no.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  3. #3
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    In theory, yes, you could use recursion or goto's.
    In practice, anyone doing this should be shot.



  4. #4
    Registered User
    Join Date
    Oct 2008
    Posts
    18
    how is it done by loop way sorry have no clue

  5. #5
    Registered User
    Join Date
    Aug 2008
    Location
    Belgrade, Serbia
    Posts
    163
    Making it without loops is not possible. Why? Because you don't know what numbers user is gonna input. That's the beauty of programming, making an algorithm that can handle every situation.

    To test a number you would use the next statement:
    Code:
    if((n!=2 && n!=3 && n!=5 && n!=7) && (n%2==0 || n%3==0 || n%5==0 || n%7==0))
    {
        do something if it's NOT a prime
    }
    Or make a function:
    Code:
    int isprime(int n)
    {
        if((n!=2 && n!=3 && n!=5 && n!=7) && (n%2==0 || n%3==0 || n%5==0 || n%7==0)) return 0; //NOT a prime
        return 1; //A prime
    }
    Last edited by hauzer; 11-07-2008 at 08:20 AM.
    Vanity of vanities, saith the Preacher, vanity of vanities; all is vanity.
    What profit hath a man of all his labour which he taketh under the sun?
    All the rivers run into the sea; yet the sea is not full; unto the place from whence the rivers come, thither they return again.
    For in much wisdom is much grief: and he that increaseth knowledge increaseth sorrow.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > Is it possible to make it without loops?
    Gotta love the bone-headed assignments from clueless teachers.

    What exactly is the point of these exercises?
    It's not like you're going to reuse your new found skill (writing programs without loops) ever again.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Registered User
    Join Date
    Aug 2007
    Posts
    15
    Perhaps he attends a class in functional programming

  8. #8
    Registered User
    Join Date
    Sep 2007
    Posts
    67
    errr.. depends how you define a loop.. you could just write the loop in assembly instead, but honestly I can't think of a way to do that.. think about how you as a human would try to solve a proble like that. Somebody gives you two numbers, you check each number to see if it's prime, and you continue doing so until you've done all the numbers. That's a loop; programming in ways that are different from how you think tends to be unproductive in my experience

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Logical errors with seach function
    By Taka in forum C Programming
    Replies: 4
    Last Post: 09-18-2006, 05:20 AM
  2. Replies: 18
    Last Post: 11-04-2005, 02:41 PM
  3. prime numbers, counters, help!
    By vege^ in forum C++ Programming
    Replies: 1
    Last Post: 03-10-2003, 04:32 PM
  4. Prime numbers
    By Lazy Student in forum C Programming
    Replies: 12
    Last Post: 05-14-2002, 05:53 AM
  5. Homework help
    By Jigsaw in forum C++ Programming
    Replies: 2
    Last Post: 03-06-2002, 05:56 PM