# last digit ending with 7

Hi,
I am trying below challenge

http://codingbat.com/prob/p125339

I wrote as below

public boolean lastDigit(int a, int b) {
if(a%10==7 && b%7==7){
return true;
}
return false;

}

I have test cases failing as below
Expected      Run
lastDigit(7, 17) → true      false      X
lastDigit(6, 17) → false      false      OK
lastDigit(3, 113) → true      false      X
lastDigit(114, 113) → false      false      OK
lastDigit(114, 4) → true      false      X
lastDigit(10, 0) → true      false      X
lastDigit(11, 0) → false      false      OK

How to improve and fix my program
LVL 7
###### Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Author Commented:
``````public boolean lastDigit(int a, int b) {
if(a%10==7&& b%10==7){
return true;
}

if(a%10==7&& b%10==7){
return true;
}

if(a%10==1&& b%10==1){
return true;
}

if(a%10==2&& b%10==2){
return true;
}

if(a%10==3&& b%10==3){
return true;
}

if(a%10==7&& b%10==7){
return true;
}

if(a%10==4&& b%10==4){
return true;
}

if(a%10==5&& b%10==5){
return true;
}

if(a%10==6&& b%10==6){
return true;
}

if(a%10==8&& b%10==8){
return true;
}

if(a%10==9&& b%10==9){
return true;
}

if(a%10==0&& b%10==0){
return true;
}
return false;

}
``````

Expected      Run
lastDigit(7, 17) → true      true      OK
lastDigit(6, 17) → false      false      OK
lastDigit(3, 113) → true      true      OK
lastDigit(114, 113) → false      false      OK
lastDigit(114, 4) → true      true      OK
lastDigit(10, 0) → true      true      OK
lastDigit(11, 0) → false      false      OK

Actually i modifed the code but still looks lengthy code. Please advise how to simplify and improve my code above
Commented:
What is wrong with the solution given when you click on the "Show Solution" button(at posted link)?
Commented:
return a%10 == b%10;

Experts Exchange Solution brought to you by

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Author Commented:
I would like to work the solution myself and improve and correct my own mistakes. I am not looking at the solutions at this point as they seems too complex for me at times.
Commented:
return (a-b)%10 == 0;
Author Commented:

return(a % 10 == b % 10);

return (a-b)%10 == 0;
Commented:
a % 10 returns the remainder when you divide by 10.  So "15 % 10" is 5.

So (a % 10 == b % 10) is true when both a and b have the same remainder when divided by 10.  E.g 25 and 15.

(a - b) % 10 returns the remainder when you divide the difference between the two values by 10.

So if we go back to our example (25 and 15) then the difference is 10 and if we figure out the remainder when we divide that by 10, we'll get 0.

So they're just 2 equivalent ways of saying the same thing.
For my money "a % 10 == b % 10" is the clearer of the two.

Doug
Author Commented:
``````So if we go back to our example (25 and 15)
``````

instead if i pass (15 and 25) difference is -10(not positive number)
still the reminder does not effect right? it is still 0.