🎓 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
In this blog post, we will learn how to write a Kotlin program to find the maximum number in a List.
In Kotlin, there are several ways to manipulate lists, and finding the maximum number within a list is a common programming task. In this blog post, we will explore a Kotlin program that efficiently finds the maximum number in a given list. We will walk through the program step by step, explaining the logic behind it.
Kotlin Program to Find the Maximum Number in a List
To find the maximum number in a list, we can follow a straightforward approach. Let's dive into the code:
fun findMaxNumber(numbers: List<Int>): Int? {
if (numbers.isEmpty()) return null
var maxNumber = numbers[0]
for (number in numbers) {
if (number > maxNumber) {
maxNumber = number
}
}
return maxNumber
}
fun main() {
val numbers = listOf(10, 5, 7, 15, 3, 25, 40)
val maxNumber = findMaxNumber(numbers)
println("The maximum number in the list is: $maxNumber")
}Output:The maximum number in the list is: 40Explanation:
The findMaxNumber() function takes a list of integers as input and returns the maximum number found in the list. If the list is empty, the function returns null to indicate that there is no maximum number:
fun findMaxNumber(numbers: List<Int>): Int? { if (numbers.isEmpty()) return nullWe initialize the maxNumber variable with the first element of the list (numbers[0]). This serves as our initial reference point: var maxNumber = numbers[0]We then iterate over the remaining elements of the list using a for loop. For each number, we compare it with the current maxNumber. If the number is greater, we update the maxNumber accordingly. Finally, we return the maximum number found after iterating through the entire list: for (number in numbers) {
if (number > maxNumber) {
maxNumber = number
}
}
return maxNumberIn the main() function, we create a sample list of numbers (val numbers = listOf(10, 5, 7, 15, 3)) and call the findMaxNumber() function with this list. The maximum number is then stored in the maxNumber variable, which we print to the console using println().fun main() {
val numbers = listOf(10, 5, 7, 15, 3, 25, 40)
val maxNumber = findMaxNumber(numbers)
println("The maximum number in the list is: $maxNumber")
}Conclusion
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