aatish19, I think I've steered you wrong! (sorry)

I just thought I'd "flush out" the ideas in my post above, and it
proved much more difficult than I thought.

Difficult enough that I'd now have to recommend just changing the
numbers, as char's, and storing them in a char array, then just print
the char array, stepping downward, instead of the usual way (of
upward), thereby reversing the number.

This is the program, but don't turn it in as your homework. Your
teacher would probably not believe it was from a beginner. It's just A
way to do it, and after doing this way, I wouldn't recommend it again.

This is in Professional BASIC, but you'll probably get the idea, and
with a few deletions of the '@' character from the end of the variable
names, I believe it will work in Qbasic or QuickBasic.

'reversing a number by decomposing it from the largest decimal place,
'first, (that is, from left to right).
'
'Although it's fast, for VERY large numbers, we'd have to change the
'algorithm, and work from the smaller decimal places, toward the
larger.

DEFINT A-Z
COLOR 7, 1
CLS

DIM number(1 TO 10) AS INTEGER

FOR i = 1 TO 10
   number(i) = -1
NEXT

PRINT : PRINT
INPUT "Please Enter a Number: ", userNum@

'userNum@ = 909911000
InputNum@ = userNum@


     'find the right value for dplaces@, which is
     'the highest number we subtract from userNum, first.

dplaces@ = 1

WHILE (dplaces@ < userNum@)
   dplaces@ = dplaces@ * 10
WEND
dplaces@ = dplaces@ / 10   'resets dplaces@ correctly

i = 1
stepdown = 0

WHILE (userNum@ > 0)

   IF userNum@ >= dplaces@ THEN
      userNum@ = userNum@ - dplaces@

      IF number(i) > -1 THEN
         number(i) = number(i) + 1
      ELSE
         number(i) = 1
      END IF

      stepdown = 0

   ELSE
        'get ready for the next loop
      dplaces@ = dplaces@ \ 10
      i = i + 1
        'we might have to deal with a zero in the number
      stepdown = stepdown + 1

      IF userNum@ > 0 AND stepdown > 0 AND dplaces@ > userNum@ THEN
        number(i) = 0     'we need to add a zero into the array
        stepdown = 0
      END IF


   END IF

WEND

        'handles adding zero's to the end of InputNum@, only
WHILE (dplaces@ > 1)
   dplaces@ = dplaces@ \ 10
   i = i + 1
   number(i) = 0
WEND


PRINT : PRINT
PRINT "User's Number Is:  "; InputNum@
PRINT
PRINT "Which Backwards Is: ";

FOR i = 10 TO 1 STEP -1


   IF number(i) > -1 THEN
      i$ = STR$(number(i))
      PRINT LTRIM$(RTRIM$(i$));
   END IF
NEXT i

END

What language are you studying?

Adak


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Algorithm Geeks" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at http://groups.google.com/group/algogeeks
-~----------~----~----~----~------~----~------~--~---

Reply via email to