Golang Program to extract the last two digits from the given year


In this tutorial we will discuss how to write a GO program to extract the last two digits of a given year.

This program takes any year as input and prints its last two digits. You need to extract the last two digits from a given year, by using the modulus operation.

Modulus Operation

The % operator is the modulus operator, which returns the remainder rather than the quotient after division. This is useful for finding numbers that are multiples of the same number. As the name suggests where there is an execution to be operated required to deal with digits of the number, modulo operator plays a vital role. Here the goal is to extract the last digits of a number.

In order to extract the last two digits that number with which the given number has to be divided must be 100.

Extract the last two digits of a given year within the function

Syntax

var variableName integer = var year int

We are using the arithmetic operator modulus % to find the last two digits.

lasttwoDigits := year % 1e2

1e2 stands for 1*100

Algorithm

Step 1 − Import the package fmt

Step 2 − Start function main()

Step 3 − Declare the variable year

Step 4 − The condition for the check is year % 1e2 (1e2 is a number expressed using scientific notation and it means 1 multiplied by 10 to the 2nd power (e is exponent). so 1e2 equals 1*100)

Step 5 − Based on the above condition, the last two digits of a year are extracted

Step 6 − Print the output.

Example

The following program code shows how to extract the last two digits of any given year within the function

package main // fmt package provides the function to print anything import "fmt" func main() { // define the variable var year int // initializing the variable year = 1897 fmt.Println("Program to extract the last two digits of a given year within the function.") // use modulus operator to extract last two digits // % modulo-divides two variables lasttwoDigits := year % 1e2 // 1e2 stands for 1*100 // printing the results fmt.Println("Year =", year) fmt.Println("Last 2 digits is : ", lasttwoDigits) }

Output

Program to extract the last two digits of a given year within the function.
Year = 1897
Last 2 digits is :  97

Description of code

  • In the above program, we declare the package main.

  • Here, we imported the fmt package that includes the files of package fmt then we can use a function related to the fmt package.

  • In the main() function, we declare and initialize a variable integer var year int

  • Next we used the modulus operator to extract the last two digits of the given year

  • Printing the last two digits of the year using fmt.Println on the console screen.

Extract the last two digits of any given year in 2 separate functions

Syntax

func d(year int) (lastTwo int)

We are using the arithmetic operator modulus % to find the last two digits.

Algorithm

Step 1 − Import the package fmt

Step 2 − Initializing the variables.

Step 3 − Extracting the last two digits by using year % 100

Step 4 − Printing the result

Example

package main // fmt package provides the function to print anything import "fmt" // This function to extract the last two digits of a given year in the function parameter func d(year int) (lastTwo int) { // use modulus operator to extract last two digits fmt.Println("The Year = ", year) lastTwo = year % 100 return } func main() { // define the variable var year, lastTwo int // initializing the variables year = 2013 fmt.Println("Program to extract the last two digits of a given year in 2 separate functions.") lastTwo = d(year) // printing the results fmt.Println("Last 2 digits is : ", lastTwo) }

Output

Program to extract the last two digits of a given year in 2 separate functions.
The Year =  2013
Last 2 digits is :  13

Description of code

  • First we Import the package fmt

  • Then we are create func d() function to extract the last two digits of a given year

  • Then we start the function main()

  • var year, lastTwo int − In this line of code we have declared and initialized the integers

  • Then we are calling d() function which we have created outside the function and store it in the second integer variable lastTwo

  • And last printing the last two digits of a given year on the console screen using fmt.Println.

Conclusion

Using the above code we can successfully extract the last two digits of any given year using Go language program.

Updated on: 30-Nov-2022

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements