To Calculate The Sum Of Two Numbers in C/C++

LEVEL: Beginner

Whats in this post?

  1. Code
  2. Output
  3. Background and Explanation of the code
  4. Download Code


To copy code: Click  View Source “View Source” on the right of this code snippet.

Background and Explanation of the code

1. The first line specifies the comments describing the purpose of the statement of the program. It is non executable statement.

2. The second line contains a preprocessor directive that instructs the compiler to include the file at this point. This file must be included in the program to perform any input/output operation.
Similarly, the header file is handled.

3. The fifth line main() is the entry point of the program execution. The parenthesis after main() indicates that it is a function. Every C++ program must contain only main() function. When a program is executed, the compiler searches for the main() function to start the execution from that point.
It is necessary to specify the return data type of every function used in C++.

4. The last statement return 0; tells main() to return a value 0 to the calling environment, from where the calling environment comes to know that the program has executed successfully.

5. The remaining lines comprises the set of statements which includes variable declaration input statements,output statements and assignment statements enclosed in braces where the beginning is specified by starting brace ‘{‘ and end is specified by closing brace ‘}’.

The function ‘cin’ is used to input any value from the keyboard and ‘cout’ statement is used to display any message or value on the screen.

On executing this C++ program ,we get:
Enter Number a = 12 (press enter)
Enter Number b= 8(press enter)
Sum is 20