Please make sure to refer Day3: C++ language | Variables | Datatypes | Part-1
Topics to be covered
- Variables
- Declare multiple Variables
- User Input
Variables
#include <iostream>
using namespace std;
int main(){
int age = 15;
cout << "my age is:" << age << endl ;
}
Declare multiple variables
To declare more than one variable of the same type.
#include <iostream>
using namespace std;
int main(){
int x = 15, y = 6, z = 50;
cout << x + y + z;
}
You can also assign the same value to multiple variables in one line.
#include <iostream>
using namespace std;
int main(){
int x, y, z;
x = y = z = 60;
cout << x + y + z;
}
User input
In C++, the term "input" generally refers to the process of taking data from an external source (like the user or a file) and storing it in a variable for use in your program.
cin
is a predefined variable that reads data from the keyboard with the extraction operator (>>
) .
- cout is pronounced "see-out". Used for output,and uses the insertion operator (
<<
) - cin is pronounced "see-in". Used for input, and uses the extraction operator (
>>
)
int z, y;
int sum;
cout <<
"Type a number: ";
cin >>
z;
cout <<
"Type another number: ";
cin >>
y;
sum = x + y;
cout <<
"Sum is: " <<
sum;
Here I gave two numbers 50 and 45 , it can be any according to that result varies.
Top comments (0)