How do I get program to identify prime number?

  • (2 Pages)
  • +
  • 1
  • 2

19 Replies - 1692 Views - Last Post: 27 January 2013 - 11:48 PM Rate Topic: -----

#16 Bawnawgwa   User is offline

  • D.I.C Head

Reputation: 9
  • View blog
  • Posts: 153
  • Joined: 21-January 13

Re: How do I get program to identify prime number?

Posted 27 January 2013 - 02:02 AM

ooh cool. I believe it accurately depicts if a number is prime or not! But getch() is now not working to keep the program open long enough to completely read it, nor is syste("pause"), which I don't use... but since getch() isn't working I tried it and it didn't work either.

And I do have everything you put now lol
Was This Post Helpful? 0
  • +
  • -

#17 Adak   User is offline

  • D.I.C Lover
  • member icon

Reputation: 332
  • View blog
  • Posts: 1,168
  • Joined: 01-April 11

Re: How do I get program to identify prime number?

Posted 27 January 2013 - 02:41 AM

View PostBawnawgwa, on 27 January 2013 - 02:02 AM, said:

ooh cool. I believe it accurately depicts if a number is prime or not! But getch() is now not working to keep the program open long enough to completely read it, nor is syste("pause"), which I don't use... but since getch() isn't working I tried it and it didn't work either.

And I do have everything you put now lol

Probably so. Add a second getch(); just before the return 0; to get it to keep the window open for you. Another way is to add scanf("%s", garbage); where "garbage" is a small char array that's never used.

The one thing you don't have, is a compiler that was written for Windows. Something is still in the input stream (aka keyboard buffer), and that is allowing the program to "skip" over the first getch(); -- it sees the char in the input stream, takes it and says "Thanks a lot", and scoots on down the road and the console window closes.

Since I use a modern compiler, I don't see that behavior very often. The window stays open when the program is finished, 95% of the time. Happened all the time in Turbo C, however.

Keep your hands off the keys during program execution, unless required.

This post has been edited by Adak: 27 January 2013 - 02:42 AM

Was This Post Helpful? 0
  • +
  • -

#18 Bawnawgwa   User is offline

  • D.I.C Head

Reputation: 9
  • View blog
  • Posts: 153
  • Joined: 21-January 13

Re: How do I get program to identify prime number?

Posted 27 January 2013 - 10:16 AM

Hooray! That second getch() did the trick. Yeah to run c programs I apparently cannot use visual studio... I write c++ in visual studio and then save a .c copy when it is finished... But this one I guess I had to work with it IN c...

Thank you for your help haha I finally finished!
Was This Post Helpful? 0
  • +
  • -

#19 Adak   User is offline

  • D.I.C Lover
  • member icon

Reputation: 332
  • View blog
  • Posts: 1,168
  • Joined: 01-April 11

Re: How do I get program to identify prime number?

Posted 27 January 2013 - 04:01 PM

Cue the fat lady to sing!

"At last... my code compiles along,
My sleepless nights are over,
And my program runs like a song"...

(with apologizes to a great song). ;)
Was This Post Helpful? 0
  • +
  • -

#20 raghav.naganathan   User is offline

  • Perfectly Squared ;)
  • member icon

Reputation: 412
  • View blog
  • Posts: 1,449
  • Joined: 14-September 12

Re: How do I get program to identify prime number?

Posted 27 January 2013 - 11:48 PM

View Postxtreampb, on 27 January 2013 - 08:29 AM, said:

well mod results in a float most of the time. you are storing a float in an integer. That may lead you in a better direction.


Well, I'm sorry to say this...but on what basis did you say mod results in a float??

what mod does is it returns the remainder of a division and well, remainder can never be a floating point value.

One more thing...the operands mod takes should be int...any other datatype used in a mod operation will give the error as follows.

error: invalid operands to binary %


regards,
Raghav

This post has been edited by raghav.naganathan: 27 January 2013 - 11:50 PM

Was This Post Helpful? 1
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2