Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7812625
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T04:34:23+00:00 2026-06-02T04:34:23+00:00

Hello I am studying for a test for an intro to C programming class

  • 0

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.

unfortunately new users aren’t allowed to post pictures hopefully these links below will work.

This fails, it doesn’t print all numbers in range with natural square root:

for (i = 2; i <= x; i++)
{
   //non relevant line
   a = sqrt(i);
   aa = a * a;
   if (aa == i); 
       printf("%d ",i);
}

source: https://i.stack.imgur.com/J5wzj.jpg

While this succeeds, and prints even numbers with natural sqaure root

for (i = 2; i <= x; i++) 
{
   a = sqrt(i);
   aa = a * a;
   if (aa == i && ((i/2) *2) == i)
        printf("%d ", i);
}

source: https://i.stack.imgur.com/ufn6B.jpg

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.

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…

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.

Thank you,
Adam

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T04:34:24+00:00Added an answer on June 2, 2026 at 4:34 am

    your code in ‘that’ includes:

    if (aa == i);
    //          ^
       printf(...);
    

    [note the ; at the end of the if condition]

    Thus, if aa == i – an empty statement happens, and the print always occures, because it is out of the scope of the if statement.

    To avoid this issue in the future, you might want to use explicit scoping1 [using {, } after control flow statements] – at least during your first steps of programming the language.


    1: spartan programmers will probably hate this statement

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hello Guys I am trying to figure out why i am gettings this error
hello i'm studying iOS programming i created a project, which is an empty application
Hello can anybody solve this please I'm creating the object in the action class
Hello World :) I'm trying to write MIXINS for BOX-SHADOW property using SASS like
Hello I have this query... if (isset($_REQUEST['deletePost'])) { $q = $dbc -> prepare(DELETE FROM
Hello I'm trying to take backup from mysql command line client. I'm using mysqldump
Hello friends a very novice question as I am very new to programming. I
Hello I am building a website using JSF 2.0. I am trying to set
Hello i am just Studying 3-tier Application. I am now having a problem with
I am studying the Windows programming by using the book Programming.Windows.5th.Edition(Charles Petzold). When I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.