Unverified Commit e3d36f99 authored by Lokesh Singh's avatar Lokesh Singh Committed by GitHub

Merge pull request #1 from MTS-29/main

Added SwapValuesOfArray
parents 155d8aef f316a249
package com.nisum.assignment1;
import java.util.Scanner;
/**
* This code swap the first and last element in an array and return it
* @author mtaqui
*
*/
public class SwapValuesOfArray {
public static void main(String[] args) {
Scanner inp = new Scanner(System.in);
System.out.print("Enter the number of elements: ");
int n = inp.nextInt();
int[] arr = new int[n];
System.out.println("Enter the elements : ");
for (int i = 0; i < n; i++) {
arr[i] = inp.nextInt();
}
int firstElement = arr[0];
int lastElement = arr[arr.length - 1];
arr[0] = lastElement;
arr[arr.length - 1] = firstElement;
for (int a : arr) {
System.out.println(a);
}
inp.close();
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment