How can we write a C++ program to add two numbers?

  • Google+ icon
  • LinkedIn icon

To write a C++ program that adds two numbers we need to write the following code:

We write a line which calls a library that has functions we need:


(Then we create a function that does our sum:)

int main()

(Here we read the two values we need for our sum, called x and y. These can be chosen by the user and written from the keyboard:)

{ cin>>x,y;

(In the next lines we declare a variable called S which stands for sum and then we give it the value of x+y:)

int S;

S= x+y;

(In the end, we return the value of S and close the brackets of the function)



Cosmin B. A Level Maths tutor, GCSE Maths tutor, GCSE Computing tutor...

About the author

is an online A Level TBA tutor with MyTutor studying at University College London University

Still stuck? Get one-to-one help from a personally interviewed subject specialist.

95% of our customers rate us

Browse tutors

We use cookies to improve your site experience. By continuing to use this website, we'll assume that you're OK with this. Dismiss