Found 517 Articles for Swift

Swift Program to Check whether a number is a Perfect Cube or not

Ankita Saini
Updated on 16-Feb-2023 16:43:03

496 Views

A number is a perfect cube if the result of the three time multiplication of a whole number is the number itself. For example: number 5 is a per Number = 125 125 = 5*5*5 Here 125 is a perfect cube. Number = 98 Here 98 is not a perfect cube. In this article, we will learn how to write a swift program to check whether a number is a perfect cube or not. Method 1: Using user defined function To check if the given number is a perfect cube or not, we create a function that check each number ... Read More

Swift Program to Check if the given number is Perfect number or not

Ankita Saini
Updated on 16-Feb-2023 16:41:41

372 Views

A perfect number is a positive number that is equal to the sum of its factors, excluding the number itself. For example − Number = 6 Proper divisors = 1, 2, 3 Hence, 6 is the perfect number because the sum of its divisors is 6(1+2+3) Number = 10 Proper divisors = 1, 2, 5 Hence, 10 is not a perfect number because the sum of its divisors is 8(1+2+5) In this article, we will learn how to write a swift program to check if the given number is a perfect number or not. Algorithm Step 1 − Create ... Read More

Swift Program to calculate the sum of rows of matrix elements

Ankita Saini
Updated on 16-Feb-2023 16:40:50

218 Views

A matrix is an arrangement of numbers in rows and columns. Matrix can be of various type like square matrix, horizontal matrix, vertical matrix etc.In this article, we will learn how to write a swift program to calculate the sum of rows of matrix elements. Here we are using the following methods to find the sum of the rows elements − Using nested for-in loop Using in-built function Method 1: Using nested for-in loop Here we use nested for-in loop to find the sum of rows of matrix elements. Algorithm Step 1 − Create a function. Step 2 ... Read More

Swift Program to Calculate the Sum of Matrix Elements

Ankita Saini
Updated on 16-Feb-2023 16:39:09

192 Views

A matrix is a rectangular array of numbers. Or we can say that a matrix is an arrangement of numbers in rows and columns. Matrix can be of various type like square matrix, horizontal matrix, vertical matrix etc. In this article, we will learn how to write a swift program to calculate the sum of matrix elements. So here, using the following methods we calculate the sum of matrix elements. Sum of square matrix elements Sum of any matrix elements For example, we have the following matrix − $\mathrm{Matrix\:=\:\begin{bmatrix}1 & 4 & 5 & 6ewline4 & 7 ... Read More

Swift Program to calculate the sum of columns of matrix elements

Ankita Saini
Updated on 16-Feb-2023 16:38:01

425 Views

A matrix is an arrangement of numbers in rows and columns. The matrix can be of various types like square, horizontal, vertical, etc. In this article, we will learn how to write a swift program to calculate the sum of columns of matrix elements. Here we are using the following methods to find the sum of the column’s elements − Using a nested for-in loop Using in-built function Method 1: Using nested for-in loop Here we use a nested for-in loop to find the sum of columns of matrix elements. Algorithm Step 1 − Create a function. Step ... Read More

Swift Program to Show Overloading of Methods in Class

Ankita Saini
Updated on 08-Feb-2023 20:07:01

189 Views

Method overloading is a technique that allows us to create multiple methods of same name within a single class but with different parameter types or different number of parameters. The specific method that is called is determined by the types or number of arguments passed to it at the time of the method call. For example − class Book { // Methods func Author(){} func Author(x: Int)->Int{} func Author(x: String)->String{} func Author(a: Int, b: String)->Int{} } Here, the class Book has four overloaded methods. ... Read More

Swift Program to Iterate Over an Array

Ankita Saini
Updated on 08-Feb-2023 20:09:08

3K+ Views

An array is used to store elements of same data type in an order. In this article, we will learn how to write a swift program to iterate over an array. Here we use the following methods to iterate through each element of the array − Using for-in loop Using forEach() function Using while loop Using enumerated() function Method 1: Using for-in loop To iterate over an array we can use for-in loop. Syntax for value in Sequence { // statement } Here, the value parameter contains an element of the array during the ... Read More

Swift Program to insert multiple elements into the array from the specified index

Ankita Saini
Updated on 08-Feb-2023 20:14:53

877 Views

An array is used to store elements of same data type. In this article, we will learn how to write a swift program to insert multiple elements into the array from the specified index. Here we use the following methods to insert multiple elements into the array from the specified index − Using Index Using insert(contentsOf:at:) function Method 1: Using Index Here we use square brackets[] along with rang operators to insert multiple elements into the array at the specified index. Algorithm Step 1 − Create an array of integer. Step 2 − Create another array ... Read More

Swift Program to get array elements after N elements

Ankita Saini
Updated on 08-Feb-2023 20:15:22

307 Views

An array is used to store elements of same data type in an order. So now we extract the array elements after N elements. For example − Array : [2, 4, 5, 6, 7, 9] N = 3 ResultArray = [6, 7, 9] In this article, we will learn how to write a swift program to get array elements after N elements. Algorithm Step 1 − Create function. Step 2 − Create a new array using Array() initializer that containing elements after N elements. Step 3 − Return the resultant array. Step 4 − Create an array and a ... Read More

Swift Program to find the index of the first occurrence of the specified item in the array

Ankita Saini
Updated on 08-Feb-2023 20:16:08

674 Views

An array is used to store elements of the same data type in order. Here we will learn how to write a swift program to find the index of the first occurrence of the specified item in the array. To achieve that we are going to use the following methods to find the index of the first occurrence of the array element − Using user-defined function Using firstIndex() function Method 1: Using user-defined function To find the index of the first occurrence of the specified element in the array we create a function which iterate through each elements ... Read More

Advertisements