‫Hello my friends, and welcome to Coding Exercise number three.
‫We are asked to write a program to swap two variables.
‫If the user enters a equal to five and b equal to six.
‫I need to display b equal to five and a equal to six.
‫I need to swap the value of these two variables.
‫Pause the video and think about how to solve such a program.
‫So before we start, I'm waiting your solutions in the comment below, so please paste your solutions
‫in the comment below so I can help you if you have any trouble.
‫I start by declaring some variables.
‫Var a, B and temp.
‫I need to create these three variables.
‫Okay.
‫This is of type int.
‫This is of type int and this is of type int.
‫Okay.
‫I need to spread the tokens by this.
‫Never be of type int.
‫Var temp.
‫Type int.
‫Okay.
‫Now I will display for the user to enter the numbers.
‫So print.
‫Enter the two numbers.
‫I will use the scanner.
‫Var scan or s1 equal to scanner dot or system dot n alt+.
‫Enter to import the package.
‫Import the package java.util dot.
‫All this asterisk will import all the classes so var x equal to s1 dot next int and var.
‫These are variables.
‫A so a here a I have declared it and b equal to s1 dot next end.
‫Okay, so these are the two variables I am storing them here or I can directly remove these.
‫And start by typing like this.
‫Var b okay.
‫Now, in order to swap the variables, I need to use the temp the help of temp variable.
‫So temp now equal to a and a equal to B and now B equal to temp.
‫Okay.
‫I'm using a temporary variable in order to store temporarily the value of a then assign a equal to b
‫be equal to temp.
‫And this this will result in swapping the variables print
‫the value of a is a and B is B.
‫Okay.
‫Let's run the program and see.
‫If this working, please enter.
‫The two numbers for a is five and the variable B is equal to six.
‫Now A is equal to six and B equal to five.
‫So this is A, this is B.
‫I swapped them.
‫So A becomes six and B, equal to five.
‫Congratulations, guys.
‫I tried to put this example in order to teach you how to think.
‫We used a temporary variable in order to solve such a program.
‫And this.