Question

-t II - CTEC 120 Review [ 10 points ] 1) Write the Java code that swaps the value of two integer variables.
0 0
Add a comment Improve this question Transcribed image text
Answer #1

import java.util.Scanner;
public class Main
{
   public static void main(String[] args)
   {
   //variable declaration
   int num1, num2, temp;
   Scanner input = new Scanner(System.in);
   //display message on the computer screen
   System.out.print("Enter the first integer: ");
num1 = input.nextInt();
//display message on the computer screen
System.out.print("Enter the second integer: ");
num2 = input.nextInt();
//swap the number using third variable
temp = num1;
num1 = num2;
num2 = temp;
//display message on the computer screen
System.out.println("\nAfter Swapping");
//display the first number on the computer screen
       System.out.println("\nThe first integer is: " + num1);
       //display the second number on the computer screen
       System.out.println("The second integer is: " + num2);
   }
}

OUTPUT:

Enter the first integer: 10 Enter the second integer: 20 After Swapping The first integer is: 20 The second integer is: 10

Add a comment
Know the answer?
Add Answer to:
-t II - CTEC 120 Review [ 10 points ] 1) Write the Java code that...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT