How to Input Two Numbers and Print Their Sum in C++
How to Input Two Numbers and Print Their Sum in C++

How to Input Two Numbers and Print Their Sum in C++

In C++, you can easily perform arithmetic operations by taking input from the user and displaying the result. One common task is to input two numbers and calculate their sum. This blog post will explain the process in a step-by-step manner.

Setting Up the Environment

First, ensure that you have a C++ development environment set up. You can use an Integrated Development Environment (IDE) like Code::Blocks, Visual Studio, or a text editor like Sublime Text with a C++ compiler. Once you have your environment ready, you can start writing the code.

Writing the C++ Program

1. Begin by including the iostream header file.

This header file allows you to use input and output functions like cin and cout. The #include directive helps in importing this library. Include the Necessary Header Files:

C++
#include <iostream>

2. Use the Standard Namespace:

You will write using namespace std; after including the header file. This line allows you to avoid prefixing standard library functions with std::. It makes the code cleaner and easier to read.

C++
   using namespace std;

3. Define the main Function:

The main function serves as the entry point of your C++ program. All the code that you want to execute will be placed within this function.

C++
int main() {

4. Declare Variables to Store the Numbers

Inside the main function, declare two integer variables. These variables will hold the values of the numbers that the user inputs. Additionally, declare a third variable to store the sum of the two numbers.

C++
int num1, num2, sum;

5. Prompt the User for Input

Use the cout statement to display a message asking the user to input the first number. Then, use the cin function to read the input from the user and store it in num1. Repeat this process for the second number and store it in num2.

C++
cout << "Enter the first number: ";
cin >> num1;

cout << "Enter the second number: ";
cin >> num2;

6. Calculate the Sum

Add the two numbers and store the result in the sum variable. The addition operation uses the + operator.

C++
sum = num1 + num2;

7. Display the Result

After calculating the sum, display the result using the cout function. You can combine text and the sum value in a single statement.

C++
cout << "The sum of " << num1 << " and " << num2 << " is: " << sum << endl;

8. Return 0 to Indicate Successful Execution

Finally, return 0 from the main function. Returning 0 indicates that the program executed successfully.

C++
return 0;
}

Complete Code Example

Here is the complete code for the program:

C++
#include <iostream>
using namespace std;

int main() {
    int num1, num2, sum;

    cout << "Enter the first number: ";
    cin >> num1;

    cout << "Enter the second number: ";
    cin >> num2;

    sum = num1 + num2;

    cout << "The sum of " << num1 << " and " << num2 << " is: " << sum << endl;

    return 0;
}

Explanation of the Program
  • The #include directive includes the input/output stream library, which allows you to use cin and cout.
  • The using namespace std; line allows you to use standard library functions without prefixing them with std::.
  • The int main() function is the entry point of the program.
  • The int num1, num2, sum; line declares three integer variables to store the input numbers and their sum.
  • The cout and cin statements handle user interaction by prompting for input and storing the entered values.
  • The sum = num1 + num2; line performs the addition of the two numbers.
  • The cout statement displays the result to the user.
  • The return 0; statement indicates successful execution of the program.

Summary

You have now written a simple C++ program that takes two numbers as input and prints their sum. This program demonstrates the basics of input/output operations, variable declaration, and arithmetic operations in C++. You can experiment with other operations like subtraction, multiplication, and division using similar concepts.


Discover more from lounge coder

Subscribe to get the latest posts sent to your email.

Leave a Reply

Your email address will not be published. Required fields are marked *

Discover more from lounge coder

Subscribe now to keep reading and get access to the full archive.

Continue reading