sirigiri sai kumar
sirigiri sai kumar

Reputation: 161

Revering the int array using java for loop

The code to return the integer array reversed. I think its entirely correct, But its not working.

The code is not entering into the for(int j=3; j==0; j--) loop.

CODE:

import java.io.*;
import java.util.*;


public class Solution {

public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    int n = in.nextInt();
    int[] arr = new int[n];
    int[] rev = new int[n];

  for(int i=0; i < n; i++)
    {
        arr[i] = in.nextInt();   
      System.out.println( "Here"+ arr[i]);
      for(int j=3; j==0; j--)
       {
          System.out.println(j);
          System.out.print( "Resevre1 "+ rev[j]);
          if (j+i==3)
          {
          rev[j] = arr[3-i];
          System.out.print( "Resevre Here"+ rev[j]);
          }
       }
    }
    in.close();
}
}

Sample Input

4

1 4 3 2

Sample Output

2 3 4 1

Upvotes: 0

Views: 55

Answers (2)

Pavlo Plynko
Pavlo Plynko

Reputation: 586

CODE:

import java.util.*;

public class Solution {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        int[] arr = new int[n];

        for (int i = 0; i < n; i++) {
            arr[i] = in.nextInt();
        }

        for (int i = n - 1; i >= 0; i--) {
            System.out.print(arr[i] + " ");
        }

        in.close();
    }
}

Upvotes: 0

agaonsindhe
agaonsindhe

Reputation: 594

The code wont go in the loop the condituon you have entered will always be false.

for(int j=3; j==0; j--)

inital value of j is 3 and the condition statement which needs to be true everytime you want your program to go inside the loop this will always keep failling unless your condition statement j==0

satisfies. As I can judge you are trying to run the loop in reverse You need to change the condition to for(int j= 3; j>=0; j--)

depands how many times you want this loop to run revrse thrice or twice.

Upvotes: 1

Related Questions