Java int addition - the long way

In summary, the conversation is about finding a way to split up integers in order to do addition in Java. One suggestion is to access the different digits of an int by using integer division and modulo operations. It is mentioned that this method can also work with bases other than 10.
  • #1
NiaSphinx
4
0
Hi guys,

I'm trying to figure out a way to split up integers so I can do addition the old school way in Java.
E.g.:
1234
1234
+_____
2468

So you know start at the 4's and then add them up and move left etc.
Does anyone have any ideas on how I access the different digits of an int?

Thanks
 
Technology news on Phys.org
  • #2
I don't know if there is some nice routine for this, but there is always the oldfashioned dirty way: to access the 10n digit (i.e. for n = 0 you get the units, for n = 1 the tens, etc) you can integer-divide by 10n and then take the result modulo 10.
For example:

1234 / 100 = 12 (as integer division produces an integer by chopping off the decimal part)
12 % 10 = 2
 
  • #3
Ah that does sound like a good way of doing it.
Can it work the same if the base is not 10?
 
  • #4
I suppose in base b, one can still obtain the digits at the nth position (starting from n = 0 on the right) of a number N as

[tex]\operatorname{digit}_n(N) = \left[ N / b^n \right] \text{ mod } b [/tex]
where the square brackets denote the floor of the division.
 

Related to Java int addition - the long way

1. What is Java int addition?

Java int addition is the process of adding two integer values together using the programming language Java. An integer, or int, is a whole number without any decimals, and addition is the mathematical operation of combining two or more numbers.

2. How do you perform Java int addition?

To perform Java int addition, you need to use the "+" operator between two integer values. For example, if you want to add the numbers 5 and 7, you would use the code "int result = 5 + 7;". The result of this addition would be stored in the variable "result".

3. Why is it called "the long way"?

"The long way" refers to the method of manually adding two integers in Java by using the "+" operator. This is considered the long way because there are other methods, such as using the "sum" method or the "addExact" method, that can perform Java int addition more efficiently.

4. What are the benefits of using "the long way" for Java int addition?

One benefit of using "the long way" for Java int addition is that it is easy to understand and implement. It is also a good way to practice basic programming concepts and improve your skills. Additionally, this method allows for more control over the addition process, as you can customize the code to fit your specific needs.

5. Are there any drawbacks to using "the long way" for Java int addition?

One drawback of using "the long way" for Java int addition is that it can be time-consuming and tedious, especially when working with a large number of values. Additionally, there is a risk of making mistakes when manually adding integers, which can lead to errors in your code. It is also not the most efficient method for performing Java int addition, as there are other methods that can achieve the same result with less code.

Similar threads

  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
4
Views
4K
  • Programming and Computer Science
Replies
29
Views
2K
  • Programming and Computer Science
Replies
1
Views
3K
  • Programming and Computer Science
Replies
5
Views
1K
  • Programming and Computer Science
Replies
2
Views
2K
  • Programming and Computer Science
Replies
9
Views
7K
  • Programming and Computer Science
Replies
8
Views
911
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
9
Views
2K
Back
Top