How do I print reverse numbers in QBasic?
Here's some sample QBasic code that will prompt the user to enter a number, and then print the digits of the number in reverse order:
INPUT "Enter a number: ", num
DO WHILE num > 0
digit = num MOD 10
PRINT digit;
num = num \ 10
LOOP
1. The "INPUT" statement prompts the user to enter a number and assigns the entered value to the variable "num".
2. The "DO WHILE" loop will execute as long as the value of "num" is greater than 0.
3. Within the loop, the variable "digit" is assigned the value of the last digit of "num" by using the "MOD" operator to get the remainder when "num" is divided by 10.
4. The "PRINT" statement then prints the value of "digit".
5. Finally, the "num" is updated to remove the last digit and "num = num \ 10"
Note that, "\" is used for Integer division.
And ";" is used to print multiple things in same line.
0 Comments