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

SwiftServer Side ProgrammingProgramming

Swift 4 Online Training

38 Lectures 1 hours

Swift Basics: Learn to Code from Scratch [For Beginners]

13 Lectures 2 hours

How to create Tic-Tac-Toe Game for iPhone using Swift in Xcode - iOS App

7 Lectures 1 hours

This tutorial will discuss how to write a Swift program to extract the last two digits from the given year.

To extract the last digits from the number we use the modulo operator(%). Or we can say that to extract the last digits from the given year we can use the modulo operator(%). So the mathematical concept of extracting last two digits from the given year is to divide the given year by a number so the remainder should be the same as the last digit of the given year. So to extract a last digit from the given year we divide the given year by 10 so the remainder will be the same as the last digit. For example −

Year = 1996
1996 % 10 = 6


Hence, the remainder 6 is the same as the last digit which is 6.

Similarly, to extract the last two digits of the given year we need to divide the given year by 100. For example.

Year = 1823
1823 % 100 = 23


Hence, the remainder 23 is the same as the last digit which is 23.

Syntax

Following is the syntax −

Year % 100


Below is a demo nstration of the same −

Input

Year = 1298


Output

The desired output would be −

Last two digits are 98


Algorithm

Following is the algorithm −

Step 1 − Declare a variable to store the year.

Step 2 − Extract the last two digits of the year using the modulo operator −

var result = myYear % 100


Step 3 − Print the output

Example 1

The following program shows how to extract the last two digits from the given year using modulo operator(%).

import Foundation
import Glibc

var myYear = 1863

// Finding the last two digits of the given year
// Using % operator
var result = myYear % 100

print("The last two digits of \(myYear) year are:", result)


Output

The last two digits of 1863 year are: 63


Here in the above, we exact the last two digits of the given year using the following code −

var result = myYear % 100 // 1863 % 100 = 63


Here 1863 is divided by 100 so the remainder is 63

Example 2

Finding last digit using str.suffix() function

We can also extract the last two digits from the given year using the suffix() function of string. This function is used to find a substring from the given string up to the specified length, starting from the end of the specified string, and count the characters in the backward direction.

Syntax

Following is the syntax −

Str.suffix(length)


Algorithm

Following is the algorithm −

Step 1 − Declare a variable to store the year.

Step 2 − Find the last to digits of the year using suffix() function −

var result = myYear.suffix(2)


Step 3 − Print the output

The following program shows how t to extract the last two digits from the given year using suffix() function.

import Foundation
import Glibc
var myYear = "2346"

// Finding the last two digits of the given year
// Using suffix() function
var result = myYear.suffix(2)

print("The last two digits of \(myYear) year are:", result)


Output

The last two digits of 2346 year are: 46


Here, in the above code, we extract the last two digits of the given year which is 2346 using the suffix() function. The suffix function will return the last words of the given string according to the passed length(that is 2). The suffix() function is a string function so we entered the given year in the form of a string and get the output 46.

Updated on 10-Oct-2022 14:48:15