🎓 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
Kotlin continues to gain traction as a preferred language for Android development and server-side applications. It offers many modern programming concepts out-of-the-box, making it a joy to work with. In today's blog, we'll walk through a straightforward program: subtracting two numbers entered by the user in Kotlin.
2. Program Overview
Our Kotlin program will:
1. Prompt the user to input two numbers.
2. Read these numbers from the console.
3. Subtract the second number from the first.
4. Display the result to the user.
3. Code Program
import java.util.Scanner
fun main() {
// Create a Scanner object to read user input from the console
val reader = Scanner(System.in)
// Ask the user to provide the first number
print("Enter the first number: ")
// Read and store the first number
val num1 = reader.nextDouble()
// Ask the user to provide the second number
print("Enter the second number: ")
// Read and store the second number
val num2 = reader.nextDouble()
// Subtract the second number from the first
val difference = num1 - num2
// Display the result
println("The difference between $num1 and $num2 is: $difference")
}
Output:
Enter the first number: 10.5 Enter the second number: 4.5 The difference between 10.5 and 4.5 is: 6.0
4. Step By Step Explanation
1. Scanner Object: The Scanner class from the java.util package helps in capturing input from users. We've created an instance of this class named reader.
2. Input for First Number: Using the print function, we guide the user to enter their first number. Immediately after, reader.nextDouble() fetches this input, which we save in the variable num1.
3. Input for Second Number: In the same way, we direct the user to enter their second number and store it in the variable num2.
4. Subtraction Operation: We find the difference between the two numbers with the subtraction operation (num1 - num2) and store the result in the difference variable.
5. Displaying the Result: Using the println function, the computed difference is shown back to the user.
Through this basic Kotlin program, we get a feel for user interactions, arithmetic operations, and Kotlin's concise syntax. This example serves as a foundation for more complex operations and applications in the Kotlin world.
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