Found 517 Articles for Swift

Tech number in Swift

Ankita Saini
Updated on 08-Sep-2023 15:59:09

68 Views

If the given number contains an even number of digits and the digits of the number can be divided into two equal parts from the middle. After dividing the digits sum up the divided digits and find the square of the final sum. If the square is equal to the sum itself then the given number is a tech number otherwise not. Example Demonstration Input 3025 Output Yes the given number is a tech number Input 2341 Output No the given number is not a tech number Here, 3025 is a ... Read More

Swift Program to Print Floyd’s Triangle

Ankita Saini
Updated on 08-Sep-2023 15:56:06

107 Views

Floyd triangle is right angle triangled which is named after Rober Floyd. It is created with the help of natural numbers starting from 1 in the top left corner and then the consecutive numbers are filled in the specified rows. Example Demonstration Input 4 Output 1 2 3 4 5 6 7 8 9 10 Here, the size of Floyd’s triangle is 4. So it will print Floyd’s triangle until 4 rows. In Swift, we can print Floyd’s triangle using the following methods: Using for−in loop Using while loop Using recursive function Algorithm ... Read More

How to Write Calculator Program using Switch Case in Swift

Ankita Saini
Updated on 08-Sep-2023 15:51:06

286 Views

A Calculator is an electronic device which is used to perform different types of mathematical operations like subtraction, addition, division, multiplication, etc. We can also create a calculator using Switch case and simple arithmetic operations in Swift programming. It will also perform the same mathematical operations as the original calculator. Example Demonstration Enter any two numbers: Number 1: 43 Number 2: 234 SIMPLE CALCULATOR 1. Addition 2. Subtraction 3. Multiplication 4. Division 5. Percentage Choose (1/2/3/4/5): 2 Result = -191.0 Here, we first enter the numbers on which we want to perform the ... Read More

How to Swap Pair of Characters in Swift?

Ankita Saini
Updated on 08-Sep-2023 15:35:03

150 Views

Swapping pair of characters is a process of interchanging the position of two characters in the given string. This operation is commonly used in various programming languages and applications to manipulate data. Example Input “mumbai” Output umbmia Input “Prita” Output rPtia Here, we divide the given string into a pair of characters like “mumbai”: “mu”, “mb”, “ai”. Now we swap the positions of the characters: “um”, “bm”, and “ia” and create the resultant string: “umbmia”. Similarly for input 2. In Swift, we can swap the pair of characters present in the given ... Read More

How to Calculate the Volume of a Tetrahedron in Swift?

Ankita Saini
Updated on 08-Sep-2023 15:30:27

49 Views

Tetrahedron is a triangular base pyramid. It is a platonic solid with four triangular faces, six straight edges and four vertex corners. Where each vertex is connected with every other vertex and each face is of an equilateral triangle. In Swift, we can calculate the volume of a tetrahedron using the following formula: Formula $$\mathrm{Area=(x*x*x*\sqrt{2})/12}$$ Here, x represents the sides of the tetrahedron. Algorithm Step 1 − Create a function which takes the side of the tetrahedron as a parameter and returns the volume. Step 2 − Inside the function, we use the mathematical formula to find the volume ... Read More

How to Calculate the Value of nPr in Swift?

Ankita Saini
Updated on 08-Sep-2023 18:37:42

62 Views

nPr is known as n permutation r, where n represents the total numbers and r represents the arrangement of the elements. A permutation is known as the arrangement of elements in a specified order. Elements can arrange either in a sequence or in a linear order, for example, we have a set of elements [2, 4], so the permutation is: [4, 2], [2, 4]. In permutation the order of elements matters whereas in combination the order of the elements does not matters. We can calculate the value of nPr with the help of the following formula: Formula nPr = ... Read More

How to Calculate the Area of a Tetrahedron in Swift?

Ankita Saini
Updated on 08-Sep-2023 15:04:47

38 Views

Tetrahedron is a 3−D triangular pyramid shape whose base is also a triangle. Generally, a tetrahedron contains four equilateral triangles, hence it has interior angles of 60 degrees. In Swift, we can calculate the area of a tetrahedron using the following formula: Formula $$\mathrm{Area=\sqrt{3}*X*X}$$ Here, x represents the side of the tetrahedron. If you wish to find the area of one side of a tetrahedron then you can use the following formula: Formula $$\mathrm{Area\:of\:one\:side\:of\:tetrahedron =(\sqrt{3}*y*y)/4}$$ Here, y represents the side of the tetrahedron. Algorithm Step 1 − Create a function which takes the side of the tetrahedron ... Read More

How to iterate a loop with index and element in Swift?

Nitin Aggarwal
Updated on 07-Sep-2023 14:25:38

379 Views

In this article, you will learn how to iterate a collection using a loop with an index and element in the Swift language. In this article, you will learn how to use the enumerated() method. In Swift, you can use the enumerated() method to iterate over the elements of a collection and access both the index and the element in each iteration of the loop. enumerated() enumerated() is a method in Swift that allows you to iterate over the elements of a collection, such as an array or a dictionary. It returns a series of tuple elements, each of which ... Read More

How to check if an element is in an array?

Nitin Aggarwal
Updated on 07-Sep-2023 14:10:43

72 Views

This article will explain how you can check if an element exists in an array or not in the Swift language. There are several ways to check if an element is in an array in Swift − Using the contains method The contains(_:) method returns true if an array contains the target element. This method can only be used with arrays whose elements conform to the Equatable protocol. Here is an example where we use contains with an array of strings. String conforms to the Equatable protocol, so we can use the contains method here. Algorithm Step 1 - ... Read More

How do I shuffle an array in Swift?

Nitin Aggarwal
Updated on 07-Sep-2023 09:14:06

97 Views

In this article, you will learn how to shuffle an array in the Swift language. In Swift, you can use the shuffle() and shuffled() methods to shuffle the array elements. shuffle() The shuffle() method shuffles the elements of the collection in place, so the original array is modified. shuffled() If you want to create a shuffled copy of the array instead of modifying the original array, you can use the shuffled() method from the Sequence protocol. Here is an example of how to use shuffle() to shuffle the array elements Algorithm Step 1 - Create an input array Step ... Read More

1 2 3 4 5 ... 52 Next
Advertisements