Newb Java Question

Started by
3 comments, last by Zahlman 17 years, 7 months ago
I need to take an integer such as 1234 and add the digits of the integer. After declaring the integer, how do I use specific digits of it? For example, I have int x = 1234. I need int y to equal the sum of the digits, in this case, 10. Help? I'd really appreciate it. [Edited by - furinto on September 17, 2006 9:31:49 AM]
Advertisement
This sounds a bit like homework, which is against our policies for this forum. Have you tried thinking about the steps you would take to get the values you want?

Some things to consider:

1.) If you only have one digit, the answer is that digit (e.g., the sum of the digits of the number 7 is 7).

2.) If you have n digits, the answer is the last digit plus the sum of the remaining n-1 digits.

Note that statement [2] suggests that an easy recursive solution; just take the last digit and re-call your own method with the shortened number. For instance, if the number is 67281, you would call SumOfDigits(67281), which would result in a call to SumOfDigits(1) + SumOfDigits(6728), which would become 1 + SumOfDigits(8) + SumOfDigits(672), and so on.

hope that helps,
- k2"Choose a job you love, and you'll never have to work a day in your life." — Confucius"Logic will get you from A to B. Imagination will get you everywhere." — Albert Einstein"Money is the most egalitarian force in society. It confers power on whoever holds it." — Roger Starr{General Programming Forum FAQ} | {Blog/Journal} | {[email=kkaitan at gmail dot com]e-mail me[/email]} | {excellent webhosting}
You should do a little research on the % and / operators.

That should get you started with coming up with an algorithm.

-JR
Two ways I can think off.

	public int addUp(int i)	{		int num =0;		int div = 1;		if(i == 0) return num;		while(true)		{			if(i / div < 10)			{				num += i /div;				num += addUp(i - (div * (i / div)));				break;			}			div *= 10;		}		return num;	}

	public int sAddUp(int i)	{		String s = Integer.toString(i);		int num = 0;				for(int t=0; t< s.length();t++)			num += Character.digit(s.charAt(t),10);		return num;	}


Both these functions should return the sum of the digits in the integer passed in to them. There is probably better ways but this is all I could think of at the top of my head.

[Edited by - clearz on September 17, 2006 7:48:48 AM]
0) "Newb questions" are best placed in For Beginners (it appears at the top of the forum listing for a reason; so it will be noticed).

1) We don't do homework here.

2) Your question, being about the algorithm, isn't really Java-specific.

3) How do you access digits of a number when you do it yourself on paper? What, just look at it? OK, smart guy, how will you get digits of the number as represented in a different base? :) Think about how bases and numerical representations work, and you should be able to figure out how you *really* extract this information (without just "looking").

This topic is closed to new replies.

Advertisement