Java Tutorial - Learn Java Programming Language
Hello Guys 🤙
In our Previous Blog We covered the Basic Syntax of Java Programming Language.
In today's Blog We will see How to Make Simple Java Program in Programming Language
A Java Program to Swap The Values of Two Variables Using a Third variable
Program -
import java. io. *;
import java.util.Scanner;class TwoValueSwap {
public static void main(String args[ ])
{
Scanner input = new Scanner(System.in);
int a, b, temp;
System.out.print("Enter Value of a = ");
a = input.nextInt();
System.out.print("Enter Value of b = ");
b = input.nextInt();
// swap the values using a temp variable
temp = a;
a = b;
b = temp;
// print the swapped values
System.out.println("Swapped Values: a = " + a + " , b = " + b);
}
}
Output -
Output -
Enter Value of a = 15
Enter Value of b = 30
Swapped Value: a = 30, b=15
A Java Program to Swap The Values of Two Variables without Third Variable
Program -
import java. io. *;
import java.util.Scanner;
class TwoValueSwap {
public static void main(String args[ ])
{
Scanner input = new Scanner(System.in);
int a, b;
System.out.print("Enter Value of a = ");
a = input.nextInt();
System.out.print("Enter Value of b = ");
b = input.nextInt();
// swap the values using a temp variable
a = a+b;
b = a-b;
a = a-b;
// print the swapped values
System.out.println("Swapped Values: a = " + a + " , b = " + b);
}
}
Output -
Enter Value of a = 15
Enter Value of b = 30
Swapped Value: a = 30, b=15
If You Want to Know more Java Programming language - So Click This Link 👇
can you want to see the privacy policy of this page then click on this link 👇


Great
ReplyDeleteGood work 👍 dheeru 💥
ReplyDeleteGood
ReplyDeleteAwesome 👍
ReplyDelete