Você está na página 1de 8

2/5/2016

Computer Science I
Lab 5

08/02/2015 - Shakhawan H. Mahmood Salahaddin University

Example 1
#include <iostream>
using namespace std;
int main()
{ //This program shows us the size of each data type
cout << "Size of int : " << sizeof(int) << endl;
cout << "Size of short int : " << sizeof(short int) << endl;
cout << "Size of long int : " << sizeof(long int) << endl;
cout << "Size of float : " << sizeof(float) << endl;
cout << "Size of double : " << sizeof(double) << endl;
cout << "Size of long double : " << sizeof(long double) << endl;
cout << "Size of boolean : " << sizeof(bool) << endl;
cout << "Size of char : " << sizeof(char) << endl;
return 0;
}
08/02/2015 - Shakhawan H. Mahmood Salahaddin University

2/5/2016

Example 2
#include <iostream>
using namespace std;
int main()
{
//Practice on Characters
cout << "Hello, W" << 'o' << "rld" << '!' << '\n';
//Practice on Horizontal Tab
cout << "Hello, World!" << '\t' << "New, World!" << '\n';
//Practice on Alert Character
cout << " Alert Character: " << '\a' << "Toooot!" << '\n';
//Practice on Literals
cout << "The product expires on Dec " << 3 << 1 << ' ' << 2015 << '\n';
return 0;
}
08/02/2015 - Shakhawan H. Mahmood Salahaddin University

Example 3
Write a C++ program to find the Circumference of a
circle, where Circumference = 2 * PI * r , PI =
3.14159
Hint: PI is constant.

08/02/2015 - Shakhawan H. Mahmood Salahaddin University

2/5/2016

Example 3
//Practice on Constants
#include <iostream>
using namespace std;
int main()
{
const double PI = 3.14159;
double r = 5.0;
// radius
double circum ;
circum = 2 * PI * r ;
cout << circum ;
return 0;
}
08/02/2015 - Shakhawan H. Mahmood Salahaddin University

Example 3 - Updated
//Practice on Constants
#include <iostream>
using namespace std;
int main()
{
const double PI = 3.14159;
const char NEWLINE = '\n';
double r = 5.0;
// radius
double circum ;
PI = 3 ; //What will happen?
circum = 2 * PI * r;

08/02/2015 - Shakhawan H. Mahmood Salahaddin University

cout <<"The circumference is ";


cout << NEWLINE;
cout << circum ;
return 0;
}

2/5/2016

Example 4
//This program adds two numbers
#include<iostream>
using namespace std;
int main(){
int num_1 = 0, num_2 = 5; /*declaration and initialization of two
integer numbers*/
int sum = 0;
sum = num_1 + num_2; //adding both numbers
cout<<Sum of the two numbers = <<sum<<endl;
return 0;
}
08/02/2015 - Shakhawan H. Mahmood Salahaddin University

Example 4 - Updated
//This program adds two numbers
#include<iostream>
using namespace std;
int main(){
int num_1 = 0, num_2 = 5; /*declaration and initialization of two
integer numbers*/
int sum = 0;
num_1 = 4.5;
//assigning num_1 with a float number
//What happens?
sum = num_1 + num_2; //adding both numbers
cout<<Sum of the two numbers = <<sum<<endl;
return 0;
}
08/02/2015 - Shakhawan H. Mahmood Salahaddin University

2/5/2016

Example 4 - Updated
//This program adds two numbers
#include<iostream>
using namespace std;
int main(){
int num_1 = 0, num_2 = 5; /*declaration and initialization of two
integer numbers*/
int sum = 0;
num_1 = 4.5;
//assigning num_1 with a float number
num_2 = 6;
//What happens?
sum = num_1 + num_2; //adding both numbers
cout<<Sum of the two numbers = <<sum<<endl;
return 0;
}
08/02/2015 - Shakhawan H. Mahmood Salahaddin University

Example 4 - Updated
//This program adds two numbers
#include<iostream>
using namespace std;
int main(){
float num_1 = 0.5, num_2 = 5; /*declaration and initialization of
two float numbers*/
int sum = 0;
sum = num_1 + num_2; //adding both numbers
cout<<Sum of the two numbers = <<sum<<endl;
return 0;
}
08/02/2015 - Shakhawan H. Mahmood Salahaddin University

10

2/5/2016

Example 4 - Updated
//This program adds two numbers
#include<iostream>
using namespace std;
int main(){
float num_1 = 0.5, num_2 = 5; /*declaration and initialization of
two float numbers*/
float sum = 0.0;
sum = num_1 + num_2; //adding both numbers
cout<<Sum of the two numbers = <<sum<<endl;
return 0;
}
08/02/2015 - Shakhawan H. Mahmood Salahaddin University

11

Example 5
//Practice on Data Types (Integers)
#include <iostream>
using namespace std;
int main()
{
int num_1 = 0 ;
int num_2 = 0 ;
num_1 = 3/4 ;
num_2 = 5/2 ;
cout << num_1 = << num_1 << endl ;
cout << num_2 = << num_2 << endl ;
return 0;
}
08/02/2015 - Shakhawan H. Mahmood Salahaddin University

//What is the output?


//What is the output?

12

2/5/2016

Example 6
//Practice on Data Types (Mixing Floating points & Integers)
#include <iostream>
using namespace std;
int main()
{
double num_1 = 0.0 , percent = 0.0 , total = 0.0 ;
num_1 = 5 / 2.0 ;
percent = 0.50 * 100;
total = 50 + 7.5;
cout << num_1 = << num_1 << endl ;
//What is the output?
cout << percent = << percent << endl ; //What is the output?
cout << total = << total << endl ;
//What is the output?
return 0;
}
08/02/2015 - Shakhawan H. Mahmood Salahaddin University

13

Example 7
//Practice on Data Types (Mixing Floating points & Integers)
#include <iostream>
using namespace std;
int main()
{
double result = 0.0, num_1 = 0.0 , num_2 = 0.0 ;
result = 5 / 2 ;
cout << "First result = " << result << endl ;
//What is the output?
num_1 = 5 ;
num_2 = 2 ;
result = num_1 / num_2;
cout << "Second result = " << result << endl ;
//What is the output?
return 0;
}
08/02/2015 - Shakhawan H. Mahmood Salahaddin University

14

2/5/2016

References
Nassir, H.S.2009. C++ programming with 469 solved problems.
Deitel, H.M. 2005. C++ How to program.
John R. H. 2000. Programming with C++.

08/02/2015 - Shakhawan H. Mahmood Salahaddin University

15

Questions?

08/02/2015 - Shakhawan H. Mahmood Salahaddin University

16

Você também pode gostar