Thread: What is wrong with my Prime-Composite program?

  1. #1
    Registered User
    Join Date
    Dec 2011
    Posts
    18

    What is wrong with my Prime-Composite program?

    Okay so first of all I'm a beginner. This code took me like 3 days. My professor only allows me to use basic functions like if-else, loops(do-while, while and for). This code works perfectly but when I input some odd composite integers like 171, it will tell me that that it is a composite number three times. *Number is composite. Number is composite. Number is composite* I already put break; so it will stop the infinite message. Some odd composite integer works fine like 169, 9 and 25. Note that I had to use getchar() twice because for some reason, DevC++ would quit the program immediately if I use only one. Thanks for any kind of help.

    [Code hidden]
    Last edited by edsoneicc; 01-01-2012 at 11:05 AM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Perhaps line 27 should be "if" rather than "while"
    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.

  3. #3
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Your while statement with the break at the end is really just an if statement. Change that while to an if and it seems to work. Leave the break, though, to break out of the other while.

  4. #4
    Registered User
    Join Date
    Dec 2011
    Posts
    18
    Whoa! Thanks guys! It works now!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. non prime numbers or composite numbers program.help plz!
    By danishzaidi in forum C Programming
    Replies: 10
    Last Post: 11-15-2011, 11:10 AM
  2. Composite Classes
    By w274v in forum C++ Programming
    Replies: 2
    Last Post: 03-26-2006, 05:26 PM
  3. prime program
    By mikeusmc in forum C Programming
    Replies: 8
    Last Post: 03-09-2006, 12:31 AM
  4. Please help w/ prime # program Almost done...
    By auntdeen in forum C Programming
    Replies: 12
    Last Post: 10-28-2002, 07:16 PM
  5. Composite inheritance
    By inakappeh in forum C++ Programming
    Replies: 4
    Last Post: 03-09-2002, 01:00 AM