Swift Program to Check whether a number is a Perfect Cube or not


A number is a perfect cube if the result of the three time multiplication of a whole number is the number itself. For example: number 5 is a per

Number = 125

125 = 5*5*5

Here 125 is a perfect cube.

Number = 98

Here 98 is not a perfect cube.

In this article, we will learn how to write a swift program to check whether a number is a perfect cube or not.

Method 1: Using user defined function

To check if the given number is a perfect cube or not, we create a function that check each number from 1 to M if the cube of any of these number is equal to the M, then the number is a perfect cube otherwise not.

Algorithm

Step 1 − Create a function.

Step 2 − Run a for loop from 1 to num.

Step 3 − Use if statement to check if the cube of the current iterator x is equal to the number, then return true. Otherwise return false.

Step 4 − Create a variable to store the input number.

Step 5 − Pass the input number into the function.

Step 6 − Print the output.

Example

Following Swift program to check whether a number is a perfect cube or not.

import Foundation
import Glibc

// Function to check if the number is a perfect cube or not
func checkPerfectCube(num: Int) -> Bool {
   for x in 1...num  {
      if x*x*x == num {
         return true
      }
   }
   return false
}

// Input 1
let N1 = 125
if checkPerfectCube(num: N1) {
   print("\(N1) is a perfect cube.")
} else {
   print("\(N1) is not a perfect cube.")
}

// Input 2
let N2 = 98
if checkPerfectCube(num: N2) {
   print("\(N2) is a perfect cube.")
} else {
   print("\(N2) is not a perfect cube.")
}

Output

125 is a perfect cube.
98 is not a perfect cube.

Here in the above code, we create a function named checkPerfectCube() to check if the given number is a perfect cube or not. This function takes a positive integer as an input and then run a for loop to iterate through each number from 1 to num. Inside the for loop check if the cube of the current iterator ‘x’ is equally to the number. If yes, then this function return true. Otherwise return false.

Method 2: Using in-built function

To check if the given number is a perfect cube or not, we uses cbrt() function to find the cube root of the input value and then find the cube of the cube root. If the cube of the cube root is equal to the number, then the number is the perfect cube. Otherwise not.

Algorithm

Step 1 − Create a function.

Step 2 − Find cube root of the input number using cbrt() function.

Step 3 − Calculate cube of the cube root.

Step 4 − Return true if both cube of the cube root and the input number are equal. Otherwise return false.

Step 5 − Create a variable to store the input number.

Step 6 − Pass the input number into the function.

Step 7 − Print the output.

Example

Following Swift program to check whether a number is a perfect cube or not.

import Foundation
import Glibc

// Function to check if the number is a perfect cube or not
func checkPerfectCube(num: Double) -> Bool {
   let cubeRoot = cbrt(num)
   let cube = cubeRoot * cubeRoot * cubeRoot

 
   // Return return if the cube is equal to num
   
   // Otherwise return false
   return cube == num
}

// Input 1
let M1 = 41.0
if checkPerfectCube(num: M1) {
   print("\(M1) is a perfect cube.")
}
else {
   print("\(M1) is not a perfect cube.")
}

// Input 2
let M2 = 512.0
if checkPerfectCube(num: M2) {
   print("\(M2) is a perfect cube.")
}
else {
   print("\(M2) is not a perfect cube.")
}

Output

41.0 is not a perfect cube.
512.0 is a perfect cube.

Here in the above code, we create a function named checkPerfectCube() to check if the given number is a perfect cube or not. This function takes a positive integer as an input and then find the cube root of the number using cbrt() function. Then, calculate the cube of the resultant cube root. Now check if cube of the cube root is equal to the input number. If they are equal, then this function return true that means the number is a perfect cube. Otherwise return false that means the number is not a perfect cube.

Conclusion

So this is how we can check whether the given number is perfect number or not.

Updated on: 16-Feb-2023

418 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements