C++14 (gcc 8.3) sample

include

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

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

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

sum = num1 + num2;

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

return 0;

}

Explanation:

  1. The #include line is a preprocessor directive that tells the compiler to include the iostream library, which provides input and output functionality.

  2. The int main() line is the starting point of the program. It declares the main function, which is a special function that is automatically called when the program starts running.

  3. Inside the main function, we declare three integer variables: num1, num2, and sum. These variables will hold the user's input and the result of the addition.

  4. The std::cout << "Enter the first number: "; line outputs the message "Enter the first number: " to the console. std::cout is used to print output to the console.

  5. The std::cin >> num1; line reads the user's input from the console and stores it in the num1 variable. std::cin is used to read input from the console.

  6. Similarly, the std::cout << "Enter the second number: "; and std::cin >> num2; lines prompt the user to enter the second number and store it in the num2 variable.

  7. The sum = num1 + num2; line calculates the sum of num1 and num2 and assigns it to the sum variable.

  8. Finally, the std::cout << "The sum of " << num1 << " and " << num2 << " is: " << sum << std::endl; line prints the result to the console. The values of num1, num2, and sum are inserted into the output using the << operator.

  9. The return 0; statement indicates the end of the main function and the program. It returns the value 0 to the operating system, indicating that the program executed successfully.