Home > front end >  Getting unexpected output when writing a program to find the first perfect square with two odd endin
Getting unexpected output when writing a program to find the first perfect square with two odd endin

Time:09-16

I'm a beginner taking a C class. Thanks for your help. So I have to write a program that finds the first perfect square which ALSO has two odd digits, and it doesn't produce the expected output.

#include <iostream>
#include <cmath>
using namespace std;

int main()
{
    for (int i = 1; i < 1000; i  )
    {
        // 1. Iterate from 1 to 100 and check which number is a perfect square (root is integer)
        int sqrRoot = sqrt(i);
        if (sqrRoot * sqrRoot == i)
        // 2. We need to find the FIRST perfect square who last digit is odd
        {
            int lastDigit = i % 10;
            int secondToLastDigit = i % 100;
     
                if(lastDigit % 2 != 0 && secondToLastDigit % 2 != 0)
                {
                    cout << "Found it. The first perfect square with two odd ending digits is " << i << "\n."; 
                }
            }
        // we printed out every perfect square...
    }
}

First, I created a for loop to check from 1 to 1000 to find the first perfect squares in the range. Then in the for loop I tested for it being a perfect square or not in an if statement. Then I created two temporary variables in the for loop, lastDigit and secondtoLastDigit which 'grabs' the last most and second to last most digit using the mod 10 'trick.' Then I check if those two digits are odd or not, and if they are, then print out the perfect square that has ending two odd digits.

Then, when I run it, I noticed I got a bunch of outputs:

shahjacob@lenovoLegion7:~/cs211$ ./a.out
Found it. The first perfect square with two odd ending digits is 25
.Found it. The first perfect square with two odd ending digits is 49
.Found it. The first perfect square with two odd ending digits is 81
.Found it. The first perfect square with two odd ending digits is 121
.Found it. The first perfect square with two odd ending digits is 169
.Found it. The first perfect square with two odd ending digits is 225
.Found it. The first perfect square with two odd ending digits is 289
.Found it. The first perfect square with two odd ending digits is 361
.Found it. The first perfect square with two odd ending digits is 441
.Found it. The first perfect square with two odd ending digits is 529
.Found it. The first perfect square with two odd ending digits is 625
.Found it. The first perfect square with two odd ending digits is 729
.Found it. The first perfect square with two odd ending digits is 841
.Found it. The first perfect square with two odd ending digits is 961

But I was confused first, since some of these outputs are unexpected. First, 121 literally has an even second to last digit. So does 225. Second, why does it not just print the first one?

Please help

CodePudding user response:

int secondToLastDigit = i % 100; gives two last digits. Thus lastDigit % 2 != 0 && secondToLastDigit % 2 != 0 tests are equivalent tests of the odd number.

Example with 121: secondToLastDigit is 1, secondToLastDigit is 21. 21 % 2 is 1 % 2 is 1.

You might want int secondToLastDigit = (i / 10) % 10;.

  • Related