- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- 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

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

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.

- Related Questions & Answers
- How to extract the last 4 characters from NSString?
- MySQL extract year from date format?
- C++ Program to find joining year from the course year lists
- Python – How to Extract all the digits from a String
- Program to find last two digits of 2^n in C++
- C++ program to convert all digits from the given range into words
- Extract the Day / Month / Year from a Timestamp in PHP MySQL?
- Extract digits from Tuple list Python
- Python program to extract only the numbers from a list which have some specific digits
- Program to find last two digits of Nth Fibonacci number in C++
- How to extract the last n characters from a string using Java?
- Python Pandas - Extract year from the DateTimeIndex with specific time series frequency
- Java Program to subtract 1 year from the calendar
- Display first two digits of year in Java (two-digit century)
- How can we extract the Year and Month from a date in MySQL?
- C++ Program to Sum the digits of a given number