Ask Question
3 March, 04:05

Given three already declared int variables, i, j, and temp, write some code that swaps the values in i and j. Use temp to hold the value of i and then assign j's value to i. The original value of i, which was saved in temp, can now be assigned to j.

+5
Answers (1)
  1. 3 March, 05:12
    0
    public class newass {

    public static void main (String[] args) {

    int i = 5, j=6, temp;

    System. out. println ("--Before swap--");

    System. out. println ("First number = " + i);

    System. out. println ("Second number = " + j);

    / / Value of first is assigned to temporary

    temp = i;

    / / Value of second is assigned to first

    i = j;

    / / Value of temporary (which contains the initial value of first) is assigned to second

    j = temp;

    System. out. println ("--After swap--");

    System. out. println ("First number = " + i);

    System. out. println ("Second number = " + j);

    }

    }

    Explanation:

    That is a java code that accomplishes the task. we start off by assigning initial values (5 and 6) respectively to i and j. We first print this out, then using a the temp values we swap the values and print out.
Know the Answer?
Not Sure About the Answer?
Find an answer to your question ✅ “Given three already declared int variables, i, j, and temp, write some code that swaps the values in i and j. Use temp to hold the value of ...” in 📘 Computers and Technology if you're in doubt about the correctness of the answers or there's no answer, then try to use the smart search and find answers to the similar questions.
Search for Other Answers