Skip to main content

ProwessApps

Learn · Practice · Excel

WAP to add two numbers

C++ Code Example — Basic Programs

ADVERTISEMENT

WAP to add two numbers

Objective

Write a C++ program to add two numbers.

Algorithm / Approach

  1. Include the <iostream> header file for input/output stream.
  2. Declare three integer variables: a, b, and c.
  3. Use cout to prompt the user to enter values.
  4. Use cin to read the input into variables a and b.
  5. Add the variables: c = a + b.
  6. Print the result using cout.
main.cpp
#include<iostream>
using namespace std;
int main() {
 int a, b, c;
 cout<<"Enter Values of A and B: ";
 cin>>a>>b;
 c = a+b;
 cout<<"Result = "<< c<< endl;
 return 0;
}

Expected Output

Enter Values of A and B: 10 20
Result = 30

Explanation of the Program

  • C++ uses streams for input and output. The cout object (character output) is used to display text to the console, and the cin object (character input) is used to read data from the keyboard.
  • The &lt;&lt; operator is the insertion operator (sending data to the screen), while the &gt;&gt; operator is the extraction operator (pulling data from the keyboard).

Complexity

Time Complexity O(1)
Space Complexity O(1)
ADVERTISEMENT