# QBasic Program to Reverse a Number

Write a program in QBasic to reverse a number.

For example,

```Input
12345
Output
54321```

## Steps to Reverse A Number

1. Let n be the input number.
2. Declare a variable temp and set temp = n.
3. Declare a variable num. Set num = 0.
4. Repeat the following steps while temp > 0
1. Set r = temp MOD 10. This step stores the rightmost digit of temp in r.
2. Set num = num * 10 + r. This step adds r at once place of num.
3. Set temp = temp / 10. This step removes the rightmost digit of temp.
5. The reverse of N is stored in num.

## Reverse a Number in QBasic

``````CLS
INPUT "Enter a Number: ", n
temp = n
num = 0
WHILE temp > 0
r = temp MOD 10
num = num * 10 + r
' Note: We are using '\' for division instead of '/'
' '\' returns integer number
' whereas '/' returns floating number
temp = temp \ 10
WEND
PRINT "Reverse of"; n; "is: "; num
END
``````

## Reverse a Number in QBasic – Using do while

``````CLS
INPUT "Enter a Number: ", n
temp = n
num = 0
DO
r = temp MOD 10
num = num * 10 + r
' Note: We are using '\' for division instead of '/'
' '\' returns integer number
' whereas '/' returns floating number
temp = temp \ 10
LOOP WHILE temp > 0
PRINT "Reverse of"; n; "is: "; num
END
``````

## Reverse a Number in QBasic – Using do until

``````CLS
INPUT "Enter a Number: ", n
temp = n
num = 0
DO
r = temp MOD 10
num = num * 10 + r
' Note: We are using '\' for division instead of '/'
' '\' returns integer number
' whereas '/' returns floating number
temp = temp \ 10
LOOP UNTIL temp = 0
PRINT "Reverse of"; n; "is: "; num
END
``````

## Reverse a Number in QBasic – Using Subroutine

``````DECLARE SUB reverse

CLS
reverse
END

SUB reverse
INPUT "Enter a Number: ", n
temp = n
num = 0
WHILE temp > 0
r = temp MOD 10
num = num * 10 + r
' Note: We are using '\' for division instead of '/'
' '\' returns integer number
' whereas '/' returns floating number
temp = temp \ 10
WEND
PRINT "Reverse of"; n; "is: "; num
END SUB
``````

## Reverse a Number in QBasic – Using Function

``````DECLARE FUNCTION reverse(n)

CLS
INPUT "Enter a Number: ", n
PRINT "Reverse of"; n; "is: "; reverse(n)
END

FUNCTION reverse (n)
temp = n
num = 0
WHILE temp > 0
r = temp MOD 10
num = num * 10 + r
' Note: We are using '\' for division instead of '/'
' '\' returns integer number
' whereas '/' returns floating number
temp = temp \ 10
WEND
reverse = num
END FUNCTION
``````

Output