Swift program to display prime numbers between intervals using function


This tutorial will discuss how to write swift program to display prime numbers between intervals using function.

Prime numbers are those numbers that are greater than 1 and has exactly two factors that is 1 and the number itself. For example, 7 is the prime number because it has only two factors that are 1 and 7.

Below is a demonstration of the same −

Input

Suppose our given input is −

Lower Interval = 18
Upper Interval = 42

Output

The desired output would be −

Prime numbers between 18 to 42 are
19
23
29
31
37
41

Algorithm

Following is the algorithm −

Step 1 − Create a function

Step 2 − Iterate through each number in the given interval.

Step 3 − Skip 1 and 0 because they are not prime numbers

Step 4 − Declare a flag variable. It represent if the given number is prime or not. If the flag = 0, then the number is prime number. Otherwise not.

Step 5 − Run another for loop to check the given number is prime number or not.

Step 6 − Declare lower and upper interval.

Step 7 − Call the function and pass lower and upper interval as a parameter.

Step 8 − Print the output.

Example 1

The following program shows how to display prime numbers between intervals using function.

import Foundation
import Glibc
func FindPrimeNumber(lowerInterval: Int, upperInterval: Int){
   for x in stride(from:lowerInterval, to: upperInterval+1, by:1){
      if (x == 0 || x == 1){
         continue
      }
      // Flag tells if x is a prime number or not
      var flag = 0
      for y in stride(from:2, to: x/2 + 1, by:1){
         if ((x % y) == 0){
            flag = 1
            break
         }
      }
      // If the flag = 0 then the number is prime number
      if (flag == 0){
         print(x)
      }
   }
}
var x = 5
var y = 19
print("Prime numbers in between (x) to (y) are:")
FindPrimeNumber(lowerInterval: x, upperInterval: y)

Output

Prime numbers in between 5 to 19 are:
5
7
11
13
17
19

Here, in the above code, we create a function named FindPrimeNumber(). In this function, we find prime number in between 5 to 19. Here, we use nested for loop along with stride() function. Outer for loop is used to iterate through each number present in between the given interval and inner for loop is used to check the given number is prime number or not by finding the remainder.

for y in stride(from:2, to: x/2 + 1, by:1){
   if ((x % y) == 0){
      flag = 1
      break
   }
}

If the remainder is equal to 0, then the number is not prime number, so flag = 1 and break the loop. If the remainder is not equal to 0, then the number is prime number, so the flag = 0 and display that number. This process will continue till upper Interval.

Example 2

The following program shows how to display prime numbers between intervals using function.

import Foundation
import Glibc
func FindPrimeNumber(number: Int)->Int{
   var flag = 1
   for x in stride(from:2, to: number/2 + 1, by:1){
      if ((number % x) == 0){
         flag = 0
         break
      }
   }
   return flag
}
var x = 1
var y = 19
print("Prime numbers in between (x) to (y) are:")
for k in x...y{
   if (k == 0 || k == 1){
      continue
   }
   let flag = FindPrimeNumber(number: k)
   if (flag == 1){
      print(k)
   }
}

Output

Prime numbers in between 1 to 19 are:
2
3
5
7
11
13
17
19

Updated on: 13-Dec-2022

137 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements