Check the given number is perfect number or not in swift 3.0


Code:

import UIKit

var n = 28
var temp = n
var sum = 0

for i in 1 ... n-1
{
    if(n%i==0)
    {
        sum = sum + i
    }
}
if(sum == temp)
{
    print("\(temp) is perfect number.")
}
else
{
     print("\(temp) is not perfect number.")

}


Result:


Find Factors of given number in swift 3.0

Code:
import UIKit

var n = 12

print("Factors of \(n) are:\n")
for i in 1 ... n
{
    if(n%i==0)
    {
        print("\(i) " )
    }

}

Result:


Sample demo application with text Field, button and label in swift 3.0


Here we are going to create simple iPhone app in iPhone with the help of  text Field, button and label.

We have to make a simple app in which we have to show the content of textfield (entered by user into uitextfield) into label when user taps on button.

First open your Xcode (i am using Xcode 8.1 here). When you open your Xcode it will ask to create new project or open a created project. choose your option.

I am choosing to create new project.

The next screen will open when you choose to create new project.

It will look like given below screen:


Then select for single view application from opened screen. It will show screen like given here:



Fill your Product name, Organisation name, organisation identifier, etc.
And then click next here. Then select the location where you have to store your project. This screen look like below screen:





And then it will show your project screen. Make design as shown in below screen in your storyboard ViewController:


Then take outlet of uitextfield  and label from storyboard and gives it name txt1 and lblresult respectively.

And also generate action of uibutton with name ClickButton.

Now we have to code it according to our requirement. I have to show the content of textfield into label when user click on button.

The whole code of Viewcontroller.swift is given here. :

import UIKit

class ViewController: UIViewController {

    @IBOutlet weak var lblresult: UILabel!
    @IBAction func ClickButton(_ sender: Any) {
        
        lblresult.text = txt1.text
    }
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }
    @IBOutlet weak var txt1: UITextField!

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

}

The whole screen of Xcode after coding is shown here:




Now your app is ready to run. Build and run your project. Choose the device iPhone 5s to run. The screenshot is given here when it runs on simulator:

Check the number is prime or not in swift 3.0

//code

import UIKit

var flag=0
var n = 13

var i = 2
for i in 2 ... n-1
{
    if(n%i==0)
    {
        flag = 1;
        break;
    }
}

if(flag==1)
{
    print("\(n)" + " is not prime")
}
else
{
    print("\(n)" + " is  prime")

}


Result:



C program to check number is prime or not


#include

void main()
{
int i,j,flag=0,n;
printf(“enter any number\n”);
scanf(“%d”,&n);

for(i=2;i
{
if(n%i==0)
{
flag = 1;
break;
}
}

if(flag==1)
{
printf(“%d is not prime\n”,n);
}
else
{
printf(“%d is a prime number\n”,n);
}

}