Subtracting numbers in Swift is another core skill every beginner must learn early. Subtraction helps you understand how Swift works with values, variables, and math operations. Even though it feels simple, it is used everywhere in real programs, from tracking remaining balance to calculating differences in scores or time.
In real applications, subtraction appears more often than people think. It is used in finance apps, games, school systems, and even health trackers. If we are building an app to track daily steps, subtraction helps compare today’s steps with yesterday’s. Learning subtraction the traditional way gives you a strong base and makes other topics easier later.
Program 1: Subtracting Two Integers in Swift
This first program shows how to subtract two whole numbers using Swift. It uses predefined integer values and prints the result clearly.
import Foundation
let totalMarks = 80
let lostMarks = 15
let finalMarks = totalMarks - lostMarks
print("Final marks are \(finalMarks)")Swift stores both numbers as integers and subtracts one from the other using the minus operator. The result is saved in a new variable and printed. This is useful for beginners because integers are common in counting, scoring, and everyday logic.
Program 2: Subtracting Decimal Numbers (Double)
This program focuses on subtracting numbers that contain decimal points. This is common when working with money, weight, or measurements.
import Foundation
let walletBalance: Double = 50.75
let purchaseAmount: Double = 18.40
let remainingBalance = walletBalance - purchaseAmount
print("Remaining balance is \(remainingBalance)")Here, Swift uses the Double type to handle decimals accurately. The subtraction works the same way as with integers, which helps beginners feel comfortable moving between number types. This approach is useful in apps that deal with prices or precise values.
Program 3: Subtracting Mixed Numbers (Int and Double)
Sometimes you need to subtract a decimal value from a whole number. Swift requires both values to be the same type, so conversion is needed.
import Foundation
let totalHours = 10
let usedHours = 3.5
let freeHours = Double(totalHours) - usedHours
print("Free hours left: \(freeHours)")The integer is converted into a Double before subtraction. This teaches beginners about Swift’s strict type rules, which help prevent mistakes. Understanding this makes your code safer and more reliable, just like programmers have always preferred.
Program 4: Subtracting Numbers Using a Function
This program shows how subtraction can be placed inside a function. Functions help keep code clean and reusable.
import Foundation
func subtractNumbers(_ first: Int, _ second: Int) -> Int {
return first - second
}
let result = subtractNumbers(30, 12)
print("The result is \(result)")The function takes two numbers, subtracts the second from the first, and returns the result. This is useful when subtraction is needed in many places. Beginners learn how to organize logic properly, which is a classic and trusted coding habit.
Program 5: Subtracting Numbers from User Input
This example shows how to subtract numbers entered by the user. It makes the program interactive and more realistic.
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 difference = firstNumber - secondNumber
print("The difference is \(difference)")Swift reads text input and safely converts it into numbers. If the user enters something invalid, the program uses zero instead of crashing. This is very useful in real apps where user input is unpredictable.
Frequently Asked Questions (FAQ)
This section answers common beginner questions about subtracting numbers in Swift. These questions often come up when learners start practicing.
Q1. Why does Swift care about number types during subtraction?
Swift uses types like Int and Double to keep calculations accurate and safe. This helps prevent hidden errors and keeps programs stable.
Q2. Can Swift subtract negative numbers?
Yes, Swift can subtract negative numbers without any problem. The result follows normal math rules.
Q3. What happens if I subtract a bigger number from a smaller one?
Swift allows it and returns a negative result. This is useful in many real situations, like tracking losses.
Q4. Is subtraction slow in Swift programs?
No, subtraction is very fast. It is one of the simplest operations and is used millions of times in apps.
Conclusion
Subtracting numbers in Swift is simple but very powerful. In this guide, you learned how to subtract integers, decimal numbers, mixed types, use functions, and handle user input. These examples follow traditional programming ideas that have worked well for years.
The best way to learn is by practicing. Change the numbers, write your own examples, and try small experiments. With time, subtraction and other Swift basics will feel natural, helping you grow into a confident and skilled programmer.




