🎓 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'll walk through a simple Kotlin program that computes the remainder when one number is divided by another.
2. Program Overview
Our Kotlin program will achieve the following:
1. Prompt the user to input two numbers.
2. Fetch the provided numbers.
3. Compute the remainder when the first number is divided by the second, ensuring we handle a potential division by zero scenario.
4. Display the result of the operation to the user.
3. Code Program
import java.util.Scanner
fun main() {
// Initialize a Scanner object to read user input
val reader = Scanner(System.in)
// Prompt the user to provide the first number
print("Enter the number from which you want to find the remainder: ")
// Capture and store the first number
val num1 = reader.nextDouble()
// Prompt the user to provide the second number
print("Enter the number to divide by (to find the remainder after division): ")
// Capture and store the second number
val num2 = reader.nextDouble()
// Ensure the second number isn't zero to prevent division by zero
if (num2 == 0.0) {
println("Division by zero isn't permissible.")
return
}
// Calculate the remainder of the two numbers
val remainder = num1 % num2
// Display the result to the user
println("The remainder when $num1 is divided by $num2 is: $remainder")
}
Output:
Enter the number from which you want to find the remainder: 29 Enter the number to divide by (to find the remainder after division): 4 The remainder when 29.0 is divided by 4.0 is: 1.0
4. Step By Step Explanation
1. Scanner Initialization: We utilize the Scanner class from the java.util package to enable user input. Here, reader
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