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

  • (2 Pages)
  • +
  • 1
  • 2

19 Replies - 619 Views - Last Post: 22 June 2013 - 06:33 PM Rate Topic: -----

#1 jasmin21patel  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • 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.

thank you in advance.
Is This A Good Question/Topic? 0
  • +

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

#2 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10462
  • View blog
  • Posts: 38,773
  • 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;


Was This Post Helpful? 1
  • +
  • -

#3 burakaltr  Icon User is offline

  • D.I.C Regular

Reputation: 91
  • View blog
  • Posts: 274
  • 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

View Postjasmin21patel, 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.

thank you in advance.


The Stringy way Spoiler...
Spoiler

Was This Post Helpful? -1
  • +
  • -

#4 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1390
  • View blog
  • Posts: 3,076
  • 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

View Postmacosxnerd101, 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

Was This Post Helpful? 2
  • +
  • -

#5 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10462
  • View blog
  • Posts: 38,773
  • 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

My bad on misunderstanding that!
Was This Post Helpful? 0
  • +
  • -

#6 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1390
  • View blog
  • Posts: 3,076
  • 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
Was This Post Helpful? 0
  • +
  • -

#7 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10462
  • View blog
  • Posts: 38,773
  • 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

Also, burakaltr, your code really isn't clean. You should adopt an indentation convention to make it easier to read and follow your code samples.
Was This Post Helpful? 0
  • +
  • -

#8 burakaltr  Icon User is offline

  • D.I.C Regular

Reputation: 91
  • View blog
  • Posts: 274
  • 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
Was This Post Helpful? 0
  • +
  • -

#9 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7641
  • View blog
  • Posts: 12,887
  • 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

View Postjasmin21patel, 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.

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?
Was This Post Helpful? 0
  • +
  • -

#10 jasmin21patel  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • 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

View PostCasiOo, on 19 June 2013 - 11:40 AM, said:

View Postmacosxnerd101, 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
Was This Post Helpful? 0
  • +
  • -

#11 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7641
  • View blog
  • Posts: 12,887
  • 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

Was This Post Helpful? 1
  • +
  • -

#12 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1390
  • View blog
  • Posts: 3,076
  • 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
Was This Post Helpful? 1
  • +
  • -

#13 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7641
  • View blog
  • Posts: 12,887
  • 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! :)
Was This Post Helpful? 0
  • +
  • -

#14 jasmin21patel  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • 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

View Postjasmin21patel, on 19 June 2013 - 01:00 PM, said:

View PostCasiOo, on 19 June 2013 - 11:40 AM, said:

View Postmacosxnerd101, 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
Was This Post Helpful? 0
  • +
  • -

#15 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7641
  • View blog
  • Posts: 12,887
  • 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?
Was This Post Helpful? 1
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2