Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

40,026 questions

51,982 answers

573 users

How to check if a number is automorphic number in Swift

1 Answer

0 votes
import Foundation
/*
    An Automorphic number is a number whose square ends with the same digits
    as the original number. E.g – 5 : 5 * 5 = 25 //ends with 5
*/
class Automorphic
{
	func CheckAutomorphicNumber(num: Int) -> Bool {
        let s = String(num)

        let square = num * num
          
        let last = square % Int(pow(10.0, Double(s.count)))
         
        print(num, "square =", square, terminator:" ")
     
        return last == num
    }

	static func main(_ args: [String])
	{
		let obj: Automorphic? = Automorphic()
		
		let n = 25 // 625
		
		print(obj!.CheckAutomorphicNumber(num: n))
		print(obj!.CheckAutomorphicNumber(num: 5))
		print(obj!.CheckAutomorphicNumber(num: 76))
		print(obj!.CheckAutomorphicNumber(num: 98))
		print(obj!.CheckAutomorphicNumber(num: 376))
	}
}

Automorphic.main([String]())




/*
run:
   
25 square = 625 true
5 square = 25 true
76 square = 5776 true
98 square = 9604 false
376 square = 141376 true
   
*/

 



answered Oct 19, 2021 by avibootz
...