Note that there are some explanatory texts on larger screens.

plurals
  1. POC programming. Why does 'this' code work but not 'that' code?
    primarykey
    data
    text
    <p>Hello I am studying for a test for an intro to C programming class and yesterday I was trying to write this program to print out the even prime numbers between 2 and whatever number the user enters and I spent about 2 hours trying to write it properly and eventually I did it. I have 2 pictures I uploaded below. One of which displays the correct code and the correct output. The other shows one of my first attempts at the problem which didn't work correctly, I went back and made it as similar to the working code as I could without directly copying and pasting everything.</p> <p>unfortunately new users aren't allowed to post pictures hopefully these links below will work.</p> <p>This fails, it doesn't print all numbers in range with natural square root:</p> <pre><code>for (i = 2; i &lt;= x; i++) { //non relevant line a = sqrt(i); aa = a * a; if (aa == i); printf("%d ",i); } </code></pre> <p>source: <a href="http://i.imgur.com/WGG6n.jpg" rel="nofollow">http://i.imgur.com/WGG6n.jpg</a></p> <p>While this succeeds, and prints even numbers with natural sqaure root</p> <pre><code>for (i = 2; i &lt;= x; i++) { a = sqrt(i); aa = a * a; if (aa == i &amp;&amp; ((i/2) *2) == i) printf("%d ", i); } </code></pre> <p>source: <a href="http://i.imgur.com/Kpvpq.jpg" rel="nofollow">http://i.imgur.com/Kpvpq.jpg</a></p> <p>Hopefully you can see and read the screen shots I have here. I know that the 'incorrect code' picture does not have the (i/2)*2 == i part but I figured that it would still print just the odd and even numbers, it also has the code to calculate "sqrd" but that shouldn't affect the output. Please correct me if I'm wrong on that last part though.</p> <p>And Yes I am using Dev-C++ which I've read is kinda crappy of a program but I initally did this on code::blocks and it did the same thing...</p> <p>Please I would very much appreciate any advice or suggestions as to what I did wrong 2 hours prior to actually getting the darn code to work for me.</p> <p>Thank you, Adam</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload