Skip to content

Commit

Permalink
swapping values at last and first index of the array
Browse files Browse the repository at this point in the history
  • Loading branch information
suhanigupta23 authored Feb 4, 2024
1 parent 4a86fa8 commit 381a5e6
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions swapping_values_in_array.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
//SWAPPING VALUES IN ARRAY STORED OF FIRST AND LAST INDEX OF ARRAY

import java.util.*;
public class Program
{
public static void swap(int []arr,int i,int r)
{
//swapping numbers by introducing third variable
int s=arr[i];
arr[i]=arr[r];
arr[r]=s;
}
public static void main(String[] args)
{
//defining array
int []arr;
arr=new int [5];
//array value given
arr[0]=33;
arr[1]=65;
arr[2]=87;
arr[3]=99;
arr[4]=45;

//swapping function applied to swap the first and last index value of array
swap(arr,0,4);

//after swapping the value at frist and last postion we print them
for(int i=0;i<arr.length;i++)
{
System.out.println(arr[i]);
}
}
}

0 comments on commit 381a5e6

Please sign in to comment.