Go Program To Check Whether a Number is Even or Odd

In this post, we will learn how to check whether a number is even or odd using Go Programming language.

Hello coders, in this post we will learn how to check whether a number is even or odd using Go Programming language. 

Go Program To Check Whether a Number is Even or Odd

Any number that can be exactly divided by 2 is called as an Even Number. For example: 2, 16, 32, 258, . . . etc.

Any number that can not be exactly divided by 2 is called as an Odd Number. For example:3, 17, 51, 243, . . . etc.

So, without further delay, let's begin this tutorial.

Table Of Contents 👊

Go Program To Check Whether a Number is Even or Odd

// Go Program To Check Whether a Number is Even or Odd
package main
import "fmt"

func main(){
  var num int
  
  // Asking for Input
  fmt.Print("Enter a number: ")
  fmt.Scan(&num)
  
  // Logic
  if (num % 2 == 0){
    fmt.Println(num, "is an Even Number.")
  } else{
    fmt.Println(num, "is an Odd Number.")
  }
}

Output 1

Enter a number: 32
32 is an Even Number.

Output 2

Enter a number: 17
17 is an Odd Number.

How Does This Program Work ?

  var num int

In this program, we have declared a int data type variable named as num.

  // Asking for Input
  fmt.Print("Enter a number: ")
  fmt.Scan(&num)

Then, the user is asked to enter a number. This number will get stored in num named variable.

  if (num % 2 == 0){
    fmt.Println(num, "is an Even Number.")
  } else{
    fmt.Println(num, "is an Odd Number.")
  }

Now, we check whether the number is exactly divisible by 2 or not. If the entered number is completely divisible by 2, then it is an even number. Otherwise it will be an odd number. 

The output is printed to the screen using fmt.Println() function.

Conclusion

I hope after reading this post, you clearly understood how to check whether a number is even or odd using Go Programming language.

If you have any doubt regarding the problem, let us know in the comment section.

Sloth Coders is a Learning Platform for Coders, Programmers and Developers to learn from the basics to advance of Coding of different langauges(python, Java, Javascript and many more).

Post a Comment