Find the product

This is currently what I am working on and hopefully you can follow. I am trying to make sure the product is not zero.

Running this just keeps repeating zero so something is in the wrong place (line 15?)
and Im being told I need to declare "rem" even though I think I have with int rem = i % 10;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>


using namespace std;

void perfectSquares(float l, float r)
{
    for (int i = l; i <= r; i++) {

        if (sqrt(i) == (int)sqrt(i) && i%63 == 0)

        int rem;
            int rem = i % 10;
                int prod = prod * rem;
                    i= i/10;
                    cout << i << "\n";
    }
}

int main(){
    int l = 0001, r = 9999;
    perfectSquares(l, r);

    return 0;
}
> int rem;
> int rem = i % 10;
You declared it twice.
One of them isn't needed.

> i= i/10;
Doing this is going to mess with this.
> for (int i = l; i <= r; i++)
You're not going to advance to the end condition one at a time, if you keep dividing by 10.
Ok thank you

I thought it was being declared twice.

Ill have another look at the second comment.
Note that sqrt() returns type double. Because of the 'imprecise' storage of double values, it's not recommended to use equality testing The sqrt() of 1522756 is 1234 - but with type double that might be 1234.00000001 etc - which wouldn't compare equal to 1234.

Often you would subtract the two values and if the absolute result is less than a certain value (EPSILON is often used) then they are considered equal.

See https://en.cppreference.com/w/cpp/types/numeric_limits/epsilon and the example of comparing.

you have
if(..)
int rem; //why create this variable conditionally? I don't think this if does what you think it does.
you may want some {} around that if statement, or to move rem away from the if (above it somewhere).

unnecessary variable anyway. just say
prod = prod * (i%10);
just declare prod outside the loop too, IMHO. you can make it work like you have it but that seems strange to me (its ok here because you just want to print it and throw it away, normally you want to keep it and return it or something, else why compute it?)

leading zeros don't mean what you think either. line 21 is not a good idea just say 1

its faster, and easier, to generate perfect squares than to try to check them with floating point problems and such
for(int i = 0; i < sqrt(target+1); i++) ///loops far less times.
{
use i*i as the found perfect square.
}

if its intentional and you know what you are doing (I do not?) ... ok But modification of the loop variable i (15) is hard to read / understand and debug. I don't feel like figuring out if your loop will ever end and if its doing extra work and if that is correct etc. Its either a bug or genius.
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
#include <cmath>

void perfectSquares( int L, int R )
{
   int imin = sqrt( L - 0.1 ) + 1, imax = sqrt( R + 0.1 );
   for ( int i = imin; i <= imax; i++ ) std::cout << i * i << '\n';
}

int main()
{
   perfectSquares( 1, 9999 );
}
Last edited on
Topic archived. No new replies allowed.