Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Assign to the variable boolean \'primeSecond\' the value true if the second lead

ID: 3642524 • Letter: A

Question

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.

Instructor's notes: must use loops and division (/) and modulus (%)

Explanation / Answer

please rate

public class PrimeSecond {
    public static void main(String[] args){
        int n = 58047;
        boolean primeSecond = false;
        int digit=0;
        int prevDigit=0;

       // This is used to get value of each digit from last. PrevDigit stores value that digit previously had
        while(n>0){
            prevDigit = digit;
            digit = n % 10;           
            n = n/10;
        }
        if(prevDigit==2||prevDigit==3||prevDigit==5||prevDigit==7){
            primeSecond = true;
        }
        System.out.println(primeSecond);
    }
}