w3resource

C++ Exercises: Print the sum of two numbers


Sum of Two Numbers

Write a program in C++ to print the sum of two numbers.

Visual Presentation:

C++ Exercises: Print the sum of two numbers

Sample Solution:

C++ Code :

#include <iostream> // Including the input-output stream header file

using namespace std; // Using the standard namespace

int main() // Start of the main function
{
    cout << "\n\n Print the sum of two numbers :\n"; // Outputting a message for displaying the sum of two numbers
	cout << "-----------------------------------\n"; // Outputting a separator line
    cout << " The sum of 29 and 30 is : "<< 29+30 <<"\n\n" ; // Calculating and outputting the sum of 29 and 30

} // End of the main function

Sample Output:

 Print the sum of two numbers :                                        
-----------------------------------                                    
 The sum of 29 and 30 is : 59

Flowchart:

Flowchart: Print the sum of two numbers

For more Practice: Solve these Related Problems:

  • Write a C++ program to compute the sum of two numbers read from the user, but perform the addition without using the '+' operator.
  • Write a C++ program that calculates the sum of two numbers and prints the result in a formatted box using special characters.
  • Write a C++ program that reads two numbers as strings, converts them to integers, and then prints their sum.
  • Write a C++ program to add two numbers where one of the numbers is generated randomly and the other is input by the user.

Go to:


PREV : Write a program in C++ to print a welcome text in a separate line.
NEXT : Size of Fundamental Data Types.

C++ Code Editor:



Have another way to solve this solution? Contribute your code (and comments) through Disqus.

What is the difficulty level of this exercise?



Follow us on Facebook and Twitter for latest update.