🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
1. Introduction
Swapping two numbers means interchanging their values. This tutorial will guide you on how to write a Java program to swap two numbers without using a temporary variable.
2. Program Steps
1. Define a class named SwapNumbers.
2. In the main method, declare two variables and initialize them with the numbers you want to swap.
3. Perform the swapping of the numbers using arithmetic operations.
4. Print the values of the numbers after swapping.
3. Code Program
public class SwapNumbers { // Step 1: Define a class named SwapNumbers
public static void main(String[] args) { // Main method
int num1 = 20; // Step 2: Declare and initialize the first number
int num2 = 30; // Declare and initialize the second number
System.out.println("Numbers before swapping: " + " num1 = " + num1 + ", num2 = " + num2);
// Step 3: Perform the swapping using arithmetic operations
num1 = num1 + num2; // num1 now holds the sum of num1 and num2
num2 = num1 - num2; // num2 now holds the original value of num1
num1 = num1 - num2; // num1 now holds the original value of num2
// Step 4: Print the values after swapping
System.out.println("Numbers after swapping: " + " num1 = " + num1 + ", num2 = " + num2);
}
}
Output:
Numbers before swapping: num1 = 20, num2 = 30 Numbers after swapping: num1 = 30, num2 = 20
4. Step By Step Explanation
Step 1: A class named SwapNumbers is defined.
Step 2: Two integer variables num1 and num2 are declared and initialized with the values you want to swap.
Step 3: The swapping of num1 and num2 is performed using arithmetic operations without using a temporary variable. The sum of num1 and num2 is stored in num1, then num2 is calculated by subtracting the current num2 from num1, and finally, num1 is calculated by subtracting the new num2 from num1.
Step 4: The values of num1 and num2 are printed after the swap.
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment