‎2014 Feb 17 4:04 PM
I have been given this problem:
Write a program that prompts the user for three values: a starting number, an ending number, and a
count by number. The program should then display the number sequence that results from beginning at the starting
number, counting by the count by number, and stopping when the ending number is hit or passed by.
For example, if the user entered 2, 3, and 12 respectively, the program should display the following:
Going from 2 to 12, counting by 3:
2, 5, 8, 11
I have the display to work by doing this:
PARAMETERS: start TYPE i,
end TYPE i,
count TYPE i.
WHILE start LE end.
write: start NO-GAP, ', '.
start = start + count.
ENDWHILE.
But can't figure out for the life of me how to not have the ending comma. I know I should prob use SY-INDEX in an IF statement, and I've tried so many ways but can't figure it out. I've now looked at it so much that I need an outsiders input. Any help would be greatly appreciated!
‎2014 Feb 17 4:15 PM
Hi,
By no means the only way, but could you accumulate the output into a string rather than write it to the screen number by number?
This would give you the option to manipulate the commas before writing the string to the screen.
Regards,
Nick
‎2014 Feb 17 4:15 PM
Hi Ashley,
This will work fine.
PARAMETERS: start TYPE i,
end TYPE i,
count TYPE i.
WHILE start LE end.
if sy-index = 1.
write: start NO-GAP.
else.
write: ', ' NO-GAP, start NO-GAP.
endif.
start = start + count.
ENDWHILE.
Regards,
Karthikeyan
‎2014 Feb 17 4:19 PM
‎2014 Feb 17 4:21 PM
‎2014 Feb 17 4:21 PM
You can move it to a string as Nick told or you can just check an extra addition of count in start in another variable to check if it is the last iteration or not
‎2014 Feb 17 4:25 PM
Hi Ashley,
or this:
PARAMETERS: start TYPE i,
end TYPE i,
count TYPE i.
IF start LE end.
WRITE start NO-GAP.
start = start + count.
ENDIF.
WHILE start LE end.
WRITE: ', ' NO-GAP, start NO-GAP.
start = start + count.
ENDWHILE.
Regards,
Dirk