You are on page 1of 23

C++ HANDS-ON

LECTURES
Source code in c++

Lesson 1
1)
2)
3)
4)
5)
6)

//Program: Display Greetings


//Author: Ima Programmer
//Date: August 13, 2015
#include <iostream>
#include <string>
using namespace std;

7)
8)
9)
10)
11)
12)

int main() {
cout << "Hello World!" << endl;
cin.get();
return 0;
}

Lesson 2 Naming Variables


1)
2)
3)
4)
5)
6)
7)
8)
9)
10)
11)
12)
13)

#include <iostream>;
using namespace std;
int main()
{
int apples;
apples = 3;
int oranges;
oranges = 5;
cout<<"There are "<<apples<<" apples"<<endl;
cout<<"There are "<<oranges<<" oranges"<<endl;
cin.get();
return 0;
}

Lesson 3
1)
2)
3)
4)
5)
6)
7)
8)
9)
10)
11)
12)
13)
14)
15)
16)
17)

#include <iostream>;
using namespace std;
int main()
{
int apples;
apples = 3;
int oranges;
oranges = 5;
int fruits;
fruits = apples + oranges;

cout<<"there are "<<apples<<" apples"<<endl;


cout<<"there are "<<oranges<<" oranges"<<endl;
cout<<"there are "<<fruits<<" fruit"<<endl;
cin.get();
return 0;
}

Lesson 4
1)
2)
3)
4)
5)
6)
7)
8)
9)
10)
11)

#include<iostream>
using namespace std;

int main ()
{
int apples = 3;
int apples = 5;
cout<< apples;
cin.get();
return 0;
}

Lesson 5 Mathematical
Operators
1)
2)
3)
4)
5)
6)
7)
8)
9)
10)
11)

#include <iostream>
using namespace std;

int main()
{
int x = 129;
int y = 5;
cout<<"The product is "<<x*y;
cin.get();
return 0;
}

Lesson 6 Float and Double


Type
1)
2)
3)
4)
5)
6)
7)
8)
9)
10)
11)
12)
13)

#include <iostream>
using namespace std;

int main()
{
double weight = 300000.1;
double z = 300000.1;
double total = weight + z;
cout<<weight;
cout<<endl<<total-600000;
cin.get();
return 0;
}

Lesson 7 The cin


Statement
1)
2)
3)
4)
5)
6)
7)
8)
9)
10)
11)
12)

#include <iostream>
using namespace std;

int main()
{
int x;
cout<<"Enter an Integer Value:";
cin>>x;
cout<<"You have Entered "<<x<<endl;
system("pause");
return 0;
}

Lesson 8 The Char Type


1)
2)
3)
4)
5)
6)
7)
8)
9)
10)
11)

#include <iostream>
using namespace std;

int main()
{
char character = 't';
cout <<character;
cout<<endl;
system("pause");
return 0;
}

Lesson 9 The Boolean


Type
1)
2)
3)
4)
5)
6)
7)
8)
9)
10)
11)

#include <iostream>
using namespace std;

int main()
{
bool smart = true;
cout<<smart;
cout<<endl;
cin.get();
return 0;
}

Lesson 10 Introduction to If
Statement
1)
2)
3)
4)
5)
6)
7)
8)
9)
10)
11)
12)

#include <iostream>
using namespace std;
int main()
{
if (false)
{
cout<<"Hi! My name is "<<endl;
cout<<"What is your name?";
}
cin.get();
return 0;
}

Lesson 11 Relational
Operators (Part 1)
1)
2)
3)
4)
5)
6)
7)
8)
9)
10)

#include <iostream>
using namespace std;

int main()
{
bool Boolean = 3 != 3;
cout<<Boolean;
cin.get();
return 0;
}

Lesson 12 Relational
Operators (Part 2)
1)
2)
3)
4)
5)
6)
7)
8)
9)
10)
11)
12)
13)

#include <iostream>
using namespace std;
int main()
{
bool Boolean = (7==7);
if (Boolean)
{
cout<<"This is True";
}
cout<<Boolean;
cin.get();
return 0;
}

Lesson 13
#include <iostream>
2) using namespace std;
3) int main()
4) {
5)
int apples, bananas;
6)
cout<<"Enter the number of bananas"<<endl;
7)
cin>>bananas;
8)
cout<<"Enter the number of apples"<<endl;
9)
cin>>apples;
10) if (apples<bananas)
11) {
12) cout<<"There are more bananas ";
13) }
14) if (apples>bananas)
15) {
16) cout<<"There are more apples ";
17) }
18) if (apples==bananas)
19) {
20) cout<<"The number of apples and bananas are equal";
21) }
22) cout<<endl;
23) system("pause");
24) return 0;
25)}
1)

Lesson 14 Different Variable


Type
1)
2)
3)
4)
5)
6)
7)
8)
9)
10)
11)

#include <iostream>
using namespace std;

int main()
{
int apples=3, oranges=5, bananas=7;
float cash=2.78, mass=9.815;
cout<<mass/apples<<endl<<cash*apples;
cin.get();
return 0;
}

Lesson 15 If Else
Statement
1)
2)
3)
4)
5)
6)
7)
8)
9)
10)
11)
12)
13)
14)
15)
16)
17)

#include <iostream>
using namespace std;

int main()
{
int apples=5, oranges=3;
if (apples < oranges);
{
cout<<"more oranges"<<endl;
}
else
{
cout<<"more apples"<<endl;
}
cin.get();
return 0;
}

Lesson 16 Increment
Decrement
1)
2)
3)
4)
5)
6)
7)
8)
9)
10)
11)

#include <iostream>
using namespace std;

int main()
{
int rocks=5, coins=71;
coins = rocks+3;
cout<<coins;
cin.get();
return 0;
}

Increment Decrement
1)
2)
3)
4)
5)
6)
7)
8)
9)
10)
11)
12)
13)
14)
15)
16)
17)
18)
19)
20)
21)
22)
23)
24)

#include <iostream>
using namespace std;

int main()
{
int rocks=5, coins=71;
char choice = b;
cout<<Would you like to buy a rock for 1 coin?;
cin>>choice;
if (choice==y)
{
coins = coins 1
rocks = rocks +1
cout<<Thank you!
}
else
{
cout<<Thats too bad;
}
cout<<endl<<coins: <<coins<<endl;
cout<<rocks: <<endl;
system(pause);
return 0;
}

Increment Decrement
1)
2)

#include <iostream>
using namespace std;

3)
4)
5)
6)
7)
8)
9)
10)
11)

int main()
{
int C = 7;
C += 5;
cout<<C;
cin.get();
return 0;
}

The while loop


1)
2)

#include <iostream>
using namespace std;

3)
4)
5)
6)
7)
8)

int main()
{
int n=0;
while(n<=5)
{
cout<< n <<endl;
n = n + 1;

9)
10)
11)
12)

}
cin.get();
return 0;
}

while loop
#include <iostream>
using namespace std;
int main()
{
int x;
char choice = y;
while (choice == y)
{
cout<<Enter an integer value for x: ;
cin>> x;
if (x%2 == 0)
{
cout<<Even<<endl;
}
else
{
cout<<Odd<<endl;
}
cout<<Would you like to enter another value? <y/n>;
cin>>choice
}
system (pause);
return 0;
}

1)
2)
3)
4)
5)
6)
7)

#include <iostream>
using namespace std;
int main()
{
int loop = 0;
float number, sum = 0;
char choice = y;

while loop

8)

while (choice == y || choice == Y)


{
cout<<Enter a number: ;
cin>>number;
sum += number;
loop++;
cout<<Enter another number? <y/n> ;
cin>>choice;
}
cout<<the sum of all number is: <<sum<<endl;
cout<<average: <<sum/loop<<endl;
system (pause);
return 0;

9)
10)
11)
12)
13)
14)
15)
16)
17)
18)
19)
20)
21)
22)

Logical Operators
1)
2)

#include <iostream>
using namespace std;

3)
4)
5)
6)
7)

8)
9)
10)
11)
12)
13)
14)
15)

int main()
{
int a=1, b, c=8;
cout<<enter a value that is not between << a <<and << c
<<endl;
cin<< b;
if (a>b || b>c)
{
cout<<correct<<endl;
}
system (pause);
return 0;
}

You might also like