Go language program to take age and tell whether its infant or child or adult

Views: 112   

Following program shows you how to take age and tell whether its infant or child or adult.
In this program we get age from user, and if the age is less than or equal to 0 it prints invalid input, and if the age is between 1 to 5 it shows infant and if the age is between 6 to 10 it prints child otherwise it prints adult.

package main

import "fmt"

func main() {
  var age  int
  fmt.Print("Please enter your age:")
  fmt.Scanf("%d",&age)
  if (age <= 0) {
    fmt.Println("Invalid input")
  } else if (age >= 1 && age <= 5){
    fmt.Println("Infant")
  } else if (age >= 6 && age <= 10) {
    fmt.Println("Child")
  } else {
    fmt.Println("Adult")
  }
  }

Output:

Example1:

Please enter your age: 3
Infant

Example2:

Please enter your age: 7
Child

Example3:

Please enter your age: 43
Adult

Example4:

Please enter your age: -3
Invalid input
On By