Gandalf14141 Posted February 4, 2013 Share Posted February 4, 2013 I'm currently taking a Java programming class in college. I have the following 2 problems that I just can not figure out. 1. Given two variables number and lsd of type int, write an expression that assigns the least significant digit of number of lsd. For example, if number is 567, then lsd should get a value of 7. and 1. Given two variables number and secondDigit of type int, write a code fragment so that the variable secondDigit gets the second digit (from right) of number. For example, if number is 56487, then secondDigit should get a value of 8. [Remark: You are allowed to write multiple Java statements. However, there is a way to achieve this by just one Java statement. ] I have searched and have gotten nowhere. If someone would be able to at least guide me in the right direction I'll be able to handle the rest. Thanks! EDIT: I'm not asking for anyone to do it for me, I'm just asking for something so I'll be able to figure it out. EDIT2: I figured out the first one.[hide] public class numberfive { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub int number; int lsd; number = 567; lsd = number%10; System.out.println("Least Significant Digit = " + lsd); } } [/hide] Finally on here to update that I have officially quit! It's been fun.[hide=Signature]R.I.P Billy Mays and <3 My Friend C.D.S 7/8/09 <360,816th to 99 Fletching 03/07/09|220,309th Person to be Able to Kill Dusties | 10 Year Cape on 12/20/14[/hide] Link to comment Share on other sites More sharing options...
Hedgehog Posted February 4, 2013 Share Posted February 4, 2013 First one: lsd = number%10; Second one: secondDigit = (number/10)%10; The second one you just divide by 10 to make the 2nd to last digit the last digit. 1 Link to comment Share on other sites More sharing options...
Gandalf14141 Posted February 4, 2013 Author Share Posted February 4, 2013 Ah. Thank you very much! That makes sense! I was stumped so I was hoping someone would be willing to help on here. Finally on here to update that I have officially quit! It's been fun.[hide=Signature]R.I.P Billy Mays and <3 My Friend C.D.S 7/8/09 <360,816th to 99 Fletching 03/07/09|220,309th Person to be Able to Kill Dusties | 10 Year Cape on 12/20/14[/hide] Link to comment Share on other sites More sharing options...
SAA Posted April 16, 2013 Share Posted April 16, 2013 I'm currently taking a Java programming class in college. I have the following 2 problems that I just can not figure out. 1. Given two variables number and lsd of type int, write an expression that assigns the least significant digit of number of lsd. For example, if number is 567, then lsd should get a value of 7. and 1. Given two variables number and secondDigit of type int, write a code fragment so that the variable secondDigit gets the second digit (from right) of number. For example, if number is 56487, then secondDigit should get a value of 8. [Remark: You are allowed to write multiple Java statements. However, there is a way to achieve this by just one Java statement. ] I have searched and have gotten nowhere. If someone would be able to at least guide me in the right direction I'll be able to handle the rest. Thanks! EDIT: I'm not asking for anyone to do it for me, I'm just asking for something so I'll be able to figure it out. EDIT2: I figured out the first one.[hide] public class numberfive { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub int number; int lsd; number = 567; lsd = number%10; System.out.println("Least Significant Digit = " + lsd); } } [/hide]Ah. Thank you very much! That makes sense! I was stumped so I was hoping someone would be willing to help on here. You could also get the length/count and extract the number that way. Have an Android phone? Check out my Runescape apps:https://play.google.com/store/apps/developer?id=SAA Follow my app development Twitter here. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now