Swift | Program for Addition of two numbers

Swift program to print the Addition of two numbers: Here, we are going to implement program in Swift programming language for the addition of two numbers.
Submitted by Mahima Rao, on September 28, 2018

In this program, we will have an idea - how two numbers can be added and displayed as the output on the screen?

Open XCode terminal and type the following program

import Foundation 

var num1 = 4; 
var num2 = 6; 

var ans = Int(); 
ans = num1+num2; 

print("num1 + num2 = ",ans); 

Output

num1 + num2 = 10 
Program ended with exit code: 0 

Explanation:

  • import Foundation is a library which is used to access all the functions present that header file.
  • var is a data type with num1 as a variable whose value is initialized as 4. Similarly, num2 is a variable with same data type whose value is initialized as 6.
  • Whereas, var ans includes same data type with ans as a variable where the result is stored.
  • print function prints the string with the answer on the output screen.
  • Whereas, "program ended with exit code: 0" at the end shows that there are no further programs to be run.
  • Semicolon ";" is must after every end of the command as similar in C/C++ etc.


Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.