🎓 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 guide, we'll explore the process of developing a Go program that calculates the area of a circle given its radius.
2. Program Overview
Our Go program is structured to:
1. Request the user to input the radius of the circle.
2. Calculate the area using the formula π * radius^2.
3. Present the computed area to the user.
3. Code Program
// The program begins with the declaration of the main package.
package main
// We enlist the fmt package to handle input and output tasks and the math package for mathematical constants and operations.
import (
"fmt"
"math"
)
// Function to determine the area of the circle.
func circleArea(radius float64) float64 {
return math.Pi * radius * radius
}
// The heartbeat of our program is the main function.
func main() {
var radius float64
// Prompting the user to specify the circle's radius.
fmt.Print("Enter the radius of the circle: ")
fmt.Scan(&radius)
// Displaying the computed area to the user.
fmt.Printf("The area of the circle with radius %.2f is: %.2f\n", radius, circleArea(radius))
}
Output:
For instance, if the user inputs a radius of 7, the program's output will elucidate: The area of the circle with radius 7.00 is: 153.94
4. Step By Step Explanation
1. Package and Import Declarations: Our exploration begins with the package main statement. The fmt package ensures smooth I/O operations, while the math package provides the necessary mathematical constants and functions.
2. Area Calculation Function: The circleArea function is straightforward, using the formula π * radius^2 to determine the circle's area.
3. User Input & Displaying Result: A floating-point variable named radius is initialized to store the user's input. Subsequent to this, the user is prompted to enter the circle's radius. The program then calculates and unveils the circle's area using the circleArea function.
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