WAP to add two numbers
Objective
Write a C++ program to add two numbers.
Algorithm / Approach
- Include the
<iostream>header file for input/output stream. - Declare three integer variables:
a,b, andc. - Use
coutto prompt the user to enter values. - Use
cinto read the input into variablesaandb. - Add the variables:
c = a + b. - 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
coutobject (character output) is used to display text to the console, and thecinobject (character input) is used to read data from the keyboard. - The
<<operator is the insertion operator (sending data to the screen), while the>>operator is the extraction operator (pulling data from the keyboard).
Complexity
Time Complexity
O(1)
Space Complexity
O(1)