🎓 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
In this blog post, we're exploring a classic Kotlin program: swapping two numbers without a temporary variable.
2. Program Overview
This Kotlin program will:
1. Request the user to input two numbers.
2. Capture the numbers provided by the user.
3. Swap the numbers without using a temporary or third variable.
4. Display the swapped values to the user.
3. Code Program
import java.util.Scanner
fun main() {
// Initialize the Scanner class for user input
val reader = Scanner(System.in)
// Ask the user to input the first number
print("Enter the first number: ")
val a = reader.nextInt()
// Prompt the user for the second number
print("Enter the second number: ")
val b = reader.nextInt()
// Swap the numbers without using a temporary variable
a = a + b
b = a - b
a = a - b
// Display the swapped values to the user
println("After swapping: First number = $a, Second number = $b")
}
Output:
Enter the first number: 5 Enter the second number: 10 After swapping: First number = 10, Second number = 5
4. Step By Step Explanation
1. Scanner Initialization: To capture user input, we make use of the Scanner class from the java.util package. The reader instance represents our scanner object.
2. Capture User Input: Using the print function, the program asks the user for two integer values, storing them in a and b.
3. Number Swapping Technique: Instead of relying on a third variable for swapping, we exploit arithmetic operations:
- The sum of a and b is stored in a.- By subtracting the new a (which is the sum) by the original b, we get the original a value, which we then assign to b.- Finally, by subtracting the new a (still the sum) by the new b (the original a), we derive the original b value, which we assign back to a. The swapping is now complete.
4. Showcase Swapped Values: The program then uses the println function to display the swapped values to the user.
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