I'm trying to write a program with BrainFuck that can read two numbers up to 9, calculate the sum of them and then print the result out, e.g. 3 & 5 give the result 8 .
I'm just trying to understand the BF language but it looks much harder than I thought it would be.
I built a code that will work with an infinite amount of input numbers but it can only output 1 or 2 digits so the range of the sum of digits can be 0-99
or in short:
>,[>+++++++[<------->-]<+[-<+>],]>-[+<[->+>+<<]>>[-<<+>>]<<<[->>>+>+<<<<]>>>>[-<<<<+>>>>]<[-[-[-[-[-[-[-[-[-[<<+<---------->>>[-]]]]]]]]]]]<<[->->+<<]>>[-<<+>>]<]<[>+++++++[<+++++++>-]<-.[-]]+++++++[<+++++++>-]<-.
Numerous people have answered the question already, but since every solution differs a bit, I'll just add mine as well.
My solution does single digit additions (also if the result is >9). So e.g. for the input "89" it retuns "17". I added quite a lot of comments so it should be relatively easy to understand.
GitHub link
I also made a program that can only handle single digit entries and answers:
That is what I know
input:
output:
You should use numbers under 10 and that result under 10
If you want to get results with more than one digit, you have to use a decimal-converter in your brainfuck-code. The rest is the normal calculation:
sadly I have to say, that I don't understand the whole converter code, though I really tried. I just copied it from the german wikipedia site. Maybe someone could explain it? :)
I saw this post 2-3 days ago and I've worked on it and now I have a solution on multi-digit addition. First I thought the name of this PL is a bit offensive but now I know, if I was authorized to name this programming language, I'd choose the same.
Now, I'll tell you how to use my code.
Only +ve numbers can be added in my code. And make sure you use the same number of digits in both inputs. i.e. if you want to add 57 with 3, then give input like 57+03= or 03+57= . Now the code. I've documented with an example. Still I prefer NOT to look into my code, as designing by yourself is easier than studying or troubleshooting a code in bf. First you need to know how to compare two numbers. My answer in this question is one solution. In documentation, I used 'plus' instead of + ,as + is a valid operation in bf.
I know its a bit lengthy code, may be there are better solutions possible. But still it worth a shot.