C++ cin and cout

Introduction

cin and cout are used to read and write data from and to the console.

cin is used to read data from the console. (read from keyboard)
cout is used to write data to the console. (output to screen)

Example:
cin >> x;

Reads a value from the console and stores it in the variable x.

cout << "Hello World";

Writes "Hello World" to the console.

Code - Display "Hello World!"

#include <iostream>
using namespace std;

int main() {
	cout << "Hello World!";
	return 0;
}
	

Code - Read a number from user. Display square of the number.

#include <iostream>
using namespace std;

int main() {
	int x;
	cout << "Enter a number: ";
	cin >> x;
	cout << "Square of " << x << " is " << x*x;
	return 0;
}
	

Code - Read the name from user. Display welcome message.

#include <iostream>
#include <string>
using namespace std;

int main() {
	string name;
	cout << "Enter your name: ";
	cin >> name;
	cout << "Welcome " << name;
	return 0;
}

Task

Read name and age from user, and display a welcome message with age.

Example:

Enter your name: John Doe
Enter your age: 25

Welcome John Doe, you are 25 years old.

Reference

geeksforgeeks.org