🎓 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's expressive and succinct syntax, combined with its robust standard library, makes it a preferred choice for a plethora of programming tasks. Among its myriad applications, mathematical computations stand out. In this blog post, we're diving into a foundational mathematical concept: calculating the factorial of a number using Kotlin.
2. Program Overview
This Kotlin piece will:
1. Prompt the user to submit a number.
2. Capture the specified number.
3. Compute the factorial of the number.
4. Display the calculated factorial to the user.
3. Code Program
import java.util.Scanner
fun main() {
// Initiate Scanner for collecting user input
val reader = Scanner(System.in)
// Ask the user to provide a number
print("Enter a non-negative integer to compute its factorial: ")
// Store the provided number
val num = reader.nextInt()
// Ensure the entered number is non-negative
if (num < 0) {
println("Please provide a non-negative number.")
return
}
// Compute the factorial of the number
val factorial = computeFactorial(num)
// Present the calculated factorial to the user
println("The factorial of $num is: $factorial")
}
// Recursive function to compute factorial
fun computeFactorial(n: Int): Long {
return if (n == 0) 1 else n * computeFactorial(n - 1)
}
Output:
Enter a non-negative integer to compute its factorial: 5 The factorial of 5 is: 120
4. Step By Step Explanation
1. Scanner Setup: We harness the Scanner class from java.util package for capturing user input. Our instance is named reader.
2. Number Input: The user is prompted via the print function to submit a number, which gets stored in num.
3. Non-negative Check: Factorials are defined for non-negative integers. Thus, the program checks if the user's input is negative and notifies the user accordingly.
4. Factorial Calculation: The program calls the computeFactorial function to obtain the factorial value. This function is recursive: it calls itself with decremented values until it reaches zero. Once zero is reached, the function returns 1, creating a chain of multiplications to give the factorial of the initial number.
5. Result Display: The println function illustrates the factorial result 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