Get up to 80 % extra points for free! More info:

Solved tasks for Swift lessons 1-2

In the previous lesson, Variables, type system and parsing in Swift, we learned basic data types of Swift. These were Int, Double, and String.

The following exercises will help you put your knowledge of Swift to the test. Based off of what you've learned from previous lessons. Try to solve them on your own, but know that you could always download the working solutions under the article. Nonetheless, beware that when you view the solution without solving the exercise, you won't learn anything :)

If you find yourself stuck at some point, you should always try going back to check previous tutorials first and figure the problem out by yourself.

Easy exercise

Create an application which will ask for a username and then for their ability. Then the program will print "name is ability", see image below.

The application screen sample:

Hi, what is your name?
Bill Gates
What are you like?
ultraepic rich
Bill Gates is ultraepic rich

Intermediate exercise

Create an application which will ask for an integer and prints its second power (x squared).

The application screen sample:

Enter a number, and I'll square it:
64
Result:4096.0

Advanced exercise

Create an application that will ask for a circle's radius. Then print its circumference and its area.

The application screen sample:

Enter the circle's radius (cm):
12.1
The circle's circumference based on the given radius is: 75.988cm.
Area of the circle is: 459.7274cm^2

In the next lesson, More on the Swift type system: Data types, we'll talk some more about type systems, and introduce you all to more data types.


 

Did you have a problem with anything? Download the sample application below and compare it with your project, you will find the error easily.

Download

By downloading the following file, you agree to the license terms

Downloaded 5x (103.34 kB)
Application includes source codes in language Swift

 

In order to absolve this article, please pass all exercises above by submitting them.

Previous article
Variables, type system and parsing in Swift
All articles in this section
Swift Basic Constructs
Skip article
(not recommended)
More on the Swift type system: Data types
Article has been written for you by Samuel Kodytek
Avatar
User rating:
2 votes
I'm intereseted in JVM languages and Swift
Activities