How do I get this to print only the integer solutions?

If I wanted to adjust my code so that only integer solutions of x and i are displayed, how would I do that? For example, if x is 2 then x should be displayed along with i which is 52. However, if x is 4.3333 then I don't want to display x or i. If someone could help me out, I'd appreciate it.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
void solution()
{
        for(double i=0; i< 100; i++)
        {
            double x;
            x=(370 - 7 * i)/3;
            if(x>=0)
            {
                    cout << x << " " << i << "\n";
            }
        }
}

int main()
{
    solution();
}
Last edited on
1
2
3
4
5
6
7
8
9
10
11
void solution()
{
    // ( 370 - 7*i ) >= 0 ; 7*i <= 370 ; i <= 370/7
    for( int i = 0 ; i <= (370/7) ; ++i )
    {
        // x == ( 370 - 7*i ) / 3 ;
        const int x_times_3 = 370 - 7*i ;
        if( x_times_3%3 == 0 ) // dividible by 3
            std::cout << "x == " << x_times_3/3 << "  i == " << i << '\n' ;
    }
}
as a side note, loops with a double loop variable have risks of not doing what you want. the comparisons can be tricky (esp for equality) to get right. It works, but be aware.
Topic archived. No new replies allowed.