Suppose that I have to print the following lines:

Number 1 is 5 Number 2 is 6 Number 3 is 7 Number 4 is 8 Number 5 is 9

Now, if I were programming in C, I would have run the following code :

{printf("Number %d is %d \n",i,i+4);}

Print["Number %d is %d", i, i + 4] doesn't work in Mathematica (and yes, I am aware of it).

Moreover, in $C$, I can scan input like this:

{printf("Enter Number %d: ",i);
scanf("%d",&Scanned[i])};           \* Where Scanned[i] is the Array Storing Values of the Numbers *\

I tried

For[i = 0, i < 5, i++, Scanned[i] = Input["Enter the Number %d: ", i]]

But it didn't work (I guess %d is not recognizable by Mathematica).

So how would I implements these tasks in Mathematica?

(I apologize if this question is of very low level, but I searched a lot for it, and didn't get an appropriate answer to my query.)

  • $\begingroup$ Take a look at StringTemplate or StringForm to learn about templates syntac in Mathematica. The way to go is Do + Print + StringTemplate. You may also be interested in column. $\endgroup$ – Kuba Jun 17 '17 at 16:38
  • $\begingroup$ See also: 11305 $\endgroup$ – Kuba Jun 17 '17 at 16:41
  • $\begingroup$ I would add see this on avoiding for loops--just generally good knowledge for people new to Mathematica. $\endgroup$ – b3m2a1 Jun 17 '17 at 16:54
StringForm["Number `` is ``", #, # + 4] & /@ Range[5] // Column

enter image description here

Print[StringForm["Number `` is ``", #, # + 4]] & /@ Range[5];

enter image description here

n = 1; While[n < 6, 
 Print[StringForm["Number `` is ``", n, 
   InputString["Enter number " <> ToString[n++]]]]]

enter image description here


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.