Cover Image for Add Two no in C++ program
101 views

Add Two no in C++ program

The simple C++ program that adds two numbers entered by the user:

C++
#include <iostream>

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

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

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

    // Add the two numbers
    sum = num1 + num2;

    // Display the result
    std::cout << "The sum of " << num1 << " and " << num2 << " is: " << sum << std::endl;

    return 0;
}

In this program:

  1. We declare three variables: num1, num2, and sum to store the two numbers entered by the user and the result of their addition, respectively.
  2. We use std::cout and std::cin to display prompts to the user and to input the two numbers.
  3. The + operator is used to add num1 and num2, and the result is stored in the sum variable.
  4. Finally, we use std::cout to display the sum of the two numbers to the user.

When you run this program, it will ask the user for two numbers, add them, and then display the result.

YOU MAY ALSO LIKE...

The Tech Thunder

The Tech Thunder

The Tech Thunder


COMMENTS