0% found this document useful (0 votes)
10 views

C++ Lessons

Uploaded by

sm165901
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

C++ Lessons

Uploaded by

sm165901
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 43

#include <iostream>

using namespace std;

int main()
{
std::cout << "Shady Mohamed" <<std::endl;
std::cout << "this is my frist c++ program\n";
std::cout << "i love motocycle\n";

std::cout << "M1" << "M2" << "M3\n";


std::cout << "M1 " << "M2 " << "M3\n\n";

std::cout << "the homework1\n\n";

std::cout << "*******************\n";


std::cout << "Name:Shady Mohamed.\n";
std::cout << "Age :19 Years.\n";
std::cout << "City :Ismailia.\n";
std::cout << "Country: Egypt.\n";
std::cout << "*******************\n\n";

std::cout << "***********\n";


std::cout << "***********\n";
std::cout << "***********\n";
std::cout << "***********\n";

std::cout << "I Love Programming!\n\n";


std::cout << "I promise to be best developer ever!\n\n";
std::cout << "I know it will take some time to practice, but I \n";
std::cout << "will achieve my goal.\n\n";
std::cout << "Best Regards,\n";
std::cout << "Shady Mohamed,\n\n";

std::cout << " * *\n";


std::cout << " * *\n";
std::cout << " * * * * *\n";
std::cout << " * *\n";
std::cout << " * *\n";

cout << "shady mohamed \n\n";

// single line ‫معاك الخر السطر‬


/* multi line for long comments */
cout << "M1\M2 \n";
cout << "M1\\M2 \n\n";

cout << "M1\tM2 \n";


cout << "M3\tM4 \n\n";

cout << "My name is \"shady\" \n\n";

cout << "\a"; //‫بيعمل صوت جرس‬

std::cout << "homework lesson 16\n\n";

cout << "\a";

cout << "Dear Sir\\Madam, \n\n";


cout << "How are you? \n\n";
cout << "My name is \"Shady\" , nice to meet you. \n\n";

cout << "Ali\tAhmed\tLina \n";


cout << "Fadi\tZain\tMon\n\n";

int myage ;
myage = 19;
cout << "my age is " << myage << " years old." << endl;

int mynumber = 19;


float myfloat = 7.84;
double mydoublenumber = 21.89822;
char myletter = 's';
string mytext = "shady";
bool myboolean = true;

cout << mynumber << endl;


cout << myfloat << endl;
cout << mydoublenumber << endl;
cout << myletter << endl;
cout << mytext << endl;
cout << myboolean << endl;

const int MinutesPerHour = 60;

cout << MinutesPerHour << 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";

int Num1 = 20, Num2 = 30, Num3 = 10;

cout << Num1 << "+" << endl;


cout << Num2 << "+" << endl;
cout << Num3 << endl << endl;
cout << "-------------------------------" << endl;
cout << "Total = " << Num1 + Num2 + Num3 << endl << endl;

int Age1 = 25;


-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
---------------------------------

Lesson _30 - Data Type Conversion

cout << "After 5 years you will be " << Age1 +5 << " years old.";

return 0;
}

-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
-------------------------

lesson18 user inputs


#include <iostream>
using namespace std;

int main()
{
char Mychar;
int Mynumber;

cout << "please enter a char ?" << endl;


cin >> Mychar;

cout << "please enter a number ?" << endl;


cin >> Mynumber;

cout << "you enter char:" << Mychar << endl;


cout << "you enter number:" << Mynumber << endl;

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;

cout << "Name:";


cin >> Name;

cout << "Age:";


cin >> Age;

cout << "City:";


cin >> City;

cout << "Country:";


cin >> Country;

cout << "Monthly Salary:";


cin >> M_Salary;

cout << "Gender:";


cin >> Gender;

cout << "Married:";


cin >> Married;
-------------------\
homework 2

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;
}

-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
----------------------------

Lesson #19 - Datatypes Sizes & Ranges

#include <iostream>
using namespace std;

int main()
{
int v1;
signed int v2;

unsigned int v3;

short int v4;


short v5;

unsigned short int v6;


unsigned short v7;

signed long int v8;


long int v9;
long v10;

unsigned long v11;

long long int v12;

unsigned long long v13;

long double v14;

signed char v15;


char v16;

unsigned char v17;

//signed ‫يعني موجب و سالب‬

return 0;
--------------------\
#include <iostream>
using namespace std;

int main()
{
double distance = 56E12; //56E12 is equal to 56*10^12
cout << distance << endl;

short d = 3434233; // Error! out of range


cout << d << endl;

unsigned int a = -10; // Error! can only store positive number


cout << a << 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;
}
-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
---------------------------------

Lesson #22 - Postfix vs Prefix : ++A vs A++ , --A vs A--

#include <iostream>
using namespace std;

int main()
{
int A = 10, B = 20;

A++;
B--;

cout << "A = " << A << endl;


cout << "B = " << B << endl;

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

cout << "B = " << B << endl;


cout << "A = " << A << endl;

B = ++A; // A will increase by 1, then B will take the new value


cout << "B = " << B << endl;
cout << "A = " << A << endl;

return 0;
}
-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
---------------------------------------------

Lesson #24 - Relational Operators

#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;
}
-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
---------------

Lesson #24 - Relational Operators


#include <iostream>
using namespace std;

int main()
{
int A = 10, B = 20;

cout << (A == B) << endl;


cout << (A != B) << endl; //!= ‫يعني ال تساوي‬
cout << (A > B) << endl;
cout << (A < B) << endl;
cout << (A >= B) << endl;
cout << (A <= B) << endl;
return 0;
}
-----------/

Homework Lesson #24 - Relational Operators

#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;
}
-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------

Lesson #26 - Math Functions in C++

#include <iostream>
#include <cmath> // ‫مهم‬

using namespace std;

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 << round(sqrt(50)) << endl; // gives 7


//----------------------------------------------------------
int X = 2;
int y = 4;

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> // ‫مهم‬

using namespace std;

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> // ‫مهم‬

using namespace std;

int main()
{
//Problem #18 Circle Area
int r ;
cout << "enter r : " << endl;
cin >> r;

cout << "Area = " << ceil( 3.14 * pow(r,2));


return 0;
}
----------------------------------------------------------------------

#include <iostream>
#include <cmath> // ‫مهم‬

using namespace std;

int main()
{
//Problem #19 - Circle Area Through Diameter
int D ;
cout << "enter D : " << endl;
cin >> D;

cout << "Area = " << ceil( (3.14*pow(D,2))/4);


return 0;
}
----------------------------------------------------------------------

#include <iostream>
#include <cmath> // ‫مهم‬

using namespace std;

int main()
{
//Problem #20 Circle Area Inscribed in a Square
int A ;
cout << "enter A : " << endl;
cin >> A;

cout << "Area = " << ceil(3.14*pow(A/2,2));


return 0;
}
-----------------------------------------------------------------

#include <iostream>
#include <cmath> // ‫مهم‬

using namespace std;

int main()
{
//Problem #21
int L;
cout << "enter L : " << endl;
cin >> L;

cout << "Area = " << floor(pow(L, 2) / (4 * 3.14));


return 0;
}
---------------------------------------------------------------------
}
-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
---------------------------------

Lesson #27 - Structures

#include <iostream>

using namespace std;

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>

using namespace std;

enum Color { Red=100, Green=200, Yellow=300, Blue=400 };


enum Direction { North, South, East, West };
enum Week { Sat, Sun, Mon, Tue, Wed, Thu, Fri };
enum Gender { Male, Female };
enum Status { Single, Married };

int main()
{
Color MyColor;
Direction MyDirection;
Week Today;
Status MyStatus;

MyColor = Color::Blue;
MyDirection = Direction::East;
Today = Week::Sat;
MyStatus = Status::Married;

cout << MyColor;


return 0;
}
-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
---------------------------------

Lesson #29 - Nested Structures and Enums - Full Practical Example

#include <iostream>
using namespace std;

enum enColor { Red, Green, Yellow, Blue};


enum enGendor { Male, Female };
enum enMaritalStatus { Single, Married};

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 = "xyz@xyz.com";
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;

cout << Person1.ContactInfo.Address.StreetName << endl;

return 0;
}
-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
----------------------------

Lesson _30 - Data Type Conversion

#include <iostream>
#include <string> // ‫مهمة في درس النهارده‬

using namespace std;

int main()
{
//‫دا شرح بس‬
//Convert double to integer

int Num1;
double Num2 = 18.99;
Num1 = Num2; // Implicit conversion from double to int ‫تحويل ضمني‬

Num1 = (int)Num2; // Explicit Conversion

Num1 = int(Num2); // Explocot Conversion

cout << Num1 << Num2;


//------------------------------------------------------------------------
//Convert string to int, float, double.

string str = "123.456";

//Convert string to integer


int num_int = stoi(str); //stoi = string to integer

//Convert string to float


float num_float = stof(str); //stof = string to float

//Convert string to double


double num_double = stod(str);

cout << "num_int = " << num_int << endl;


cout << "num_float = " << num_float << endl;
cout << "num_double = " << num_double << endl;
//--------------------------------------------------------------------------
//Convert Numbers to string

int Num1 = 123;


double Num2 = 18.99;

string St1, St2;


St1 = to_string(Num1);
St2 = to_string(Num2);

cout << St1 << endl;


cout << St2 << endl;
return 0;
}
-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
---------------------------------
#include <iostream>
#include <string> // ‫مهمة في درس النهارده‬

using namespace std;

void veriable ‫ليس له قيمة‬


void Detials()
{
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";

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;
}
-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
---------------------------------

Lesson _33 - Solving the getline() Problem


#include <iostream>
#include <string> // ‫مهمة في درس النهارده‬

using namespace std;

int main()
{
int Number;
string Name;
string Country;

cout << "Please enter Employee Number?\n";


cin >> Number;

cout << "Please enter Name?\n";


cin.ignore(1, '\n'); //‫عشان يطنش كبسة االنتر‬
getline(cin, Name);

cout << "Please enter Country\n";


cin >> Country;

cout << "Number: " << Number << ", Name: " << Name << " Country: " << Country;

return 0;
}
-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
---------------------------------

Lesson _34 - Functions Part II - Functions vs Procedures


#include <iostream>
#include <string> // ‫مهمة في درس النهارده‬

using namespace std;

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> // ‫مهمة في درس النهارده‬

using namespace std;

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();

cout << MySumFunction;


return 0;
}
-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
---------------------------------

Lesson _35 - Function Part 3 - Parameters


#include <iostream>
#include <string> // ‫مهمة في درس النهارده‬

using namespace std;

int MySumFunction(int Num1, int Num2)


{

return Num1 + Num2;

int main()
{
cout << MySumFunction(10, 20) << endl;
cout << MySumFunction(90, 22) << endl;
cout << MySumFunction(16, 40) << endl;
--------------------------------------------------------
// int Num1 , Num2;
// cin >> Num1;
//cin >> Num2;

// cout << MySumFunction(Num1, Num2) << endl;

return 0;
}
-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
---------------------------------

Lesson _36 - Variable Scope_ Local vs Global Variables


#include <iostream>

using namespace std;

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;
}
-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
---------------------------------

‫مهمة البروبلم دي‬


#include <iostream>

using namespace std;

int x = 300;

void Swap(int &A, int &B)


{
int Temp;

Temp = A;
A = B;
B = Temp;
cout << "After Swap inside function A= " << A << ", B=" << B << endl;
}

int main()
{
int A, B;

cout << "Please enter the A?\n";


cin >> A;

cout << "Pleae enter the B?\n";


cin >> 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;
}
-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
---------------------------------

Lesson _38 - Structures and Functions - Re-usability


#include <iostream>
#include <string>

using namespace std;


struct strInfo{
string FirstName;
string LastName;
int Age;
string Phone;
};

void ReadInfo(strInfo &Info){

cout << "Please enter your FirstName?\n";


cin >> Info.FirstName;

cout << "Please enter your LastName?\n";


cin >> Info.LastName;

cout << "Please enter your Age?\n";


cin >> Info.Age;

cout << "Please enter your Phone?\n";


cin >> Info.Phone;
}

void PrintInfo(strInfo Info){


cout << "\n****************************\n";

cout << "FirstName: " << Info.FirstName << endl;


cout << "LastName: " << Info.LastName << endl;
cout << "Age: " << Info.Age << endl;
cout << "Phone: " << Info.Phone << endl;
cout << "\n****************************\n";
}

int main()
{
strInfo Person1Info;
ReadInfo(Person1Info);
PrintInfo(Person1Info);

strInfo Person2Info;
ReadInfo(Person2Info);
PrintInfo(Person2Info);

//‫بنعمل كدا عشان نقلل االكواد في المين عشان الكود يبقا اسرع‬
return 0;
}
-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
---------------------------------

Homework lesson 38
#include <iostream>
#include <string>

using namespace std;

struct strInfo {
string Name;
int Age;
string City;
string Country;
float M_Salary;
char Gender;
bool Married;
};

void ReadInfo(strInfo &Info) {


cout << "Please enter your Name?\n";
cin >> Info.Name;

cout << "Please enter your City?\n";


cin >> Info.City;

cout << "Please enter your Age?\n";


cin >> Info.Age;

cout << "Please enter your Country?\n";


cin >> Info.Country;

cout << "Please enter your MonthSalary?\n";


cin >> Info.M_Salary;

cout << "Please enter your Gender?\n";


cin >> Info.Gender;

cout << "Are you Married?\n";


cin >> Info.Married;
}
void PrintInfo(strInfo Info) {
cout << "\n****************************\n";
cout << "Name: " << Info.Name << endl;
cout << "Age: " << Info.Age << endl;
cout << "City: " << Info.City << endl;
cout << "Country: " << Info.Country << endl;
cout << "Monthly Salary: " << Info.M_Salary << endl;
cout << "Yearly Salary: " << Info.M_Salary * 12 << endl;
cout << "Gender: " << Info.Gender << endl;
cout << "Married: " << Info.Married << endl;
cout << "\n****************************\n";
}

int main()
{
strInfo Person1Info;
ReadInfo(Person1Info);
PrintInfo(Person1Info);

return 0;
}
-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
---------------------------------

Lesson _39 - Arrays


#include <iostream>
#include <string>

using namespace std;

int main()
{
int x[5] = { 22, 18, 2, 55, 520 };

cout << x[0] << endl; // Prints 22


cout << x[1] << endl; // Prints 18
cout << x[2] << endl; // Prints 2
cout << x[3] << endl; // Prints 55
cout << x[4] << endl; // Prints 520

cout << x[0] + x[2] << endl;

return 0;
}
-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
---------------------------------

Homework lesson 39
#include <iostream>
#include <string>

using namespace std;


int main()
{
float Grades[3];

cout << "Please Enter Grade1?" << endl;


cin >> Grades[0];

cout << "Please Enter Grade2?" << endl;


cin >> Grades[1];

cout << "Please Enter Grade3?" << endl;


cin >> Grades[2];

cout << "***************************************" << endl;

float Avg = (Grades[0] + Grades[1] + Grades[2]) / 3;


cout << "The average of grades is " << Avg;

return 0;
}
-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
-----------------------------------------------------

Homework lesson 41 ‫مهم جدا جدا‬

#include <iostream>
#include <string>

using namespace std;

struct strInfo {
string FirstName;
string LastName;
int Age;
string Phone;
};

void ReadInfo(strInfo& Info)


{
cout << "Please enter your First Name?" << endl;
cin >> Info.FirstName;

cout << "Please enter your Last Name?" << endl;


cin >> Info.LastName;

cout << "Please enter your Age?" << endl;


cin >> Info.Age;

cout << "Please enter your Phone?" << endl;


cin >> Info.Phone;

cout << "----------------------------------" << endl;


}

void PrintInfo(strInfo Info)


{
cout << "\n**************************************\n";
cout << "FirstName: " << Info.FirstName << endl;
cout << "LastName: " << Info.LastName << endl;
cout << "Age: " << Info.Age << endl;
cout << "Phone: " << Info.Phone << endl;
cout << "\n**************************************\n";
}

void ReadPersonsInfo(strInfo Persons[2])


{
ReadInfo(Persons[0]);
ReadInfo(Persons[1]);
}

void PrintPersonsInfo(strInfo Persons[2])


{
PrintInfo(Persons[0]);
PrintInfo(Persons[1]);
}

int main()
{
strInfo Persons[2];

ReadPersonsInfo(Persons);
PrintPersonsInfo(Persons);

return 0;
}
-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
---------------------------------

Lesson _42 - Conditional Statements_ IF_ IF ... Else Statemen


#include <iostream>
#include <string>

using namespace std;

int main()
{
int x;

cout << "Please enter your number? \n";


cin >> 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>

using namespace std;

struct strInfo {
int Age;
bool Driver;
};

void ReadInfoPerson(strInfo& Info)


{
cout << "Please enter your age?\n";
cin >> Info.Age;

cout << "Are you have a Driver License?\n";


cin >> Info.Driver;
}
int main()
{
strInfo Person;

ReadInfoPerson(Person);

if (Person.Age > 21 && Person.Driver == 1)


{
cout << "Hired" << endl;
}
else
{
cout << "Rejected" << endl;
}
return 0;
}
-----------------------------------------
#Problem 8
#include <iostream>
#include <string>

using namespace std;

void ReadMark(int &Mark)


{
cout << "Please your Mark?\n";
cin >> Mark;
}
void PrintResult(int Mark)
{
if (Mark >= 50)
{
cout << "PASS";
}
else
{
cout << "Fail";
}
}
int main()
{
int Mark;
ReadMark(Mark);
PrintResult(Mark);

return 0;
}
-----------------------------------------
#Problem 11
#include <iostream>
#include <string>

using namespace std;

void ReadMarks(float &Mark1,float &Mark2,float &Mark3 ,float &Avg)


{
cout << "Please enter Mark1?\n";
cin >> Mark1;

cout << "Please enter Mark2?\n";


cin >> Mark2;

cout << "Please enter Mark3?\n";


cin >> Mark3;

Avg = (Mark1 + Mark2 + Mark3) / 3;

void Result(float Avg)


{
if(Avg >=50)
{
cout << endl << "PASS";
}
else
{
cout << "Fail";
}
}

int main()
{
float Mark1, Mark2, Mark3, Avg;

ReadMarks(Mark1, Mark2, Mark3, Avg);


Result(Avg);
return 0;
}
-----------------------------------------
#Problem 24
#include <iostream>
#include <string>

using namespace std;

void ReadAge(int &Age)


{
cout << "Please enter your age?\n";
cin >> Age;
}

void Result(int Age)


{
if (Age >=18 && Age<=45)
{
cout << "Valid Age";
}
else
{
cout << "Invalid Age";
}
}

int main()
{
int Age;

ReadAge(Age);
Result(Age);
return 0;
}
-----------------------------------------
#Problem 49
#include <iostream>
#include <string>

using namespace std;

void ReadPIN(int &PIN)


{
cout << "Please enter PIN?\n";
cin >> PIN;
}

void Result(int PIN, int &Balance)


{
Balance = 7500;

if (PIN==1234)
{
cout << "Your Balance is " << Balance;
}
else
{
cout << "Wrong PIN";
}
}

int main()
{
int PIN, Balance;

ReadPIN(PIN);
Result(PIN, Balance);
return 0;
}
-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
---------------------------------

Lesson _43 - Conditional Else IF Statement


#include <iostream>
#include <string>

using namespace std;

int main()
{
int time = 22;

if (time < 10)


{
cout << "Good morning." << endl;
}
else if (time < 20)
{
cout << "Good day." << endl;
}

else
{
cout << "Good evening." << endl;
}
return 0;
}
-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
---------------------------------

Homework lesson 43
#Problem 33
#include <iostream>
#include <string>

using namespace std;

void ReadGrade(int &Grade)


{
cout << "Please enter your grade?\n";
cin >> Grade;
}
void ResultGrade(int Grade)
{
if (Grade >= 90 && Grade <= 100)
{
cout << "A" << endl;
}
else if (Grade >= 80 && Grade <= 89)
{
cout << "B" << endl;
}
else if (Grade >= 70 && Grade <= 79)
{
cout << "C" << endl;
}
else if (Grade >= 60 && Grade <= 69)
{
cout << "D" << endl;
}
else if (Grade >= 50 && Grade <= 59)
{
cout << "E" << endl;
}
else
{
cout << "F" << endl;
}
}
int main()
{
int Grade;
ReadGrade(Grade);
ResultGrade(Grade);
return 0;
}
-----------------------------------------

-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
---------------------------------

Lesson _44 - Enums with IF Statements ‫مهم الدرس دا‬


#include <iostream>
#include <string>

using namespace std;

enum enScreenColor { Red=1, Blue=2, Green=3, Yellow=4 };


int main()
{
cout << "**********************************\n";
cout << "please chose the number of your color?\n";
cout << "(1) Red\n";
cout << "(2) Blue\n";
cout << "(3) Green\n";
cout << "(4) Yellow\n";
cout << "**********************************\n";
cout << "Your Choise? ";

//‫عشان اقدر اقرأ االنيم من ال سي ان مش بقدر اقرأها مباشرة عشان كدا عملت كدا‬
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>

using namespace std;

enum enScreenChoice { Jordan = 1, Tunisa = 2, Algeria = 3, Oman = 4, Egypt = 5,


Iraq = 6 };
int main()
{
cout << "**********************************\n";
cout << "please enter the number of your country?\n";
cout << "(1) Jordan\n";
cout << "(2) Tunisa\n";
cout << "(3) Algeria\n";
cout << "(4) Oman\n";
cout << "(5) Egypt\n";
cout << "(6) Iraq\n";
cout << "(7) Other\n";
cout << "**********************************\n";
cout << "Your Choise? ";

//‫عشان اقدر اقرأ االنيم من ال سي ان مش بقدر اقرأها مباشرة عشان كدا عملت كدا‬
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;
}
-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
--------------------------------

Lesson _45 - Switch .. Case Statement


#include <iostream>
#include <string>

using namespace std;

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>

using namespace std;


void ReadNum(int &Num1,int &Num2)
{
cout << "Please enter number 1 \n";
cin >> Num1;
cout << "Please enter number 2 \n";
cin >> Num2;

cout << "Choise your operation type\n";


cout << "(1) Num1 + Num2" << endl;
cout << "(2) Num1 - Num2" << endl;
cout << "(3) Num1 * Num2" << endl;
cout << "(4) Num1 / Num2" << endl;
cout << "Your choise?";

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;
}
-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
---------------------------------

‫ جامد الدرس دا‬Lesson _46 - Nested Functions with Enums


#include <iostream>

using namespace std;

enum enWeekDay { Sun=1, Mon=2, Tue=3, Wed=4, Thu=5, Fri=6, Sat=7 };

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();

cout << "today is " << GetWeekDayName(ReadWeekDay()) << endl;


return 0;
}
-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
-----------------------

Lesson _47 - For Loops


#include <iostream>

using namespace std;

int main()
{
for (int i = 1; i <= 5; i++)
{
cout << i << endl;
}

for (int i = 1; i<=10; i = i+2)


{
cout << i << endl;
}

for (int i = 0; i <= 10; i = i + 2)


{
cout << i << endl;
}
return 0;
}
-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
-----------------------

Homework lesson 47
#Problem 26
#include <iostream>

using namespace std;


void ReadNum(int &Num)
{
cout << "Please enter numbers from 1 to N?" << endl;
cin >> Num;
}
void PrintNum(int Num)
{
for (int i=1 ; i <= Num; i++)
{
cout << i << endl;
}
}

int main()
{
int Num;
ReadNum(Num);
PrintNum(Num);

return 0;
}
----------------------
#Problem 27
#include <iostream>

using namespace std;

void ReadNum(int &Num)


{
cout << "Please enter numbers from N to 1?" << endl;
cin >> Num;
}
void PrintNum(int Num)
{
for (int i = Num; i >= 1; i--)
{
cout << i << endl;
}
}

int main()
{
int Num;
ReadNum(Num);
PrintNum(Num);

return 0;
}
-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
-----------------------

Lesson _48 - For Loop _ Arrays


#include <iostream>

using namespace std;


void ReadArrayData(int Arr1[100], int& Length)
{

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];

}
}

void PrintArrayData(int Arr1[100], int Length)


{

for (int i = 0; i <= Length - 1; i++)


{

cout << "Number [" << i + 1 << "] : " << Arr1[i] << endl;
}
}

int CalculateArraySum(int Arr1[100], int Length)


{
int Sum = 0;

for (int i = 0; i <= Length - 1; i++)


{
Sum += Arr1[i]; // Sum = Sum + Arr1[i]
}

return Sum;
}

float CalculateArrayAverage(int Arr1[100], int Length)


{
return (float)CalculateArraySum(Arr1, Length) / Length;
}
int main()
{
int Arr1[100], Length = 0;
ReadArrayData(Arr1, Length);
PrintArrayData(Arr1, Length);

int Sum = CalculateArraySum(Arr1, Length);

cout << "\n***********************************\n";


cout << "Sum = " << Sum << endl;
cout << "Average = " << Sum / Length << endl;

return 0;
}
-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
-----------------------
Homework lesson 48 ‫مهم جدا‬
#include <iostream>
#include <string>

using namespace std;

struct strInfo {
string FirstName;
string LastName;
int Age;
string Phone;
};

void ReadInfo(strInfo& Info)


{
cout << "Please enter your First Name?" << endl;
cin >> Info.FirstName;

cout << "Please enter your Last Name?" << endl;


cin >> Info.LastName;

cout << "Please enter your Age?" << endl;


cin >> Info.Age;

cout << "Please enter your Phone?" << endl;


cin >> Info.Phone;

cout << "----------------------------------" << endl;


}

void PrintInfo(strInfo Info)


{
cout << "\n**************************************\n";
cout << "FirstName: " << Info.FirstName << endl;
cout << "LastName: " << Info.LastName << endl;
cout << "Age: " << Info.Age << endl;
cout << "Phone: " << Info.Phone << endl;
cout << "\n**************************************\n";
}

void ReadPersonsInfo(strInfo Persons[100], int& NumberOfPersons)


{
cout << "How many Persons do you want to enter" << endl;
cin >> NumberOfPersons;

for (int i = 0; i <= NumberOfPersons - 1; i++)


{
cout << "Please enter persons " << i + 1<< "Info:" << endl;
ReadInfo(Persons[i]);
}
}

void PrintPersonsInfo(strInfo Persons[100], int Per)


{
for (int i = 0; i <= Per - 1; i++)
{
PrintInfo(Persons[i]);
}
}
int main()
{
strInfo Persons[100];
int NumberOfPersons = 1;

ReadPersonsInfo(Persons, NumberOfPersons);
PrintPersonsInfo(Persons, NumberOfPersons);

return 0;
}
-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
-----------------------

Lesson 49 - Nested For Loops


#include <iostream>
#include <string>

using namespace std;

int main()
{

for (int i = 1; i <= 12; i++)


{
cout << "i=" << i << endl;

for (int t = 0; t <= 12; t++)


{
cout << i << " * " << t << " = " << i * t << "\n";
}

cout << "---------------------------\n";


}

//--------------------------------------------------------
for (int i = 1; i <= 10; i++)
{

for (int t = 1; t <= i; t++)


{
cout << " * ";
}

cout << endl;


}
return 0;
}
-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
-----------------------

Homework lesson 49
#include <iostream>

using namespace std;


int main()
{
for (int i = 65; i <= 90; i++)
{
cout << "Letter:" << char(i) << endl;

for (int j = 65; j <= 90; j++)


{
cout << char(i) << char(j) << endl;
}

cout << "------------------------\n";


}

return 0;
}
-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
-----------------------

Lesson _50 - While Loop


#include <iostream>

using namespace std;

int main()
{
cout << "\n For Loop \n";
// for loop from 1 to 5
for (int x = 1; x <= 5; x++)
{
cout << x << endl;
}

cout << "\n While Loop \n";


// while loop from 1 to 5
int i = 1;
while (i <= 5)
{
cout << i << endl;
i++;
}
return 0;
}
----------------------------------
#include <iostream>

using namespace std;

int main()
{
int Number;
cout << "Please enter a positive number?\n";
cin >> Number;

while (Number < 0)


{
cout << "Wrong number, Plz Enter a positive Number ?\n";
cin >> Number;
}

cout << "\n The number you entered is " << Number << endl;
return 0;
}
--------------------------------------------------------------------
#include <iostream>

using namespace std;

int ReadIntNumberInRange(int From, int To)


{
int Number;
cout << "Please enter anumber between " << From << " and " << To << endl;
cin >> Number;

while (Number < From || Number > To)


{
cout << "Wrong Number,";
cout << "Please enter a number between " << From << " and " << To << endl;
cin >> Number;
}

return Number;
}

int main()
{
cout << "\n The Number is " << ReadIntNumberInRange(18, 45) << endl;

return 0;
}
-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
-----------------------

Homework lesson 50
#Problem 26
#include <iostream>

using namespace std;

void ReadNum(int &Num)


{
cout << "Enter number from 1 to N.\n";
cin >> Num;

int Counter = 1;

while (Num >= Counter)


{
cout << Counter << endl;
Counter = Counter + 1;
}
}
int main()
{
int Num;
ReadNum(Num);

return 0;
}
--------------------------------------------
#Problem 27
#include <iostream>

using namespace std;

void ReadNum(int &Num)


{
cout << "Enter number from N to 1.\n";
cin >> Num;

int Counter = 1;

while (Num >= Counter)


{
cout << Num << endl;
Num--;
}
}

int main()
{
int Num;
ReadNum(Num);

return 0;
}
-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
-----------------------

Lesson _51 - Do . . . While Loop


#include <iostream>

using namespace std;

int ReadIntNumberInRangeUsingWhile(int From, int To)


{
int num;
cout << "Please enter number between " << From << " and " << To << endl;
cin >> num;

while (num < From || num > To)


{
cout << "Wrong Number, ";
cout << "Please enter a number between " << From << " and " << To << endl;
cin >> num;
}

return num;
}

int ReadIntNumberInRangeUsingDoWhile(int From, int To)


{
int 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;
}
-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
-----------------------

Lesson _52 - Break Statement


#include <iostream>

using namespace std;

int main()
{
for (int i = 1 ; i <= 10; i++)
{

if (i == 3)
{
break;// ‫بتطلعه برا اللوب مش بتخليه يكمل الكود‬
}

cout << i << endl;


}
return 0;
}
--------------------------------------------
#include <iostream>

using namespace std;

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;
}
-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
-----------------------

Lesson _53 - Continue Statement


#include <iostream>

using namespace std;

int main()
{

for (int i = 1; i <= 5; i++)


{

if (i == 3)
{
continue;//‫ و مش هيطبعها‬3‫هيجي عند ال‬
}

cout << i << endl;


}

return 0;
}
---------------------------------------------
#include <iostream>

using namespace std;

int main()
{
int sum = 0;
int num = 0;

for (int i = 1; i <= 5; i++)


{
cout << "Enter a number : ";
cin >> num;
if (num > 50)
{
cout << "The number is greater than 50 and won't be calculate ." <<
endl;

continue;
}

sum += num;
}

return 0;
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy