Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

last digit ending with 7

Posted on 2014-10-10
9
Medium Priority
?
137 Views
Last Modified: 2014-10-11
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
0
Comment
Question by:gudii9
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
9 Comments
 
LVL 7

Author Comment

by:gudii9
ID: 40373137
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;

  
}

Open in new window



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
0
 
LVL 28

Expert Comment

by:rrz
ID: 40373181
What is wrong with the solution given when you click on the "Show Solution" button(at posted link)?
0
 
LVL 84

Accepted Solution

by:
ozo earned 1400 total points
ID: 40373188
return a%10 == b%10;
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 7

Author Comment

by:gudii9
ID: 40373198
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.
0
 
LVL 84

Expert Comment

by:ozo
ID: 40373213
return (a-b)%10 == 0;
0
 
LVL 7

Author Comment

by:gudii9
ID: 40373565
How both below statements are same? Please advise

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

return (a-b)%10 == 0;
0
 
LVL 28

Assisted Solution

by:dpearson
dpearson earned 600 total points
ID: 40374075
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
0
 
LVL 7

Author Comment

by:gudii9
ID: 40375100
So if we go back to our example (25 and 15)

Open in new window


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

Please advise
0
 
LVL 84

Expert Comment

by:ozo
ID: 40375135
There are differences in how various languages handle % for negative integers, but all of them should agree when the answer is 0
0

Featured Post

The top UI technologies you need to be aware of

An important part of the job as a front-end developer is to stay up to date and in contact with new tools, trends and workflows. That’s why you cannot miss this upcoming webinar to explore the latest trends in UI technologies!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Having just graduated from college and entered the workforce, I don’t find myself always using the tools and programs I grew accustomed to over the past four years. However, there is one program I continually find myself reverting back to…R.   So …
This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
Suggested Courses

715 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question