i need some guidance on how to add digit at odd position together.

• (2 Pages)
• 1
• 2

19 Replies - 1279 Views - Last Post: 22 June 2013 - 06:33 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=323261&amp;s=7d5b2844fccd39f942858bca3ebe0f92&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

#1 jasmin21patel

Reputation: -1
• Posts: 20
• Joined: 19-November 09

i need some guidance on how to add digit at odd position together.

Posted 19 June 2013 - 10:49 AM

as for example if i have int a = 123456 how can i add 135. i dont have code because i can not think how to do it so some guidance will be very helpful if you will explain through code that would be big help.

Is This A Good Question/Topic? 0

Replies To: i need some guidance on how to add digit at odd position together.

#2 macosxnerd101

• Games, Graphs, and Auctions

Reputation: 12298
• Posts: 45,399
• Joined: 27-December 08

Re: i need some guidance on how to add digit at odd position together.

Posted 19 June 2013 - 10:52 AM

This is pretty basic. Have you not bothered reading your Java book or anything on the syntax...?
```int a = 123456;
a = a + 135; //or a += 135;

```

#3 burakaltr

• D.I.C Regular

Reputation: 91
• Posts: 280
• Joined: 07-November 10

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:

as for example if i have int a = 123456 how can i add 135. i dont have code because i can not think how to do it so some guidance will be very helpful if you will explain through code that would be big help.

The Stringy way Spoiler...
Spoiler

#4 CasiOo

• D.I.C Lover

Reputation: 1576
• Posts: 3,548
• Joined: 05-April 11

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:

This is pretty basic. Have you not bothered reading your Java book or anything on the syntax...?
```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 macosxnerd101

• Games, Graphs, and Auctions

Reputation: 12298
• Posts: 45,399
• Joined: 27-December 08

Re: i need some guidance on how to add digit at odd position together.

Posted 19 June 2013 - 11:46 AM

#6 CasiOo

• D.I.C Lover

Reputation: 1576
• Posts: 3,548
• Joined: 05-April 11

Re: i need some guidance on how to add digit at odd position together.

Posted 19 June 2013 - 11:46 AM

burakaltr this is just too complicated
```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

#7 macosxnerd101

• Games, Graphs, and Auctions

Reputation: 12298
• Posts: 45,399
• Joined: 27-December 08

Re: i need some guidance on how to add digit at odd position together.

Posted 19 June 2013 - 11:52 AM

#8 burakaltr

• D.I.C Regular

Reputation: 91
• Posts: 280
• Joined: 07-November 10

Re: i need some guidance on how to add digit at odd position together.

Posted 19 June 2013 - 12:07 PM

I am all too Sorry !

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 back-and-forth methodology in my would-be-sol'n

#9 jon.kiparsky

• Beginner

Reputation: 11040
• Posts: 18,852
• Joined: 19-March 11

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:

as for example if i have int a = 123456 how can i add 135. i dont have code because i can not think how to do it so some guidance will be very helpful if you will explain through code that would be big help.

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 jasmin21patel

Reputation: -1
• Posts: 20
• Joined: 19-November 09

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:

This is pretty basic. Have you not bothered reading your Java book or anything on the syntax...?
```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 jon.kiparsky

• Beginner

Reputation: 11040
• Posts: 18,852
• Joined: 19-March 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 most-significant 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 CasiOo

• D.I.C Lover

Reputation: 1576
• Posts: 3,548
• Joined: 05-April 11

Re: i need some guidance on how to add digit at odd position together.

Posted 19 June 2013 - 02:07 PM

Quote

First, why chop off the last digit of even numbers?

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 jon.kiparsky

• Beginner

Reputation: 11040
• Posts: 18,852
• Joined: 19-March 11

Re: i need some guidance on how to add digit at odd position together.

Posted 19 June 2013 - 02:16 PM

Yeah, using log 10 would work - I'd have to stop and think about exactly how it would work, though. Log base 10 of 100 is 2, so log base 10 of 99 is just under 2, so it's 1 when you convert it to an int, so int(Math.log(number, 10)) is odd if number has an even number of digits so I chop off the least-significant digit...
ugh.

Easy enough to make it work, but explaining it to a novice sounds like it would be painful!

#14 jasmin21patel

Reputation: -1
• Posts: 20
• Joined: 19-November 09

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:

This is pretty basic. Have you not bothered reading your Java book or anything on the syntax...?
```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 jon.kiparsky

• Beginner

Reputation: 11040
• Posts: 18,852
• Joined: 19-March 11

Re: i need some guidance on how to add digit at odd position together.

Posted 19 June 2013 - 08:00 PM

0 is a number - you can add any number to it and get that number, you can multiply by it and get 0 back, and all that good stuff. '0' is a character. You can print it to the screen, and you can concatenate it onto a String and that sort of thing. They're different. But '0' is also a number - it's got a value on an ASCII table, you can look it up. That number is not the number that you can add any number to and get that same number, though, it's some ordinary boring number that's so boring I don't even care what it actually is. The only thing interesting about it is that it's followed, in order, by the characters representing the other digits - '1', '2', etc.
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?