r/dailyprogrammer • u/Elite6809 1 1 • Dec 08 '14
[2014-12-8] Challenge #192 [Easy] Carry Adding
(Easy): Carry Adding
When you were first learning arithmetic, the way most people were tought to set out addition problems was like follows:
23+456=
23
+456
---
479
---Look familiar? And remember how, if the number went above 10, you put the number below the line:
559
+447
---
1006
---
11Those 1s under the line are called the carry values - they are 'carried' from one column into the next. In today's challenge, you will take some numbers, add them up and (most importantly) display the output like it is shown above.
Formal Inputs and Outputs
Input Description
You will accept a list of non-negative integers in the format:
559+447+13+102Such that the carry value will never be greater than 9.
Output Description
You are to output the result of the addition of the input numbers, in the format shown above.
Sample Inputs and Outputs
Sample Input
23+9+66Sample Output
23
9
66
--
98
--
1Sample Input
8765+305Sample Output
8765
305
----
9070
---
1 1Sample Input
12+34+56+78+90Sample Output
12
34
56
78
90
---
270
---
22Sample Input
999999+1Sample Output
999999
1
-------
1000000
-------
111111Extension
Extend your program to handle non-integer (ie. decimal) numbers.
2
u/thinksInCode Dec 09 '14
Here's a solution in Groovy:
BTW, what's with the second two being carried on 12+34+56+78+90? Unless I'm missing something obvious, only the 2 from the first column needs to be carried - there shouldn't need to be more than n - 1 carry digits, where n is the number of digits in the largest addend.
Is my output wrong? Note my carry digits are at the top, the way I learned in math class. :) 8+6+4+2 = 20, carry the two. 9+7+5+3+1 = 25, plus the 2 carry = 27. 270.
Groovy code here:
2
u/[deleted] Dec 09 '14 edited Dec 09 '14
bash/shellscript
this one doesn't output the carry addition table, but uses simple digit sums and carries internally.Sry, I re-read the challenge after I wrote the code, so I forgot the table-format part :'/ninja edit: Added the correct table code :D
naja edit #2: Fixed table code :D
Sample input and output:
2
u/Super_Cyan Dec 11 '14
Java
Really bloated, but I suck at programming (usually only do it for classes and tutorials). Also, I'm used to the carried number being on top, so that's how I made it. Can I have some feedback?
Output
193+1934+12349
2
u/Qnob Dec 17 '14 edited Dec 17 '14
I just finished an intro to Java class and I thought this would be fun to do. What can I improve on? (I try to not use library methods, etc, because I am still unfamiliar with them)
JAVA
7
u/jetRink Dec 08 '14 edited Dec 08 '14
Clojure
Builds the output sideways, then rotates 90°
6
u/Edward_H Dec 08 '14 edited Dec 08 '14
A COBOL solution, supporting up to 10
15-digit integersEDIT: numbers up to 1x1017 to 15 decimal places:6
u/programmingdaily Dec 09 '14
C#. First time post, I found this sub the other day and finally decided to take the plunge and make an account. Hopefully I got the formatting right.
4
u/bob13bob Dec 11 '14
Java
3
u/adrian17 1 4 Dec 08 '14 edited Dec 09 '14
Some fairly compact Python 3:
EDIT: with extension:
2
u/13467 1 1 Dec 08 '14 edited Dec 08 '14
Python 3
2
u/Acidom Dec 08 '14
Python
2
u/[deleted] Dec 09 '14 edited Dec 22 '18
deleted 0.1989 What is ^^^this?
2
u/kirsybuu 0 1 Dec 10 '14
D Language, with carries on top. I didn't do the official extension, but I do support arbitrarily large carry values.
Example:
2
u/vbfischer Dec 10 '14 edited Dec 10 '14
My first submission... Be gentle please..
Javascript/Node.js
Not too elegant but...
Output Example:
2
u/oasisguy Dec 14 '14
C++.
1
u/scripore Dec 09 '14
Ruby
Relatively new to programming, any feedback is appreciated
1
u/fvandepitte 0 0 Dec 09 '14
C#, it shows step by step what the carry is.
Result
1
u/chunes 1 2 Dec 09 '14
I wonder how many of these solutions break with the input 9+9+9+9+9+9+9+9+9+9+9+9.
1
u/undergroundmonorail Dec 09 '14
Python 2
1
u/marchelzo Dec 09 '14 edited Dec 09 '14
Haskell. Not as concise as I would have liked it to be, but it works, heh.
1
u/LaminatedSteel Dec 10 '14
First time poster! Here's my Ruby solution - criticism and feedback welcome.
1
u/Sakuya_Lv9 Dec 10 '14
Ruby 1.9.3
Comment will help
1
u/jonnywoh Dec 10 '14 edited Dec 10 '14
Python
1
u/frozensunshine 1 0 Dec 11 '14
[C] Okay, so I got this one working, but really would love some feedback. Unnecessarily complicated, and I haven't freed memory yet. But in the mean time, I'd really love some critique, please! C people, can you suggest something shorter?
1
u/[deleted] Dec 11 '14
Python
I can't figure out the carrying part. :(
1
u/[deleted] Dec 13 '14
First Ruby program
1
u/[deleted] Dec 15 '14
Java. Carry ends up kinda funky but I ran out of time and as I was trying to do this pretty quick.
1
u/kooschlig Dec 16 '14
Here's my solution in nodejs.
I made much use of the automagic number/string conversion that js offers. Suggestions on how to improve this are always welcome( escpecially with nodejs specific stuff, as I'm still picking up the nodejs way :).
1
u/diclus4 Dec 19 '14 edited Dec 19 '14
small one in ruby :)
1
u/gregsaw Dec 21 '14
First post and about 2 weeks late, but here we go (in Java):
1
u/[deleted] Jan 09 '15 edited Jan 09 '15
C solution
1
u/zebulan_ Jan 27 '15
Python 3:
0
u/[deleted] Dec 08 '14
[deleted] — view deleted comment
0
u/[deleted] Dec 09 '14
[deleted] — view deleted comment