C++ Lessons
C++ Lessons
C++ Lessons
int main()
{
std::cout << "Shady Mohamed" <<std::endl;
std::cout << "this is my frist c++ program\n";
std::cout << "i love motocycle\n";
int myage ;
myage = 19;
cout << "my age is " << myage << " years old." << endl;
return 0;
}
-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
------------------------
#include <iostream>
using namespace std;
int main()
{
string Name = "Shady Mohamed.";
int Age = 19;
string City = "Ismailia.";
string Country = "Egypt.";
float M_Salary = 5000;
char Gender = 'S';
bool Married = false ;
cout << "************************** \n";
cout << "Name: " << Name << endl;
cout << "Age: " << Age << "Years." << endl;
cout << "City: " << City << endl;
cout << "Country: " << Country << endl;
cout << "Monthly Salary: " << M_Salary << endl;
cout << "Yearly Salary: " << M_Salary *12 << endl;
cout << "Gender: " << Gender << endl;
cout << "Married: " << Married << endl;
cout << "************************** \n\n";
cout << "After 5 years you will be " << Age1 +5 << " years old.";
return 0;
}
-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
-------------------------
int main()
{
char Mychar;
int Mynumber;
return 0;
}
-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
--------------------------
Lesson #18 Homework Solution
#include <iostream>
using namespace std;
int main()
{
string Name;
int Age;
string City;
string Country;
float M_Salary;
char Gender;
bool Married;
int Age;
cin >> Age;
cout << "afrer 5 years you will be " << Age + 5 << "years old";
--------------------\
homework 3
int a, b ,c;
cout << "please enter the first number ?" << endl;
cin >> a;
cout << "please enter the second number ?" << endl;
cin >> b;
cout << "please enter the third number ?" << endl;
cin >> c;
cout << endl;
int sum = a + b + c;
cout << a << "+" << endl;
cout << b << "+" << endl;
cout << c << endl << endl;
cout << "-------------------------------" << endl;
cout << sum;
return 0;
}
-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
----------------------------
#include <iostream>
using namespace std;
int main()
{
int v1;
signed int v2;
return 0;
--------------------\
#include <iostream>
using namespace std;
int main()
{
double distance = 56E12; //56E12 is equal to 56*10^12
cout << distance << endl;
return 0;
}
--------------------\
#include <iostream>
using namespace std;
int main()
{
cout << "The size of bool data type is " << sizeof(bool) << "\n";
cout << "The size of char data type is " << sizeof(char) << "\n";
cout << "The size of short data type is " << sizeof(short int) << "\n";
cout << "The size of int data type is " << sizeof(int) << "\n";
cout << "The size of int long data type is " << sizeof(long) << "\n";
cout << "The size of int long long data type is " << sizeof(long long) << "\n";
cout << "The size of float data type is " << sizeof(float) << "\n";
cout << "The size of double data type is " << sizeof(double) << "\n";
return 0;
}
-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
---------------------------------
#include <iostream>
using namespace std;
int main()
{
int A = 10, B = 20;
A++;
B--;
return 0;
}
--------------------\
#include <iostream>
using namespace std;
int main()
{
int A = 10;
int B = A++; // B will take the old value of A , then A will increase by 1
return 0;
}
-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
---------------------------------------------
#include <iostream>
using namespace std;
int main()
{
int A = 10, B = 20;
A += B; // A = A + B
cout << "A = " << A << endl;
A -= B; // A = A - B
cout << "A = " << A << endl;
A *= B; // A = A * B
cout << "A = " << A << endl;
A /= B; // A = A / B
cout << "A = " << A << endl;
A %= B; // A = A % B
cout << "A = " << A << endl;
return 0;
}
-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
---------------
int main()
{
int A = 10, B = 20;
#include <iostream>
using namespace std;
int main()
{
int A, B;
cout << "Please enter the first number A?" << endl;
cin >> A;
cout << "Please enter the second number B?" << endl;
cin >> B;
cout << A << " = " << B << " is " << (A == B) << endl;
cout << A << " != " << B << " is " << (A != B) << endl;
cout << A << " > " << B << " is " << (A > B) << endl;
cout << A << " < " << B << " is " << (A < B) << endl;
cout << A << " >= " << B << " is " << (A >= B) << endl;
cout << A << " <= " << B << " is " << (A <= B) << endl;
return 0;
}
-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
#include <iostream>
#include <cmath> // مهم
int main()
{
double x = 64;
cout << "square root value of 64 : " << sqrt(x) << endl;//sqrt يعني الجذر التربيعي
cout << "square root value of 50 : " << sqrt(50) << endl; // gives 7.07107
//----------------------------------------------
cout << round(2.4) << endl; // gives 2
cout << round(2.5) << endl; // gives 3
cout << round(2.7) << endl; // gives 3
cout << "Power value: X^y = (2^4) : " << pow(X, y) << endl; //gives 16
cout << "Power value: X^y = (4^3) : " << pow(4, 3) << endl; //gives 64
//-----------------------------------------------------------------------
cout << "Ceiling value of 2.9 : " << ceil(2.9) << endl; // gives 3
cout << "Floor value of 2.9 : " << floor(2.9) << endl; // gives 2
cout << "Ceiling value of -2.9 : " << ceil(-2.9) << endl; // gives -2
cout << "Floor value of -2.9 : " << floor(-2.9) << endl; // gives -3
//----------------------------------------------------------------------
cout << abs(-10) << endl; // gives 10 يعني القيمة المطلقة
cout << abs(10) << endl; // gives 10
return 0;
}
-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
---------------------------------------------
homework lesson 26
#include <iostream>
#include <cmath> // مهم
int main()
{
//Problem #16 - Rectangle area Through Diagonal and Side Area
int a, d;
cout << "enter a : " << endl;
cin >> a;
cout << "enter d : " << endl;
cin >> d;
cout << "Area = " << a * sqrt(pow(d, 2) - pow(a, 2));
return 0;
---------------------------------------------------------------------
#include <iostream>
#include <cmath> // مهم
int main()
{
//Problem #18 Circle Area
int r ;
cout << "enter r : " << endl;
cin >> r;
#include <iostream>
#include <cmath> // مهم
int main()
{
//Problem #19 - Circle Area Through Diameter
int D ;
cout << "enter D : " << endl;
cin >> D;
#include <iostream>
#include <cmath> // مهم
int main()
{
//Problem #20 Circle Area Inscribed in a Square
int A ;
cout << "enter A : " << endl;
cin >> A;
#include <iostream>
#include <cmath> // مهم
int main()
{
//Problem #21
int L;
cout << "enter L : " << endl;
cin >> L;
#include <iostream>
struct stAddress
{
string Street1;
string POBOX;
};
struct stOwner
{
string FullName;
string Phone;
stAddress Address;
};
struct car
{
string Brand;
string Model;
int Year;
stOwner Owner;
};
int main()
{
car Mycar1, Mycar2;
Mycar1.Brand = "BMW";
Mycar1.Model = "X5";
Mycar1.Year = 2000;
Mycar1.Owner.FullName = "shady";
Mycar1.Owner.Phone = "01020946289";
Mycar1.Owner.Address.POBOX = "ismailia";
Mycar2.Brand = "Ford";
Mycar2.Model = "Mustang";
Mycar2.Year = 2022;
cout << Mycar1.Brand << " " << Mycar1.Model << " " << Mycar1.Year << endl;
cout << Mycar2.Brand << " " << Mycar2.Model << " " << Mycar2.Year << endl;
cout << Mycar1.Owner.FullName << endl;
cout << Mycar1.Owner.Phone << endl;
cout << Mycar1.Owner.Address.POBOX;
return 0;
}
-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
---------------------------------
Lesson 28 - Enums
#include <iostream>
int main()
{
Color MyColor;
Direction MyDirection;
Week Today;
Status MyStatus;
MyColor = Color::Blue;
MyDirection = Direction::East;
Today = Week::Sat;
MyStatus = Status::Married;
#include <iostream>
using namespace std;
struct stAddress
{
string StreetName;
string BuildingNo;
string POBox;
string ZipCode;
};
struct stContactInfo
{
string Phone;
string Email;
stAddress Address;
};
struct stPerson
{
string FirstName;
string LastName;
stContactInfo ContactInfo;
enMaritalStatus MaritalStatues;
enGendor Gendor;
enColor FavourateColor;
};
int main()
{
stPerson Person1;
Person1.FirstName = "Mohammed";
Person1.LastName = "Abu-Hadhoud";
Person1.ContactInfo.Email = "[email protected]";
Person1.ContactInfo.Phone = "+961000000999";
Person1.ContactInfo.Address.POBox = "7777";
Person1.ContactInfo.Address.ZipCode = "11194";
Person1.ContactInfo.Address.StreetName = "Queen1 Street";
Person1.ContactInfo.Address.BuildingNo = "313";
Person1.Gendor = enGendor::Male;
Person1.MaritalStatues = enMaritalStatus::Married;
Person1.FavourateColor = enColor::Green;
return 0;
}
-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
----------------------------
#include <iostream>
#include <string> // مهمة في درس النهارده
int main()
{
//دا شرح بس
//Convert double to integer
int Num1;
double Num2 = 18.99;
Num1 = Num2; // Implicit conversion from double to int تحويل ضمني
void stars()
{
cout << "***********\n";
cout << "***********\n";
cout << "***********\n";
cout << "***********\n";
void mylove()
{
cout << "I Love Programming!\n\n";
cout << "I promise to be best developer ever!\n\n";
cout << "I know it will take some time to practice, but I \n";
cout << "will achieve my goal.\n\n";
cout << "Best Regards,\n";
cout << "Shady Mohamed,\n\n";
}
void H()
{
cout << " * *\n";
cout << " * *\n";
cout << " * * * * *\n";
cout << " * *\n";
cout << " * *\n";
}
int main()
{
Detials();
stars();
mylove();
H();
return 0;
}
-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
---------------------------------
int main()
{
int Number;
string Name;
string Country;
cout << "Number: " << Number << ", Name: " << Name << " Country: " << Country;
return 0;
}
-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
---------------------------------
void myFunction()
{
cout << "This is my first pricedure, it got executed" << endl;
}
string myFunction2()
{
return "This is my first returning value frnction, this is the value.";
}
float myFunction3()
{
float x = 10.5;
float y = 20.3;
return x * y;
}
int main()
{
float Result;
Result = myFunction3() - 10;
myFunction();
cout << Result;
return 0;
}
homework lesson34
#include <iostream>
#include <string> // مهمة في درس النهارده
void MySumProcedure()
{
int N1, N2;
int sum;
cout << "Please enter Number1?" << endl;
cin >> N1;
cout << "Please enter Number2?" << endl;
cin >> N2;
cout << "*****************************" << endl;
sum = N1 + N2;
cout << sum;
int MySumFunction()
{
int Num1, Num2,sum;
cout << "Please enter Number1?" << endl;
cin >> Num1;
cout << "Please enter Number2?" << endl;
cin >> Num2;
cout << "*****************************" << endl;
return Num1 + Num2;
int main()
{
int result;
//MySumProcedure();
int main()
{
cout << MySumFunction(10, 20) << endl;
cout << MySumFunction(90, 22) << endl;
cout << MySumFunction(16, 40) << endl;
--------------------------------------------------------
// int Num1 , Num2;
// cin >> Num1;
//cin >> Num2;
return 0;
}
-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
---------------------------------
int x = 300;
void MyFunction()
{
int x = 500;
cout << "The value of x inside function is: " << x << endl;
}
int main()
{
int x = 10;
cout << "The local value of x inside Main is: " << x << endl;
MyFunction();
cout << "The golobal value of x inside Main is: " << ::x << endl;
return 0;
}
-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
---------------------------------
int x = 300;
Temp = A;
A = B;
B = Temp;
cout << "After Swap inside function A= " << A << ", B=" << B << endl;
}
int main()
{
int A, B;
cout << "Before Swap A=" << A << ", B=" << B << endl;
Swap(A, B);
cout << "After Swap inside Main the function A=" << A << ", B=" << B << endl;
return 0;
}
-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
---------------------------------
int main()
{
strInfo Person1Info;
ReadInfo(Person1Info);
PrintInfo(Person1Info);
strInfo Person2Info;
ReadInfo(Person2Info);
PrintInfo(Person2Info);
//بنعمل كدا عشان نقلل االكواد في المين عشان الكود يبقا اسرع
return 0;
}
-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
---------------------------------
Homework lesson 38
#include <iostream>
#include <string>
struct strInfo {
string Name;
int Age;
string City;
string Country;
float M_Salary;
char Gender;
bool Married;
};
int main()
{
strInfo Person1Info;
ReadInfo(Person1Info);
PrintInfo(Person1Info);
return 0;
}
-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
---------------------------------
int main()
{
int x[5] = { 22, 18, 2, 55, 520 };
return 0;
}
-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
---------------------------------
Homework lesson 39
#include <iostream>
#include <string>
return 0;
}
-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
-----------------------------------------------------
#include <iostream>
#include <string>
struct strInfo {
string FirstName;
string LastName;
int Age;
string Phone;
};
int main()
{
strInfo Persons[2];
ReadPersonsInfo(Persons);
PrintPersonsInfo(Persons);
return 0;
}
-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
---------------------------------
int main()
{
int x;
if (x > 5)
{
cout << "Yes, X is grator than 5 " << endl;
}
else
{
cout << "No, X is less than 5 " << endl;
cout << "The code after if body always executed. << "اكتب عادي بعديهم الكود هيشتغل عادي
endl;
return 0;
}
-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
---------------------------------
Homework lesson 42
#Problem 4
#include <iostream>
#include <string>
struct strInfo {
int Age;
bool Driver;
};
ReadInfoPerson(Person);
return 0;
}
-----------------------------------------
#Problem 11
#include <iostream>
#include <string>
int main()
{
float Mark1, Mark2, Mark3, Avg;
int main()
{
int Age;
ReadAge(Age);
Result(Age);
return 0;
}
-----------------------------------------
#Problem 49
#include <iostream>
#include <string>
if (PIN==1234)
{
cout << "Your Balance is " << Balance;
}
else
{
cout << "Wrong PIN";
}
}
int main()
{
int PIN, Balance;
ReadPIN(PIN);
Result(PIN, Balance);
return 0;
}
-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
---------------------------------
int main()
{
int time = 22;
else
{
cout << "Good evening." << endl;
}
return 0;
}
-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
---------------------------------
Homework lesson 43
#Problem 33
#include <iostream>
#include <string>
-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
---------------------------------
//عشان اقدر اقرأ االنيم من ال سي ان مش بقدر اقرأها مباشرة عشان كدا عملت كدا
int c;
enScreenColor Color;
cin >> c;
Color = (enScreenColor) c; //عشان احولها من انتجر ل االنيم
if (Color == enScreenColor::Red)
{
system("color 4F");
}
else if (Color == enScreenColor::Blue)
{
system("color 1F");
}
else if (Color == enScreenColor::Green)
{
system("color 2F");
}
else if (Color == enScreenColor::Yellow)
{
system("color 6F");
}
else
{
system("color 4F");
}
return 0;
}
مثال اخر
#include <iostream>
#include <string>
//عشان اقدر اقرأ االنيم من ال سي ان مش بقدر اقرأها مباشرة عشان كدا عملت كدا
int c;
enScreenChoice Country;
cin >> c;
Country = (enScreenChoice) c; //عشان احولها من انتجر ل االنيم
if (Country == enScreenChoice::Jordan)
{
system("Your country is Jordan\n");
}
else if (Country == enScreenChoice::Tunisa)
{
system("Your country is Tunisa\n");
}
else if (Country == enScreenChoice::Algeria)
{
system("Your country is Algeria\n");
}
else if (Country == enScreenChoice::Oman)
{
system("Your country is Oman\n");
}
else if (Country == enScreenChoice::Egypt)
{
system("Your country is Egypt\n");
}
else if (Country == enScreenChoice::Iraq)
{
system("Your country is Iraq\n");
}
else
{
cout << "Your country is other";
}
return 0;
}
-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
--------------------------------
int main()
{
int day = 1;
switch (day) {
case 1:
cout << "Sun";
break; //دا بيفصل عشان الكود لو اتحقق مش يكمل بقيت الشروط
case 2:
cout << "Monday";
break; //لو شيلت البريك هيطبعلي كل ايام االسبوع
case 3:
cout << "Tuesday";
break;
case 4:
cout << "Wednesday";
break;
case 5:
cout << "Thursday";
break;
case 6:
cout << "Friday";
break;
case 7:
cout << "Saturday";
break;
default:
cout << "Not a week day\n";
}
return 0;
}
-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
--------------------------------
Homework lesson 45
#include <iostream>
#include <string>
int c;
cin >> c;
switch (c){
case 1:
cout << Num1 + Num2;
break;
case 2:
cout << Num1 - Num2;
break;
case 3:
cout << Num1 * Num2;
break;
case 4:
cout << Num1 / Num2;
break;
}
int main()
{
int Num1, Num2;
ReadNum(Num1, Num2);
return 0;
}
-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
---------------------------------
void ShowWeekDayMenue()
{
cout << "*****************************" << endl;
cout << " Week Days " << endl;
cout << "*****************************" << endl;
cout << "1: Sunday" << endl;
cout << "2: Monday" << endl;
cout << "3: Tuesday" << endl;
cout << "4: Wednesday" << endl;
cout << "5: Thursday" << endl;
cout << "6: Friday" << endl;
cout << "7: Saturday" << endl;
cout << "*****************************" << endl;
cout << "Please enter the number of day?" << endl;
}
enWeekDay ReadWeekDay()
{
enWeekDay WeekDay;
int wd;
cin >> wd;
return (enWeekDay)wd; // بحول الwd من انتجر ل انيم
}
string GetWeekDayName(enWeekDay WeekDay)
{
switch(WeekDay) {
case enWeekDay::Sun:
return "Sunday";
break;
case enWeekDay::Mon:
return "Monday";
break;
case enWeekDay::Tue:
return "Tuesday";
break;
case enWeekDay::Wed:
return "Wednesday";
break;
case enWeekDay::Thu:
return "Thursday";
break;
case enWeekDay::Fri:
return "Friday";
break;
case enWeekDay::Sat:
return "Saturday";
break;
default:
return "Not a week day!\n";
}
}
int main()
{
ShowWeekDayMenue();
int main()
{
for (int i = 1; i <= 5; i++)
{
cout << i << endl;
}
Homework lesson 47
#Problem 26
#include <iostream>
int main()
{
int Num;
ReadNum(Num);
PrintNum(Num);
return 0;
}
----------------------
#Problem 27
#include <iostream>
int main()
{
int Num;
ReadNum(Num);
PrintNum(Num);
return 0;
}
-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
-----------------------
cout << "How many Numbers do you want to enter? 1to 100?\n";
cin >> Length;
for (int i = 0; i <= Length - 1; i++) // حطينا الi بتساوي صفر عشان االراي بتبدأ من صفر
{
cout << "Please enter Number " << i + 1 << endl; //1+0 معناها
cin >> Arr1[i];
}
}
cout << "Number [" << i + 1 << "] : " << Arr1[i] << endl;
}
}
return Sum;
}
return 0;
}
-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
-----------------------
Homework lesson 48 مهم جدا
#include <iostream>
#include <string>
struct strInfo {
string FirstName;
string LastName;
int Age;
string Phone;
};
ReadPersonsInfo(Persons, NumberOfPersons);
PrintPersonsInfo(Persons, NumberOfPersons);
return 0;
}
-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
-----------------------
int main()
{
//--------------------------------------------------------
for (int i = 1; i <= 10; i++)
{
Homework lesson 49
#include <iostream>
return 0;
}
-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
-----------------------
int main()
{
cout << "\n For Loop \n";
// for loop from 1 to 5
for (int x = 1; x <= 5; x++)
{
cout << x << endl;
}
int main()
{
int Number;
cout << "Please enter a positive number?\n";
cin >> Number;
cout << "\n The number you entered is " << Number << endl;
return 0;
}
--------------------------------------------------------------------
#include <iostream>
return Number;
}
int main()
{
cout << "\n The Number is " << ReadIntNumberInRange(18, 45) << endl;
return 0;
}
-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
-----------------------
Homework lesson 50
#Problem 26
#include <iostream>
int Counter = 1;
return 0;
}
--------------------------------------------
#Problem 27
#include <iostream>
int Counter = 1;
int main()
{
int Num;
ReadNum(Num);
return 0;
}
-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
-----------------------
return num;
}
do
{
cout << "Please enter a number between " << From << " and " << To << endl;
cin >> num;
} while (num < From || num > To);
return num;
}
int main()
{
//cout << "\n The number you entered is " << ReadIntNumberInRangeUsingWhile(1,
10);
cout << "\n The number you entered is " << ReadIntNumberInRangeUsingDoWhile(1,
10);
return 0;
}
-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
-----------------------
int main()
{
for (int i = 1 ; i <= 10; i++)
{
if (i == 3)
{
break;// بتطلعه برا اللوب مش بتخليه يكمل الكود
}
int main()
{
int arr[10] = { 10,20,44,55,33,22,99,88,99,100 };
int SearchFor = 20;
for (int i = 0; i <= 10; i++)
{
cout << "we are at iteration " << i + 1 << endl;
if (SearchFor == arr[i])
{
cout << endl << SearchFor << " found at position " << i << endl <<
endl;
break; //ابقا جرب الكود من غير بريك وانت هتعرف الفرق
}
}
return 0;
}
-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
-----------------------
int main()
{
if (i == 3)
{
continue;// و مش هيطبعها3هيجي عند ال
}
return 0;
}
---------------------------------------------
#include <iostream>
int main()
{
int sum = 0;
int num = 0;
continue;
}
sum += num;
}
return 0;
}