mmamathes
(Bill Blair)
1
a = 2
b = 3
c = 5
Print["a+b=", a + b]
Print["ab=", a b]
Print["a-c=", a - c]
The above code invokes the Print[] function line by line, yielding three distinct lines of output.
My desired output format is a single line display, as shown below:
{a + b = 5, ab = 6, a - c = -3}
I’ve tried implementing the following code on my own, but it doesn’t produce the desired result. What’s the correct approach to this?
{Print["a+b=", a + b], Print["ab=", a b], Print["a-c=", a - c]}
mroge02
(Michael Rogers)
2
You have to use one Print:
Print[{"a+b=", a + b, "ab=", a b, "a-c=", a - c}]
I’m not sure how you want the combination formatted & punctuated. You might find StringForm[] helpful.
Another format:
Row[{HoldForm[a + b] == a + b,
HoldForm[a*b] == a*b,
HoldForm[a - c] == a - c}, ", "] // TraditionalForm
The output is printed without need for an explicit Print:
{"a+b=", a + b, "ab=", a b, "a-c=", a - c}