Swift Program to Compute Quotient and Remainder


This tutorial will discuss how to write a Swift program to compute quotient and remainder.

In a division process, a number is divided by another number to get a resultant number. Or we can say that division is a process in which we divides larger number of group into small number of groups such that each group contain same number of elements. The four main terms of division are −

Dividend − Dividend is a number which we divide.

Divisor − Divisor is a number by which we divide

Quotient − Quotient is the result obtained by the division.

Remainder − Remainder is the leftover number.

Below is a demonstration of the same −

Input

Suppose our given input is −

Dividend 40
Divisor 10

Output

The desired output would be

Quotient 4
Remainder 0


We can find the quotient and remainder using the following two methods −

Method 1 - Using Formula

To find the quotient we have to divide the dividend by diver using / operator whereas to find the remainder we have to use % operator. Here both the dividend and divisor can be of any data type except string and the result we be calculated accordingly.

Formula

Following is the formula of quotient

Quotient = Dividend/Divisor

Following is the formula of remainder

Remainder = Dividend % Divisor

Algorithm

Following is the algorithm −

  • Step 1 − Declare two variables with user-defined/pre-defined values. Here these variables are used to store dividend and divisor.

  • Step 2 − Find the quotient using the following formula and store the result into myQuotient variable.

var myQuotient = myDividend / myDivisor
  • Step 3 − Find the remainder using the following fo rmula and store the result into myRemainder variable.

var myRemainder = myDividend % myDivisor
  • Step 4 − Print output.

Example

The following program shows how to compute quotient and remainder using formula.

import Foundation import Glibc var myDividend = 105 var myDivisor = 2 // Finding quotient var myQuotient = myDividend/myDivisor // Finding remainder var myRemainder = myDividend%myDivisor print("The quotient of \(myDividend) and \(myDivisor) is", myQuotient) print("The remainder of \(myDividend) and \(myDivisor) is", myRemainder)

Output

The quotient of 105 and 2 is 52
The remainder of 105 and 2 is 1

Here in the above code, we have dividend = 105 and divisor = 2. So first we find their quotient using the following formula

Formula

myQuotient = myDividend/myDivisor = 105/2 = 52
Then we find the remainder using the following formula
myRemainder = myDividend%myDivisor = 105%2 = 1
And display the value of quotient and remainder

Method 2 - Using predefined function

Swift also provide a built-in function to find the quotient and remainder. The quotientAndRemainder(dividingBy:) function is used to find the quotient and remainder by dividing the value by the specified value. It always return the result in a tuple, where the first element of the resultant tuple is quotient and second element is the remainder.

Syntax

Following is the syntax of built-in function

DividendVariable. quotientAndRemainder(dividingBy: divisorValue)

Algorithm

Following is the algorithm −

  • Step 1 − Declare a variable named as myDividend and read the value from the user/pre-defined. Here this variable is used to store the value of dividend.

  • Step 2 − Declare a variable named as myDivisor and read the value from the user/pre-defined. Here this variable is used to store the value of divisor.

  • Step 3 − Find the quotient and remainder using the following function and store the result into a tuple.

myDividend.quotientAndRemainder(dividingBy:myDivisor)
  • Step 4 − Print output.

Example

The following program shows how to compute quotient and remainder using function.

import Foundation import Glibc print("Please enter the value of Dividend") var myDividend = Int(readLine()!)! print("Please enter the value of Divisor") var myDivisor = Int(readLine()!)! // Finding the quotient and remainder let(myQuotient, myRemainder) = myDividend.quotientAndRemainder(dividingBy:myDivisor) print("The quotient of \(myDividend) and \(myDivisor) is", myQuotient) print("The remainder of \(myDividend) and \(myDivisor) is", myRemainder)

STDIN input

Please enter the value of Dividend
117
Please enter the value of Divisor
3

Output

The quotient of 117 and 3 is 39
The remainder of 117 and 3 is 0

Here in the above code, we read the value of dividend and divisor from the user using readLine() function and then convert the entered value into integer type using Int() function. Now we find the quotient and remainder of the given values using quotientAndRemainder() function and store the result into a tuple. Display the quotient of 117 and 3 is 39 and remainder is 0.

Updated on: 18-Aug-2022

418 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements