Numeric data types
#include <iostream>
using namespace std;
int main() {
int myNum = 1000;
float myFloat = 5.75;
double myDouble = 19.99;
cout << myNum << endl;
cout << myFloat << endl;
cout << myDouble << endl;
return 0;
}
Boolean data type
#include <iostream>
using namespace std;
int main() {
bool fun = true;
bool tasty = false;
cout << fun << endl;
cout << tasty;
return 0;
}
Character and ASCII values
#include <iostream>
using namespace std;
int main() {
char a = 65;
char b = 66;
char c = 67;
cout << a << b << c;
return 0;
}
String input and display
#include <iostream>
#include <string>
using namespace std;
int main() {
string name;
cout << "Enter your full name: ";
getline(cin, name);
cout << "Your name is: " << name;
return 0;
}
String concatenation
#include <iostream>
#include <string>
using namespace std;
int main() {
string firstName = "John ";
string lastName = "Doe";
string fullName = firstName + lastName;
cout << fullName;
return 0;
}
String length and accessing characters
#include <iostream>
#include <string>
using namespace std;
int main() {
string myString = "Hello";
cout << "Length: " << myString.length() << endl;
cout << "First index: " << myString[0] << endl;
cout << "Second index: " << myString[1] << endl;
cout << "Last index: " << myString[myString.length() - 1];
return 0;
}
auto keyword
#include <iostream>
#include <string>
using namespace std;
int main() {
auto myNum = 5;
auto myFloat = 5.99;
auto myDouble = 9.98;
auto myLetter = 'D';
auto myBoolean = true;
auto myString = string("Hello");
cout << myNum << endl;
cout << myFloat << endl;
cout << myDouble << endl;
cout << myLetter << endl;
cout << myBoolean << endl;
cout << myString;
return 0;
}
max() and min()
#include <iostream>
using namespace std;
int main() {
cout << max(33, 10) << endl;
cout << max(3, -10) << endl;
cout << min(5, 10) << endl;
cout << min(-5, -10) << endl;
return 0;
}
cmath functions
#include <iostream>
#include <cmath>
using namespace std;
int main() {
cout << "Square root of 64: " << sqrt(64) << endl;
cout << "Round 2.6: " << round(2.6) << endl;
cout << "Natural logarithm of 2: " << log(2) << endl;
return 0;
}
Real-life example – total product price
#include <iostream>
using namespace std;
int main() {
int quantity = 5;
double price = 19.99;
double total = quantity * price;
cout << "Total Price: $" << total;
return 0;
}
Real-life example – user information
#include <iostream>
#include <string>
using namespace std;
int main() {
string name = "Gulan";
string gender = "Female";
int age = 28;
cout << "Name:\t" << name << endl;
cout << "Gender:\t" << gender << endl;
cout << "Age:\t" << age << endl;
return 0;
}Ready to practice this lecture?