19 Replies  691 Views  Last Post: 22 June 2013  06:33 PM
#1
i need some guidance on how to add digit at odd position together.
Posted 19 June 2013  10:49 AM
thank you in advance.
Replies To: i need some guidance on how to add digit at odd position together.
#2
Re: i need some guidance on how to add digit at odd position together.
Posted 19 June 2013  10:52 AM
int a = 123456; a = a + 135; //or a += 135;
#3
Re: i need some guidance on how to add digit at odd position together.
Posted 19 June 2013  11:17 AM
jasmin21patel, on 19 June 2013  05:49 PM, said:
thank you in advance.
The Stringy way Spoiler...
#4
Re: i need some guidance on how to add digit at odd position together.
Posted 19 June 2013  11:40 AM
macosxnerd101, on 19 June 2013  05:52 PM, said:
int a = 123456; a = a + 135; //or a += 135;
I think he meant to add every every 2nd digit for any given integer
So it should be int result = 1 + 3 + 5 and not 135
At least that's what I think he meant... Not exactly what he wrote though
Like
(Yes there is a bug in this example at the number % 2 part)
int number = 123456; int result = 0; if (number % 2 == 0) number /= 10; while (number > 0) { result += number % 10; number /= 100; } System.out.println(result);
Or the char way
String number = "123456"; int result = 0; for (int i=0; i<number.length(); i+=2) result += number.charAt(i)  '0'; System.out.println(result);
This post has been edited by CasiOo: 19 June 2013  02:08 PM
#5
Re: i need some guidance on how to add digit at odd position together.
Posted 19 June 2013  11:46 AM
#6
Re: i need some guidance on how to add digit at odd position together.
Posted 19 June 2013  11:46 AM
if(i%2==0)sum+=Integer.parseInt(String.valueOf(str.charAt(i)));
Too much converting back and forth
The compiler already treats the char as an integer, so no need to convert it in any way
#8
Re: i need some guidance on how to add digit at odd position together.
Posted 19 June 2013  12:07 PM
My spoiler pretends to be an Answer. All the OP needs is to determine the digits of the number, by consecutive modular arithmetics and subtractions, Really.
I agree on the too backandforth methodology in my wouldbesol'n
#9
Re: i need some guidance on how to add digit at odd position together.
Posted 19 June 2013  12:19 PM
jasmin21patel, on 19 June 2013  12:49 PM, said:
thank you in advance.
There are a number of ways to go about this. I'm betting that you've got this assignment because someone expects that you can do it, so I have some confidence that you can manage to come up with a way to do it  or at least some idea.
If you can't think of the whole thing, at least think of how to start it. For instance, how could you just get the last digit of the number?
#10
Re: i need some guidance on how to add digit at odd position together.
Posted 19 June 2013  01:00 PM
CasiOo, on 19 June 2013  11:40 AM, said:
macosxnerd101, on 19 June 2013  05:52 PM, said:
int a = 123456; a = a + 135; //or a += 135;
I think he meant to add every every 2nd digit for any given integer
So it should be int result = 1 + 3 + 5 and not 135
At least that's what I think he meant... Not exactly what he wrote though />
Like
int number = 123456; int result = 0; if (number % 2 == 0) number /= 10; while (number > 0) { result += number % 10; number /= 100; } System.out.println(result);
Or the char way
String number = "123456"; int result = 0; for (int i=0; i<number.length(); i+=2) result += number.charAt(i)  '0'; System.out.println(result);
thank you so much for your correction to my question and yes u understand totaly right
#11
Re: i need some guidance on how to add digit at odd position together.
Posted 19 June 2013  01:39 PM
int number = 123456; int result = 0; if (number % 2 == 0) number /= 10; while (number > 0) { result += number % 10; number /= 100; } System.out.println(result);
Um, there's two bugs here. First, why chop off the last digit of even numbers?
Second, this fails in half the cases  the numbers are numbered from the mostsignificant digit, so depending on whether this number is even or odd (see bug #1) this fails if the number has an odd or an even number of digits. Or is it the other way around? In any case, half the time.
Since we're in "let's do the homework" mode, here's a cute approach:
public int addOddDigits(int number) { int resultA= 0; int resultB= 0; while (number > 0) { resultA += number % 10; number /= 10; if (number == 0) return resultA; resultB += number % 10; number /= 10; } return resultB; }
@OP: since you're getting this handed to you, I'd like to have you explain what it does and why it works, and how you know it works correctly.
This post has been edited by jon.kiparsky: 19 June 2013  01:41 PM
#12
Re: i need some guidance on how to add digit at odd position together.
Posted 19 June 2013  02:07 PM
Quote
It just makes the while loop more neat when starting with an odd number of digits
And yes I made a mistake, it shouldn't be number % 2 It just happened to work when I ran the code once haha
I guess you could use log10 or convert it to a String to get the number of digits if you want my first example to actually work
#13
Re: i need some guidance on how to add digit at odd position together.
Posted 19 June 2013  02:16 PM
ugh.
Easy enough to make it work, but explaining it to a novice sounds like it would be painful!
#14
Re: i need some guidance on how to add digit at odd position together.
Posted 19 June 2013  03:25 PM
jasmin21patel, on 19 June 2013  01:00 PM, said:
CasiOo, on 19 June 2013  11:40 AM, said:
macosxnerd101, on 19 June 2013  05:52 PM, said:
int a = 123456; a = a + 135; //or a += 135;
I think he meant to add every every 2nd digit for any given integer
So it should be int result = 1 + 3 + 5 and not 135
At least that's what I think he meant... Not exactly what he wrote though />/>
Like
int number = 123456; int result = 0; if (number % 2 == 0) number /= 10; while (number > 0) { result += number % 10; number /= 100; } System.out.println(result);
Or the char way
String number = "123456"; int result = 0; for (int i=0; i<number.length(); i+=2) result += number.charAt(i)  '0'; System.out.println(result);
thank you so much for your correction to my question and yes u understand totaly right
what does this 0 mean in this statement(result += number.charAt(i)  '0' in the for loop
#15
Re: i need some guidance on how to add digit at odd position together.
Posted 19 June 2013  08:00 PM
Now since '0' is a number, if you subtract it from itself, you get actually really honestly zero, right? '0'  '0' = 0. Doesn't matter what number '0' is on the ASCII table, this is always true.
So, if the next symbol after '0' is '1', what do you get if you subtract '1' from '0'?
And now do you know what number.charAt(i)  '0' means?
