Learning how to add two numbers in Swift is one of the first and most important steps for any beginner. Even though addition sounds very simple, it teaches you how Swift works with values, variables, and basic logic. These ideas are the foundation of everything you will build later, from small school projects to real iOS apps used every day in places like Kenya or Zambia.
Addition is used everywhere in programming. Swift uses it when calculating totals, handling money, measuring scores, or working with sensor data. When Harry builds a small calculator app or Hermione tracks points in a learning app, addition is quietly doing its job in the background. By understanding how to add numbers properly, you gain confidence and start thinking like a programmer who respects the basics, just as coding has always been taught.
Program 1: Adding Two Integers in Swift
This first program shows the most basic way to add two whole numbers in Swift. It uses predefined integer values and prints the result to the screen. This is usually the first example beginners see when learning Swift.
import Foundation
let firstNumber = 10
let secondNumber = 20
let sum = firstNumber + secondNumber
print("The sum is \(sum)")In this program, Swift stores two whole numbers in variables and then adds them using the plus operator. The result is stored in another variable called sum, which is then printed. This approach is useful because integers are common in real life, such as counting items or tracking scores, and beginners can clearly see how values flow through the program.
Program 2: Adding Two Decimal Numbers (Floats)
This program focuses on adding numbers that include decimal points. It is helpful when working with prices, measurements, or averages, which are common in everyday applications.
import Foundation
let priceOne: Double = 12.5
let priceTwo: Double = 7.25
let totalPrice = priceOne + priceTwo
print("The total price is \(totalPrice)")Here, Swift uses the Double type to handle decimal numbers. The addition works the same way as with integers, but it allows more precision. Beginners often use this when building apps that deal with money or scientific values, and it shows how Swift carefully separates whole numbers from decimal ones.
Program 3: Adding an Integer and a Decimal Number
Sometimes you need to add different number types together. This program shows how Swift handles adding an integer and a decimal number by converting one type to match the other.
import Foundation
let apples = 5
let weightPerApple = 0.3
let totalWeight = Double(apples) + weightPerApple
print("The total weight is \(totalWeight)")In this example, Swift cannot directly add an Int and a Double, so the integer is converted into a Double. This teaches beginners an important lesson about type safety in Swift. Understanding this helps avoid errors and builds good habits that programmers have followed for many years.
Program 4: Adding Two Numbers Using a Function
This program shows a more organized and traditional approach by using a function. Functions help keep code clean and reusable, which is a respected practice in programming.
import Foundation
func addNumbers(_ first: Int, _ second: Int) -> Int {
return first + second
}
let result = addNumbers(15, 25)
print("The result is \(result)")The function takes two numbers, adds them, and returns the result. This is useful when the same logic is needed in many places, such as in larger apps. Beginners can see how breaking code into small pieces makes programs easier to read and maintain, which is how programmers have worked for generations.
Program 5: Adding Two Numbers from User Input
This final program shows how to get numbers from the user and then add them. This makes the program interactive and closer to real-world use.
import Foundation
print("Enter the first number:")
let firstInput = readLine()!
print("Enter the second number:")
let secondInput = readLine()!
let firstNumber = Int(firstInput) ?? 0
let secondNumber = Int(secondInput) ?? 0
let sum = firstNumber + secondNumber
print("The sum is \(sum)")In this program, Swift reads text from the user and converts it into numbers. If the input is not valid, it safely uses zero. This approach is very practical and teaches beginners how to handle real input, which is essential when building apps for real users, whether in South Africa or Ghana.
Frequently Asked Questions (FAQ)
This section answers common beginner questions about adding two numbers in Swift. These are things new learners often wonder about when they first start coding.
Q1. Why does Swift use different types like Int and Double for numbers?
Swift separates number types to avoid mistakes and keep programs safe. This helps programmers catch errors early and write reliable code.
Q2. Can I add more than two numbers in Swift?
Yes, you can add as many numbers as you want by continuing to use the plus operator or by combining them inside a function.
Q3. What happens if the user enters text instead of a number?
Swift will fail to convert it, which is why safe conversion is important. Using default values helps prevent crashes and keeps programs stable.
Q4. Is addition in Swift fast?
Yes, addition is very fast and efficient. It is one of the simplest operations and is used millions of times in real applications.
Conclusion
Adding two numbers in Swift may seem small, but it carries great importance. Through these examples, you learned how to add integers, decimals, mixed types, use functions, and accept user input. These are classic skills that form the backbone of programming and are still taught the same way today because they work.
By practicing these simple programs, you build confidence and prepare yourself for bigger challenges. Keep experimenting, change the numbers, and try new ideas. With steady practice, Swift will start to feel natural, and your journey as a programmer will grow stronger with every line of code you write.




