
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Determine Recursively Whether a Given Number is Even or Odd in Go
Steps
- Take a number from the user and store it in a variable.
- Pass the number as an argument to a recursive function.
- Define the base condition as the number to be lesser than 2.
- Otherwise, call the function recursively with the number minus 2.
- Then, return the result and check if the number is even or odd.
- Print the final result.
Enter a number: 124 Number is even! |
Enter a number: 567 Number is odd! |
Example
package main import ( "fmt" ) func check(n int) bool{ if n < 2 { return n % 2 == 0 } return check(n - 2) } func main(){ var number int fmt.Print("Enter a number:") fmt.Scanf("%d", &number) check(number) if check(number)==true { fmt.Println("Number is even!") }else{ fmt.Println("Number is odd!") } }
Output
Enter a number:8 Number is even!
Advertisements