0
0
C++programming~10 mins

Multiple input and output in C++ - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Multiple input and output
Start
Read input1, input2
Process inputs
Output result1, result2
End
The program reads multiple inputs, processes them, and then outputs multiple results.
Execution Sample
C++
#include <iostream>
using namespace std;
int main() {
  int a, b;
  cin >> a >> b;
  cout << a + b << " " << a * b << endl;
  return 0;
}
This code reads two integers, then outputs their sum and product.
Execution Table
StepActionInput ValuesVariables AfterOutput
1Start program-a: uninitialized, b: uninitialized-
2Read inputsa=3, b=4a:3, b:4-
3Calculate sum and product-a:3, b:4-
4Output results-a:3, b:47 12
5End program-a:3, b:4-
💡 Program ends after outputting sum and product of inputs.
Variable Tracker
VariableStartAfter InputAfter CalculationFinal
auninitialized333
buninitialized444
Key Moments - 2 Insights
Why do we use 'cin >> a >> b;' instead of two separate input lines?
Using 'cin >> a >> b;' reads both inputs in one line, making the code shorter and easier to read, as shown in execution_table step 2.
How does the program output two values in one line?
The program uses 'cout << a + b << " " << a * b << endl;' to print both results separated by a space, as seen in execution_table step 4.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what are the values of 'a' and 'b' after step 2?
Aa=0, b=0
Ba=7, b=12
Ca=3, b=4
Da and b are uninitialized
💡 Hint
Check the 'Variables After' column in step 2 of the execution_table.
At which step does the program output the sum and product?
AStep 4
BStep 3
CStep 2
DStep 5
💡 Hint
Look at the 'Output' column in the execution_table.
If the input values were changed to 5 and 6, what would be the output?
A5 6
B11 30
C30 11
DError
💡 Hint
Sum is a+b and product is a*b as shown in the code and execution_table.
Concept Snapshot
Multiple input and output in C++:
- Use 'cin >> var1 >> var2;' to read multiple inputs.
- Use 'cout << val1 << " " << val2 << endl;' to output multiple values.
- Inputs and outputs can be on the same line.
- Variables hold input values for processing.
- Program ends after outputting results.
Full Transcript
This example shows how a C++ program reads two integers from the user using 'cin >> a >> b;'. Then it calculates the sum and product of these two numbers. Finally, it outputs both results on the same line separated by a space using 'cout'. The execution table traces each step: starting the program, reading inputs, calculating results, outputting them, and ending the program. The variable tracker shows how variables change after input and calculation. Key moments clarify why inputs and outputs are done in one line. The quiz tests understanding of variable values and output timing. This helps beginners see how multiple inputs and outputs work together in a simple program.