Input and Output

Input and output are basic concepts in C++ programming. They allow a program to take data from the user and display results on the screen.

What is Input?

Input means taking data from the user or another source into the program.

Example:

  • Entering name
  • Entering age
  • Entering numbers

What is Output?

Output means displaying information from the program to the user.

Example:

  • Showing result
  • Displaying messages
  • Printing calculations

Input and Output Library

C++ uses the <iostream> library for input and output operations.

#include <iostream>

Output Using cout

cout is used to display output on the screen.

Syntax

cout << value;

Example

#include <iostream>
using namespace std;

int main() {
cout << "Welcome to C++";

return 0;
}

Output

Welcome to C++

Input Using cin

cin is used to take input from the user.

Syntax

cin >> variable;

Example

#include <iostream>
using namespace std;

int main() {
int age;

cin >> age;

cout << age;

return 0;
}

Example with Message

#include <iostream>
using namespace std;

int main() {
string name;

cout << "Enter your name: ";
cin >> name;

cout << "Hello " << name;

return 0;
}

Output Example

Enter your name: Ali
Hello Ali

Using endl

endl is used to move output to the next line.

cout << "Line 1" << endl;
cout << "Line 2";

Multiple Inputs

int a, b;

cin >> a >> b;

Example Program

#include <iostream>
using namespace std;

int main() {
int num1, num2;

cout << "Enter two numbers: ";
cin >> num1 >> num2;

cout << "Sum: " << num1 + num2;

return 0;
}

Output Example

Enter two numbers: 5 3
Sum: 8

Important Points

  • cin is used for input
  • cout is used for output
  • << is insertion operator
  • >> is extraction operator
  • endl creates a new line

Why Input and Output are Important

They are important because they:

  • Allow user interaction
  • Make programs dynamic
  • Display results clearly
  • Help process user data

Conclusion

Input and output operations are essential in C++ programming. Using cin and cout, programs can interact with users by taking input and displaying meaningful results. Understanding these concepts is important for building interactive applications.

Home » C++ Fundamentals (Beginner Level) > Basics of Programming > Input and Output