ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

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.
ADVERTISEMENT




Comments and Discussions

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.