02-Introduction To C++, Variables
02-Introduction To C++, Variables
What is C++ ?
#include <iostream> 1) Open note pad, or any text editor and write this
using namespace std; program.
2) Save the program with any name like
int main() { program_name.cpp
cout << "Hello World!"; 3) Open command prompt, compile the program using g++
return 0; command.
} 4) Execute .exe file.
C++ Syntax
#include <iostream> is a header file library that lets
#include <iostream> us work with input and output objects, such
using namespace std; as cout (used in line 5). Header files add functionality to
C++ programs.
Output:
#include <iostream>
using namespace std;
Hello World!
I am learning C++
int main() {
cout << "Hello World!";
cout << "I am learning C++";
return 0;
}
C++ Comments
Where type is one of C++ types (such as int), and variable is the name of the variable (such
as x or myName). The equal sign is used to assign values to the variable.
#include<iostream> Output:
using namespace std;
int main(){ SUM = 15
int x=5;
int y=10;
int sum=x+y;
cout<<"SUM = "<<sum;
return 0;
}
C++ User Input
✔ You have already learned that cout is used to output (print) values. Now we will
use cin to get user input.
✔ cin is a predefined variable that reads data from the keyboard with the extraction
operator (>>).
#include<iostream>
using namespace std;
int main(){
int x;
cout<<"Enter value of x :";
cin>>x;
cout<<"You have entered the value of x = "<<x;
return 0;
}
Output:
WAP in C++ to add two integer number. Take integer input from console.
#include<iostream> Output:
using namespace std;
int main(){ Enter value of x: 12 (Enter)
int x,y; Enter value of y: 6 (Enter)
cout<<"Enter value of x: “; SUM = 18
cin>>x;
cout<<"Enter value of y: “;
cin>>y;
int sum=x+y;
cout<<"SUM = "<<sum;
return 0;
};