Assign to the variable boolean ‘primeSecond’ the value true if the second leading (just after the most significant) decimal digit of the value of an int variable ‘n’ is 2,3,5 or 7; otherwise assign ‘primeSecond’ the value false .

Assign to the variable boolean ‘primeSecond’ the value true if the second leading (just after the most significant) decimal digit of the value of an int variable ‘n’ is 2,3,5 or 7; otherwise assign ‘primeSecond’ the value false .

LANGUAGE: JAVA

CHALLENGE:

Assign to the variable boolean ‘primeSecond’ the value true if the second leading (just after the most significant) decimal digit of the value of an int variable ‘n’ is 2,3,5 or 7; otherwise assign ‘primeSecond’ the value false . Assume ‘primeSecond’ and ‘n’ are already declared and that ‘n’ has already been assigned a value . So if n’s value is 58047 primeSecond will be false because the second leading digit of 58047 is 8 which is not 2 or 3 or 5 or 7.

SOLUTION:

int digit = 0;
if (n < 9)
    primeSecond = false;
else
{
    digit = n;
    while (digit > 99)
    {
         digit = digit / 10;
    }
    digit = digit % 10;
    if (digit == 2 || digit == 3 || digit == 5 || digit == 7)
    {
        primeSecond = true;
    }
    else
    {
         primeSecond = false; 
    }
}

Add comment