Java > Warmup-1 > lastDigit (CodingBat Solution)

Problem:

Given two non-negative int values, return true if they have the same last digit, such as with 27 and 57. Note that the % "mod" operator computes remainders, so 17 % 10 is 7.

lastDigit(7, 17) → true
lastDigit(6, 17) → false
lastDigit(3, 113) → true


Solution:

public boolean lastDigit(int a, int b) {
  String strA = Integer.toString(a);
  String strB = Integer.toString(b);
  
  if (strA.charAt(strA.length() - 1) == strB.charAt(strB.length() - 1))
    return true;
  else
    return false;
}


No comments :

Post a Comment

Follow Me

If you like our content, feel free to follow me to stay updated.

Subscribe

Enter your email address:

We hate spam as much as you do.

Upload Material

Got an exam, project, tutorial video, exercise, solutions, unsolved problem, question, solution manual? We are open to any coding material. Why not upload?

Upload

Copyright © 2012 - 2014 Java Problems  --  About  --  Attribution  --  Privacy Policy  --  Terms of Use  --  Contact