Found 517 Articles for Swift

Swift Program to Find the Transpose

Ankita Saini
Updated on 20-Dec-2022 16:14:36

339 Views

In this article, we will learn how to write a swift program to find the transpose of a matrix. Transpose of a matrix is calculated by interchange the rows of a matrix into columns or columns of a matrix into rows. For example, we have the following matrix: $$\mathrm{X\:=\:\begin{bmatrix} 13, & 59, & 32, & 67 ewline 23, & 56, &89, & 3 ewline 98, & 3, & 32, & 43 ewline 6, & 90, & 43, &23 \end{bmatrix}}$$ So the transpose of matrix X is − $$\mathrm{X^{T}\:=\:\begin{bmatrix} 13, & 23, & 98, & 6 ewline 59, ... Read More

Swift Program to Find Common Array Elements

Ankita Saini
Updated on 20-Dec-2022 10:56:51

835 Views

This article will teach us how to write a swift program to find common array elements. So here we use the following methods − Using the filter method Using sets But before moving to the methods, we first understand common elements with the help of an example. Suppose we have two arrays − Arr1 = [2, 4, 6, 89, 78] Arr2 = [56, 88, 32, 4, 99, 89] So the common elements between two arrays are [4, 89]. Because they are available in Arr1 and Arr2. Method 1: Using filter(_:) Method To find common elements between two arrays ... Read More

Swift Program to Count the elements of an Array

Ankita Saini
Updated on 20-Dec-2022 10:53:35

197 Views

In this article, we will learn how to write a swift program to count the elements of an array. To count the elements of an array swift provide a count property. This property will return the total number of elements present in the specified array. For example − Arr = [3, 5, 6, 4, 6] so the count will return 5. Syntax var count: Int{get} Or you can also write it as − arrayName.count Here count is the property of the array to count the total elements present inside the array. To use this property we ... Read More

Swift Program to Check if Two Arrays Are Equal or Not

Ankita Saini
Updated on 20-Dec-2022 10:50:47

3K+ Views

In this article, we will learn how to write a swift program to check whether two arrays are equal. To check if two arrays are equal to not either, we are going to use the following two methods − using == operator elementsEqual(_:) method. Both method and operator return true if both the given array has the same elements. Otherwise, they will return false. For example, arr1 = [2, 3, 4, 5] and arr2 = [2, 3, 4, 5] both arrays are equal so both the method and operator will return true. arr1 = [2, 3, 4, ... Read More

Swift Program to Check if an array is empty

Ankita Saini
Updated on 20-Dec-2022 10:38:52

3K+ Views

In this article, we will learn how to write a swift program to check if an array is empty. Here we use two methods: Using isEmpty property Using conditional statements Method 1: Using isEmpty Property To check if an array is empty Swift provides an isEmpty property. This property will return true if the given array is empty or return false if the given array is not empty. For example Arr = [3, 5, 6, 4, 6] so isEmpty property will return false. Syntax var isEmpty: Bool{get} Or you can also write it as − arrayName.isEmpty ... Read More

Swift program to check whether a number can be expressed as sum of two prime numbers

Ankita Saini
Updated on 13-Dec-2022 15:31:37

111 Views

This tutorial will discuss how to write swift program to check whether a number can be expressed as a sum of two prime numbers. Prime numbers are those numbers that are greater than 1 and has exactly two factors that is 1 and the number itself. For example, 7 is the prime number because it has only two factors that are 1 and 7. Here we check that the given number can be expressed as Sum of two prime numbers. For example − Number = 10 So 10 can be expressed as a sum of prime numbers − 3+7 ... Read More

Swift program to get the remainder of float numbers using library function

Ankita Saini
Updated on 13-Dec-2022 14:59:47

202 Views

This tutorial will discuss how to write swift program to get the remainder of float numbers using library function. A number with fractional component is known as float numbers. For example, 34.5, 2.1, 0.2, etc. We can divide two float numbers with each other to find the remainder and quotient. Remainder is the value left after dividing a number by another number. Below is a demonstration of the same − Input Suppose our given input is − Num1 = 0.16 Num2 = 0.04 Output The desired output would be − Remainder = 0.0 To find the remainder of ... Read More

Swift program to print spiral pattern

Ankita Saini
Updated on 13-Dec-2022 14:55:44

239 Views

This tutorial will discuss how to write swift program to print spiral pattern. Numeric pattern is a sequence of numbers which is used to develop different patterns or shapes like pyramid, rectangle, cross, etc. These numeric patterns are generally used to understand or practice the program flow controls, also they are good for logical thinking. To create a spiral pattern of numbers, we can use any of the following methods − Using nested for loop Using stride Function Below is a demonstration of the same − Input Suppose our given input is − Num = 6 Output ... Read More

Swift program to convert array to a string

Ankita Saini
Updated on 13-Dec-2022 14:48:23

2K+ Views

This tutorial will discuss how to write swift program to convert array to a string. String is an ordered collection of characters. For example: “TutorialsPoint”, “Pinky”, etc. To create a string type variable we use String keyword. var str : String Array is a collection of similar data types. Like any array of integer can contain only integer values, it does not accept string values. var arr = [Int] To convert an array to a string we can use any of the following method. Below is a demonstration of the same − Input Suppose our given input is ... Read More

Swift program to convert string to an array

Ankita Saini
Updated on 13-Dec-2022 14:43:17

6K+ Views

This tutorial will discuss how to write swift program to convert string to an array. String is an ordered collection of characters. For example − “TutorialsPoint”, “Pinky”, etc. To create a string type variable we use String keyword. var str : String Array is a collection of similar data types. Like any array of integer can contain only integer values, it does not accept string values. var arr = [Int] To convert string into an array we can use any of the following method. Below is a demonstration of the same − Input Suppose our given input is ... Read More

Advertisements