In this post, we will learn how to write a program to check whether a number is even or odd using the Go programming language.
Any number that is completely divisible by 2 is called an even number, whereas the number that leaves a remainder when divided by 2 is called an odd number.
The below program asks the user to enter a positive integer, then uses the if… else condition to check whether the entered integer is exactly divisible by 2 or not.
If the condition is true, then the entered integer is an even number; otherwise, it is an odd number.
So, without further ado, let’s begin this tutorial.
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 fmt.Print("Enter an integer: ") fmt.Scanln(&num) if num%2 == 0 { fmt.Println(num, " is an even number.") } else { fmt.Println(num, "is an odd number.") } }
Output
Enter an integer: 31
31 is an odd number.
How Does This Program Work?
var num int
We have declared an integer data type variable named num.
fmt.Print("Enter an integer: ") fmt.Scanln(&num)
The user is asked to enter a positive integer to find out whether it’s an even number or an odd number. The integer entered by the user is 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 take advantage of the if…else condition to check whether the integer is an even number or an odd number.
We use the % modulus operator to find the remainder when num is divided by 2. If the remainder is 0, then it means the integer is completely divisible by 2. Therefore, the integer is an even number.
If the above condition is false, then it means that the integer leaves a remainder other than 0. Therefore, in such a case, the entered integer will be an odd number.
Conclusion
In this post, you learned how to write a program to check whether a number is even or odd using the Go programming language.
If you have any doubts regarding the tutorial, leave your queries in the comment section.
Alice was a young and ambitious ninja who had always been fascinated by the art of jutsu. She spent countless hours training and perfecting her skills, hoping to one day become a powerful ninja like her heroes. One day, she learned a new jutsu that required chakra to be used. This jutsu was different from anything she had ever seen before, as its power was determined by the intensity of the jutsu.
Alice was eager to try out this new jutsu, but first, she needed to calculate the amount of chakra required for a jutsu with a given intensity.
Alice, a diligent ninja, was deep in her training when she realized she needed to calculate the amount of chakra required for a jutsu of a given intensity. As much as she wanted to do it herself, she was pressed for time and couldn’t afford to interrupt her practice.
That’s when Alice turned to you for help. Knowing your expertise in jutsu, she trusted you to calculate the amount of chakra needed for her new jutsu.
Will you carefully calculate the amount of chakra required and share the result with Alice? So, that Alice continued to practice tirelessly, determined to become the best ninja she could be.
Input Format
The first line of input contains a single integer T denoting the number of test cases.
Each test case contains an integer N in a separate line.
Constraints
1≤T≤10^3
1≤N≤10^9
Output Format
For each test case, print a single integer denoting the answer to that test case a new line.
Sample Input 0
1
10
Sample Output 0
6
Explanation 0
For 10, the divisors are 1, 2, 5 and 10. Hence, the sum of odd divisors is 1+5=6.