For a given integer N, print all the squares of the natural numbers not exceeding N in ascending order. Input Format A natural number is entered. Output Output the answer to the problem.

Sample Input: 50

Sample Output: 1 4 9 16 25 36 49

 #include <iostream> #include <cmath> using namespace std; int main() { int a, b = 1; cin >> a; while (b <= a) { if (float(sqrt(b)) % 1 == 0) { cout << b; } b++; } return 0; } 

Closed due to the fact that off-topic participants Abyx , Dmitriy Simushev , Vladimir Martyanov , Igor , αλεχολυτ 26 Oct '16 at 8:02 .

It seems that this question does not correspond to the subject of the site. Those who voted to close it indicated the following reason:

  • “Questions asking for help with debugging (“ why does this code not work? ”) Should include the desired behavior, a specific problem or error, and a minimum code for playing it right in the question . Questions without an explicit description of the problem are useless for other visitors. See How to create minimal, self-sufficient and reproducible example . " - Abyx, Dmitriy Simushev, Vladimir Martyanov, Igor, αλεχολυτ
If the question can be reformulated according to the rules set out in the certificate , edit it .

  • For a given integer N, print all the squares of the natural numbers not exceeding N in ascending order. Input Format A natural number is entered. Output Output the answer to the problem. Sample Input: 50 Sample Output: 1 4 9 16 25 36 49 - Max
  • This program condition - Max
  • Transfer the text of the condition to the question. - PinkTux

4 answers 4

What this line means is a mystery to me.

if (float(sqrt(b)) % 1 == 0) {

Find the remainder of dividing by 1 the square root of a number. What do you want to say? Wanted to check whether the number is an integer? This is done wrong.

 if (1 - floor(sqrt(b)) < 1e-6) { 

And your problem is solved so

 int b = 1; int c = 1; while (c <= a) { cout << c; b++; c = b * b; } 

    In C ++, the floating point operator % not defined for floating-point numbers.

    Therefore, the condition in this clause is if

     if (float(sqrt(b)) % 1 == 0) { 

    wrong.

    It will be easier to record the next cycle.

     unsigned int a; std::cin >> a; for ( unsigned int i = 1, b; ( b = i * i ) <= a; i++ ) std::cout << b << ' '; std::cout << std::endl; 

    There is no need to include the cmath header and use the sqrt function declared in it.

    The program can be made more meaningful. For example,

     #include <iostream> int main() { while ( true ) { std::cout << "Enter a non-negative number (0 - exit): "; unsigned int a; if ( not ( std::cin >> a ) || a == 0 ) break; for ( unsigned int i = 1, b; ( b = i * i ) <= a; i++ ) std::cout << b << ' '; std::cout << std::endl; } return 0; } 

    The dialogue with the program may look something like this.

     Enter a non-negative number (0 - exit): 100 1 4 9 16 25 36 49 64 81 100 Enter a non-negative number (0 - exit): 50 1 4 9 16 25 36 49 Enter a non-negative number (0 - exit): 25 1 4 9 16 25 Enter a non-negative number (0 - exit): 0 

      In my opinion, the biggest mistake is to solve your problem by iterating through all the numbers and find out if this is not a square. It is better to go in a different direction and display the squares alternately, as long as they are less than the requested a (the code is specially simplified to make it clearer):

       #include <iostream> using namespace std; int main() { int a,b = 1; cin >> a; while(b*b <= a) { cout << b*b << " "; ++b; } cout << endl; } 
         #include <iostream> #include <cmath> using namespace std; int main() { int a,sqr = 1, b = 1; cin >> a; while (sqr <= a) { cout << sqr << " "; b++; sqr = b * b;; } return 0; } 

        conclusion:

         50 1 4 9 16 25 36 49 500 1 4 9 16 25 36 49 64 81 100 121 144 169 196 225 256 289 324 361 400 441 484 
        • I do not know who put you a positive answer, but your code is incorrect. - Vlad from Moscow
        • @VladfromMoscow Thanks, I understood the error and corrected. are you talking about sqrt? - Senior Pomidor
        • I'm talking about the output, which was obtained using sqrt. But now the program you have corrected will also give an incorrect conclusion. - Vlad from Moscow
        • In fact, "squares" and "powers of two" are "two big differences" ... - Harry