Articles

Solved Mulplication table using for,nexted loops

December 3, 2012 at 16:22:12
Specs: Windows 7 Service Pack 2, Pentium 8 4GHz / 4 Gigs

I need a properly spaced Multiplication Table using For,Next loops that I can easily whip up on my QBasic. It needs to use If/Then/Elses for the spacing issue. Multiplication table of 10x10.

It needs to be able to evenly space between single digit, double digit, and triple digit numbers.

I have somewhat the body for it, but I can't figure out the spacing.

Typing from memory here...


CLS
FOR J = 1 TO 10
FOR I = 1 TO 10
IF X * Y < 10 THEN
PRINT X * Y; " "; (<-- 2 spaces)
END IF
IF X * Y > 10 AND X * Y < 100 THEN
PRINT X * Y; " "; (<-- 1 space)
END IF
ELSE
PRINT X * Y;
END IF
NEXT I
NEXT J
END

Please tell me how to correctly code this, and how to fix the spacing issue!!!

If you could type what your program looked like with the For,Next and If/Then/Elses, that would be greatly appreciated.

Doesn't count indentations on the loops, but they are there!!!


See More: Mulplication table using for,nexted loops

Report •


#1
December 3, 2012 at 17:50:32
✔ Best Answer

The simplest "tried and true" method is to add a bunch of spaces (to left, for right-just, to right for left-justified) after converting the number to a string.
Here is a generic Basic program (qb, vb, or gw)
'=== begin BASIC program
FOR I = 1 TO 10
FOR J = 1 TO 10
PRINT RIGHT$("   " + STR$(J * I), 4);
NEXT J
PRINT
NEXT I
'===== end basic code

If (pun intended!) you really want to use IF statements, then this might do:

FOR J=1 TO 10
SP$=""
IF I*J<100 THEN SP$=" "
IF I*J<10 THEN SP$=" "
PRINT SP$;I*J;
...

Report •
Related Solutions


Ask Question