What is wrong with my Prime-Composite program?

This is a discussion on What is wrong with my Prime-Composite program? within the C Programming forums, part of the General Programming Boards category; Okay so first of all I'm a beginner. This code took me like 3 days. My professor only allows me ...

  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 10:05 AM.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,541
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  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, 10:10 AM
  2. Composite Classes
    By w274v in forum C++ Programming
    Replies: 2
    Last Post: 03-26-2006, 04:26 PM
  3. prime program
    By mikeusmc in forum C Programming
    Replies: 8
    Last Post: 03-08-2006, 11:31 PM
  4. Please help w/ prime # program Almost done...
    By auntdeen in forum C Programming
    Replies: 12
    Last Post: 10-28-2002, 06:16 PM
  5. Composite inheritance
    By inakappeh in forum C++ Programming
    Replies: 4
    Last Post: 03-09-2002, 12:00 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21