469,342 Members | 6,455 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,342 developers. It's quick & easy.

how do I print a reverse order of these numbers in this while loop

Hello how do I print a reverse order of these numbers in this while loop. I'm using array.

Expand|Select|Wrap|Line Numbers
  1.   Sub Main()
  2.         Dim data() = {30, 32, 81, 43, 58, 85, 23, 19}
  3.         Dim i = 0
  4.  
  5.         While (i > 8)
  6.  
  7.             Console.WriteLine(data(i))
  8.             i = i + 1
  9.  
  10.         End While
  11.     End Sub
Oct 29 '19 #1
3 3008
gits
5,390 Expert Mod 4TB
well - if you have to use the while loop then 1st you would need to make sure it runs at all - the condition in line 5 is never met - so fix the comparison. now - i is counting up and so you just would need to access the elements from the end in that kind of loop - which basically means that you could use something like (Pseudocode here):

Expand|Select|Wrap|Line Numbers
  1. indexOfElementInTheLoop = (data.Length - 1) - i
a much cleaner way here would probably be a For-loop though - where you count down from the array's length-1 to 0 and you may use the counter directly to access the elements.
Oct 30 '19 #2
cactusdata
177 Expert 128KB
Use a For-Next loop:

Expand|Select|Wrap|Line Numbers
  1. Sub Main()
  2.  
  3.     Dim data() As Integer = {30, 32, 81, 43, 58, 85, 23, 19}
  4.     Dim index As Integer
  5.  
  6.     For index = data.Length - 1 To 0 Step -1    
  7.         Console.WriteLine(data(index).ToString())
  8.     Next
  9.  
  10. End Sub
Oct 30 '19 #3
Sherin
77 64KB
Try This Code

Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. int main() {
  3.     int n, rev = 0, remainder;
  4.     printf("Enter an integer: ");
  5.     scanf("%d", &n);
  6.     while (n != 0) {
  7.         remainder = n % 10;
  8.         rev = rev * 10 + remainder;
  9.         n /= 10;
  10.     }
  11.     printf("Reversed number = %d", rev);
  12.     return 0;
  13. }
Output

Expand|Select|Wrap|Line Numbers
  1. Enter an integer: 1234
  2. Reversed number = 4321
Apr 10 '20 #4

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

3 posts views Thread by James Lee | last post: by
20 posts views Thread by sahukar praveen | last post: by
15 posts views Thread by Fabio Cannizzo | last post: by
10 posts views Thread by aatish19 | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by suresh191 | last post: by
1 post views Thread by haryvincent176 | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.