
- Swift Tutorial
- Swift - Home
- Swift - Overview
- Swift - Environment
- Swift - Basic Syntax
- Swift - Data Types
- Swift - Variables
- Swift - Optionals
- Swift - Tuples
- Swift - Constants
- Swift - Literals
- Swift - Operators
- Swift - Decision Making
- Swift - Loops
- Swift - Strings
- Swift - Characters
- Swift - Arrays
- Swift - Sets
- Swift - Dictionaries
- Swift - Functions
- Swift - Closures
- Swift - Enumerations
- Swift - Structures
- Swift - Classes
- Swift - Properties
- Swift - Methods
- Swift - Subscripts
- Swift - Inheritance
- Swift - Initialization
- Swift - Deinitialization
- Swift - ARC Overview
- Swift - Optional Chaining
- Swift - Type Casting
- Swift - Extensions
- Swift - Protocols
- Swift - Generics
- Swift - Access Control
- Swift Useful Resources
- Swift - Compile Online
- Swift - Quick Guide
- Swift - Useful Resources
- Swift - Discussion
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.
- Related Articles
- Swift Program to Check if the given number is Perfect number or not
- Swift Program to check whether a given string is Heterogram or not
- Swift Program to Check If a Number is Spy number or not
- Swift Program to check a given number is finite or not
- C++ Program to Check Whether a Number is Prime or Not
- C++ Program to Check Whether a Number is Palindrome or Not
- C Program to Check Whether a Number is Prime or not?
- Java Program to Check Whether a Number is Prime or Not
- Haskell Program to Check Whether a Number is Prime or Not
- Program to check whether a number is Proth number or not in C++
- Write a Golang program to check whether a given number is prime number or not
- How to check whether a number is a prime number or not?
- Swift Program to Check whether the input number is a Neon Number
- Write a Golang program to check whether a given number is a palindrome or not
- Check whether a number is a Fibonacci number or not JavaScript
