🎓 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 sum of the digits of a number.
Calculating the sum of the digits of a number is a common problem in programming. In this blog post, we will explore a Kotlin program that efficiently computes the sum of the digits of a given number. We will walk through the program step by step, explaining the logic behind it.
Kotlin Program: Finding the Sum of Digits of a Number
To find the sum of digits in Kotlin, we can follow a straightforward approach. Let's dive into the code:
fun sumOfDigits(number: Int): Int {
var sum = 0
var n = number
while (n != 0) {
val digit = n % 10
sum += digit
n /= 10
}
return sum
}
fun main() {
val number = 12345
val digitSum = sumOfDigits(number)
println("Number: $number")
println("Sum of Digits: $digitSum")
}
Output:
Number: 12345
Sum of Digits: 15Explanation:
The sumOfDigits() function takes an integer number as input and returns the sum of its digits as an integer.
Inside the function, we initialize a variable sum to 0, which will store the running sum of the digits.
We create a variable n and assign it the value of a number. This allows us to perform operations on the number without modifying the original input.
Using a while loop, we continue until n becomes 0. In each iteration, we extract the last digit of n using the modulo operator % with 10. We add this digit to the sum variable and then divide n by 10 to remove the last digit.
Finally, we return the sum as the result, which represents the sum of digits in the original number.
In the main() function, we create a sample input number (val number = 12345) and call the sumOfDigits() function with this number. The original number and the sum of its digits are then printed to the console using println().
Conclusion
In this blog post, we have discussed a Kotlin program that efficiently finds the sum of digits in a given number. By implementing a simple loop and using modulo and division operators, we can compute the sum iteratively. Understanding this program equips you with the necessary skills to handle digit manipulation and solve similar numerical problems in Kotlin.
Feel free to explore and modify the code to suit your specific needs. The ability to calculate the sum of digits is valuable in various programming scenarios, ranging from simple arithmetic operations to more complex mathematical algorithms.
Happy coding!
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