455,429 views
17 votes
17 votes
Write a program whose input is two integers and whose output is the two integers swapped. Place the values in an array, where x is position 0 and y is position 1.Ex: If the input is: 3 8 then the output is: 83 Your program must define and call a method: public static void swapValues (int[] "values) LabProgram.java 1 import java.util.Scanner; 23 public class LabProgram 4 5 /* Define your method here 6 7 public static void main(String[args) { 8 /* Type your code here. / 9 } 10 } 11

User Gabriele Mariotti
by
2.9k points

1 Answer

25 votes
25 votes

Answer:

import java.util.*;

import java.lang.*;

import java.io.*;

class Codechef

{ public static void swapValues(int[] values)

{int temp= values[0];

values[0]=values[1];

values[1]=temp;

System.out.print(values[0]+" "+values[1]);}

public static void main (String[] args) throws java.lang.Exception

{

Scanner sc= new Scanner(System.in);

int A[] = new int[2];

A[0] = sc.nextInt();

A[1]= sc.nextInt();

swapValues(A);

}

}

Input:-

3 8

Output:-

8 3

Input:-

1 2

Output:-

2 1

User Jay Jay Jay
by
3.0k points