🎓 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 detail the steps to design a Go program that divides two numbers offered by the user.
2. Program Overview
Our targeted Golang program aims to:
1. Prompt the user to input two numbers.
2. Execute the division of the provided numbers.
3. Showcase the resultant quotient to the user.
3. Code Program
// Declaring the main package as the kickoff point for our Go application.
package main
// Incorporating the fmt package to cater to input and output tasks.
import "fmt"
// The core of our program, the main function, commences the execution.
func main() {
// Three float64 variables are initialized to store the user's numbers and the division result.
var num1, num2, quotient float64
// The user is prompted to input the first number, which is then stored in num1.
fmt.Print("Enter the dividend (the number to be divided): ")
fmt.Scan(&num1)
// Similarly, the user is prompted for the second number (the divisor), saved in num2.
fmt.Print("Enter the divisor (the number by which division is to be performed): ")
fmt.Scan(&num2)
// A check to ensure the divisor is not zero to avoid division by zero error.
if num2 == 0 {
fmt.Println("Error: Division by zero is undefined!")
return
}
// The division of num1 by num2 is computed, and the result is stowed in quotient.
quotient = num1 / num2
// Displaying the result of the division in a structured manner.
fmt.Printf("The quotient when %v is divided by %v is: %v\n", num1, num2, quotient)
}
Output:
When a user inputs the numbers 20 and 5, the program's display would be: The quotient when 20 is divided by 5 is: 4 If the user inputs 20 and 0, the program would warn: Error: Division by zero is undefined!
4. Step By Step Explanation
1. Package and Import Directives: The journey begins with package main, marking our program's genesis. The fmt package is then summoned for its invaluable input-output tools.
2. Variable Announcements: With the help of the var keyword, we proclaim three float64 variables. These placeholders will hold the user-fed numbers and the ensuing quotient.
3. Gathering User Input: fmt.Print plays the role of the prompter, while fmt.Scan acts as the collector, gathering user responses into the designated variables.
4. Division Mechanics: Prior to division, a vital check ensures the divisor isn't zero to circumvent any mathematical ambiguities. The / operator then conducts the division, with the quotient being lodged in the aptly named quotient variable.
5. Result Exhibition: Lastly, fmt.Printf takes center stage, presenting the outcome of the division to the user in an organized format.
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