1

How can I add a list of numbers like an accountant would (using presumably bc or dc)? Typically this is done by entering each number followed by a "+" key, e.g., there would be on an adding machine. I tried

300[RET]<br>
521+[RET]

and got a syntax error. The same thing happens if I put the plus sign first:

300[RET]<br>
+521[RET]

I do not want to make a long line of numbers separated by plus signs.

3
  • 1
    Your first example works for me as-is in dc.
    – Celada
    Jul 29 '15 at 13:41
  • 1
    @Celada work as is, but you need to enter p to print result.
    – Archemar
    Jul 29 '15 at 14:35
  • So how do you want to print the result ? Can you show an example with more numbers - say 3 or 4 ?
    – amisax
    Jul 29 '15 at 16:11
2

Here's an example using dc

dc
300
521+p

which pushes 300 then 521 onto the stack, then pops and adds them (+) and prints the answer: 821. Continuing the sequence with more postfix additions:

123+
456+

and then printing the final result

p

outputs 1400 (i.e. 821 + 123 + 456). You can exit the dc interpreter by typing q. So the full transcript is:

dc
300
521+p
821
123+
456+
p
1400
q

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.