🎓 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
Determining if a number is even or odd is a basic computational task and an introduction to conditional structures in programming. In this article, we'll take a closer look at how to create a program in Golang that evaluates the parity of a number given by the user.
2. Program Overview
The primary objectives of our Go program are to:
1. Prompt the user to provide a number.
2. Evaluate if the entered number is even or odd.
3. Display the assessment to the user.
3. Code Program
// The entry point of our application is denoted by the main package declaration.
package main
// The fmt package is harnessed for managing input and output operations.
import "fmt"
// Execution of our program begins with the main function.
func main() {
// We declare an int variable to store the user's input.
var number int
// The user is requested to input a number, which is then stored in the number variable.
fmt.Print("Enter a number: ")
fmt.Scan(&number)
// A conditional statement checks the remainder when the number is divided by 2.
if number % 2 == 0 {
fmt.Println("The number is even.")
} else {
fmt.Println("The number is odd.")
}
}
Output:
If the user enters the number 5, the program's output will be: The number is odd. However, if the user provides 4 as input, the display would read: The number is even.
4. Step By Step Explanation
1. Package and Import Directives: We start with the package main to signify our program's initiation point. For handling inputs and outputs, the fmt package is integrated.
2. Variable Declaration: Using the var keyword, an integer variable named number is initialized to store the number provided by the user.
3. Receiving User Input: With fmt.Print, we provide a prompt to the user, and with fmt.Scan, the user's input is stored in the number variable.
4. Even-Odd Evaluation: To determine the number's parity, the remainder of the number when divided by 2 is checked. If the remainder is 0, the number is even; otherwise, it's odd.
5. Displaying the Result: Based on the evaluation, an appropriate message is printed, informing the user whether the number is even or odd.
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