Question

In: Computer Science

Write a Java function to swap two integers.

Write a Java function to swap two integers.

Solutions

Expert Solution

//swap_number() method will swap two integers
public static void swap_number(int m,int n)
{
   int c; //declare third variable
   c=m; //assign first number to third variable
   m=n; //assign value of second variable to firtst variable
   n=c;//assign value of third variable to second variable
   System.out.println("After swap first number is "+m +" second number is "+n);
}

PROGRAM IMPLEMENTATION

import java.lang.*;
import java.util.Scanner;

class swap
{

//swap_number() method will swap two integers
public static void swap_number(int m,int n)
{
   int c; //declare third variable
   c=m; //assign first number to third variable
   m=n; //assign value of second variable to firtst variable
   n=c;//assign value of third variable to second variable
   System.out.println("After swap first number is "+m +" second number is "+n);
}
//driver program
public static void main(String[] args)
{
   //declare the scanner object to read data
Scanner scr= new Scanner(System.in);
   int a,b; //declare two integers
   System.out.println("Enter two numbers"); //ask user for two numbers
   a=scr.nextInt(); //read first number
   b=scr.nextInt();//read second number
   //diplsay the numbers
   System.out.println("Before swap first number is "+a +" second number is "+b);
   swap_number(a,b); //call to swap method
}
}

          

OUTPUT


Related Solutions

In Java, a set of integers is given. write a function to find 2 integers in...
In Java, a set of integers is given. write a function to find 2 integers in this set that sums upto a target value. i.e [1,5,2,0,11,3] target = 7 result [5,2] i.e [1,5,4,0,14,-7] target = 9 result [5,4] NOTE: THE SAME INTEGER CANNOT BE USED TWICE !!!
Write a function called swapElements to swap two elements in a character array. This function takes...
Write a function called swapElements to swap two elements in a character array. This function takes two parameters, a character array and input file. The function should read two numbers from a file and it swap the elements stored in the indices read. please code in c++
Write a java method to swap between two values in a singly linked list
Write a java method to swap between two values in a singly linked list
Write a c program Write a function to swap two elements of an integer array. Call...
Write a c program Write a function to swap two elements of an integer array. Call the function to swap the first element, i[0] with last element i[n], second element i[1] with the last but one element i[n-1] and so on. Should handle arrays with even and odd number of elements. Call the swap function with the following arrays and print results in each case before and after swapping. i. int arr1[] = {0, 1, 2, 3, 30, 20, 10,...
Write a function called is_equal(). This function will take as an argument two integers. Call the...
Write a function called is_equal(). This function will take as an argument two integers. Call the is_equal function and supply two integers. You do not need to get the integers from the keyboard. Compare the two integers. If the two integers are equal, then print "equal". If the two integers are not equal, print "not equal". Call the function from your main code to execute the function. Sample output: equal Sample output: not equal
C++ The minimum function. (a) Write a function that takes two integers and returns the value...
C++ The minimum function. (a) Write a function that takes two integers and returns the value of the smaller one. In the main() function provide 5 test cases to verify its correctness. (b) Write the function that takes two characters and return the smaller one in the lexicographical order. Write the main() function that tests that functions for 5 different pairs of character type variables. (c) Write a generic function that takes two numeric objects and returns the value of...
Write a Java program that reads two integers on the keyboard and displays them on the...
Write a Java program that reads two integers on the keyboard and displays them on the screen.
For the following Ackerman function defined for non-negative integers, write a Java program that: Contains a...
For the following Ackerman function defined for non-negative integers, write a Java program that: Contains a method called computeAckermann that takes two integer values as parameters and calculates and returns the value of the Ackerman function. The method should be defined inside a class named Ackermann. Test the implementation of Ackerman in (a) by calling it from main function and printing the returned value. The main function should be defined in a separate class named AckermannDemo.
FOR JAVA Write a method called findNum that takes a two-dimension array of integers and an...
FOR JAVA Write a method called findNum that takes a two-dimension array of integers and an int as parameters and returns the number of times the integer parameter appears in the array. For example, if the array (as created by the program below) is 10 45 3 8 2 42 3 21 44 And the integer parameter is 3, the value returned would be 2 (the number 3 appears two times in the array) public class HomeworkA { public static...
Using Java programming, Write a LinkedList method swap() that takes two ints as arguments and swaps...
Using Java programming, Write a LinkedList method swap() that takes two ints as arguments and swaps the elements at those two positions. The method should not traverse the list twice to find the elements, and it should not create or destroy any nodes.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT