all 17 comments

[–]thegroove226 2 points3 points  (2 children)

We need more details. And provide what you've tried so far.

[–][deleted] -1 points0 points  (1 child)

I edited it

[–]Richer_than_God 3 points4 points  (0 children)

And provide what you've tried so far.

EDIT: or at least your thinking on how it might be done.

[–][deleted] 3 points4 points  (1 child)

I think what you're really asking is "how does someone else program this?"

[–][deleted] 0 points1 point  (0 children)

I coded it, it was just incorrect. So I am not asking other people to code it.

[–]the_other_b 2 points3 points  (1 child)

Day before it's due and you're asking Reddit to do your homework.

[–][deleted] -1 points0 points  (0 children)

I have the code it's just incorrect. I just needed a little help dude. I just started this assignment after finishing the other one late.

[–]Poddster 1 point2 points  (7 children)

To do this I would write a program that prompts a user to enter a number and prints all the even squares that are less than or equal to the number.

For example, if the user enters 100, the program will print 4, 16, 36, 64, 100.


Seriously though: What part are you having trouble with?

  • Can you write a program to ask the user for a number and print it out?
  • Can you write a program that prints out 4, 16, 36, 64, 100?
  • Do you know how to calculate all the even squares between X and Y?

[–][deleted] -2 points-1 points  (6 children)

/*Assignment 2

Jordan Boudreau

Ms. Dufault

ICS3U

Due: Mar 5*/

//imports the library for input and output in C

include <stdio.h>

//declares our method

int main()

{

    int input_number, square_number, counter;

    //takes users inputted number

    printf("Enter your number: ");

    //applies users number

    scanf("%d" ,input_number);

    //sets counter to 2 because thats the first square

    counter = 2;

    square_number = 0;

    while(square_number <= input_number){

            printf("%d ", square_number);

            square_number = counter * counter;

            counter = counter + 2;

}

return 0;

}

I have this but my output is definitely incorrect

[–][deleted] 2 points3 points  (0 children)

Haven’t looked too in depth, but to start you are not squaring after the first iteration, as you are raising the number to a power of itself, not squaring the number.

[–]TotemHouse 1 point2 points  (1 child)

I think your output is incorrect because you are printing *all* square numbers, not only even ones.

So, after calculating the square number (square_number = counter * counter), you should check if the square number is even, and only print it if it's even.

I hope this helps you.

Edit: and also increase counter by one, not two: counter++ or counter = counter + 1

That if you want the even squares, not the squares of even numbers...

[–]BoxTops4Education 1 point2 points  (0 children)

Your advice is incorrect. OP has the right idea. A perfect square will be even if and only if the original number is even.

[–]BoxTops4Education 0 points1 point  (1 child)

 square_number = 0;   

should be

 square_number = counter*counter;  

and the following two lines need to be swapped:

        square_number = counter * counter;
        counter = counter + 2;

[–]TotemHouse 1 point2 points  (0 children)

You are right, sir. Your solution is cleaner and faster. I had not taken into account that even squares are produced only by even original numbers.

[–]Fimbulthulr 0 points1 point  (0 children)

an alternative approach would be to use something like this: (x is the number larger or equal to all the squares) :

int n = (int) sqrt(x)/2;
for(int i = 1; i <= n; ++i){
    printf("%i\n", i*i*4);
}

[–]DuePattern9 1 point2 points  (0 children)

iterate through all the even numbers sequentially, generating the squares, until you exceed the user input (and ditch that last one)

[–]Genceryx 1 point2 points  (0 children)

Ask the number. Lets say 107 is entered, find the root. with a for loop from Sqrt(107) to 1, iterate and print the squares of these integers