Académique Documents
Professionnel Documents
Culture Documents
A-)
#include <iostream>
using namespace std;
int main()
{
int count = 1;
Output
*****
+++++
*****
+++++
*****
+++++
*****
+++++
*****
+++++
Press any key to continue
B-)
#include <iostream>
using namespace std;
int main()
{
int row = 10, column;
1
Amer Daradkah
--row;
cout << endl;
}
return 0;
}
Output
$$$$$$$$$$
**********
$$$$$$$$$$
**********
$$$$$$$$$$
**********
$$$$$$$$$$
**********
$$$$$$$$$$
**********
Press any key to continue
C-)
#include <iostream>
using namespace std;
int main()
{
int x, y;
cout << "Enter two integers in the range 1-20: ";
cin >> x >> y;
for ( int i = 1; i <= y; i++ ) {
return 0;
}
Output
2
Amer Daradkah
D-)
#include <iostream>
using namespace std;
int main()
{
// initialization phase
total = 0; // clear total
gradeCounter = 1; // prepare to loop
// processing phase
while ( gradeCounter <= 10 ) { // loop 10 times
cout << "Enter grade: "; // prompt for input
cin >> grade; // input grade
total = total + grade; // add grade to total
gradeCounter = gradeCounter + 1; // increment counter
}
// termination phase
average = total / 10; // integer division
cout << "Class average is " << average << endl;
return 0;
}
Output
Enter grade: 85
Enter grade: 88
Enter grade: 88
Enter grade: 73
Enter grade: 82
Enter grade: 97
Enter grade: 81
Enter grade: 67
Enter grade: 62
Enter grade: 51
Class average is 77
Press any key to continue
E-)
#include <iostream>
using namespace std;
int main()
{
int c;
c = 5;
cout << c << endl; // print 5
3
Amer Daradkah
return 0;
}
Output
5
5
6
5
6
6
Press any key to continue
F-)
#include <iostream>
int main()
{
return 0;
}
Output
1
2
3
4
5
6
7
8
9
10
Press any key to continue
G-)
4
Amer Daradkah
#include <iostream>
using namespace std;
int main()
{
// Initialization, repetition condition, and incrementing
// are all included in the for structure header.
return 0;
}
Output
1
2
3
4
5
6
7
8
9
10
Press any key to continue
H-)
#include <iostream>
using namespace std;
int main()
{
int sum = 0;
for ( int number = 2; number <= 10; number += 2 )
sum += number;
cout << "Sum is " << sum << endl;
return 0;
}
Output
Sum is 30
Press any key to continue
K-)
#include <iostream>
using namespace std;
int main()
{
int i=0.5;
while (i<20.5)
{cout<<i<<"\t ";
5
Amer Daradkah
i+=5;
}
cout<<"just\n";
return 0;
}
Output
0 5 10 15 20 just
Press any key to continue
L-)
#include <iostream>
using namespace std;
int main()
{
int counter = 1;
do {
cout << counter << " ";
} while ( ++counter <= 10 );
1 2 3 4 5 6 7 8 9 10
Press any key to continue
M-)
#include <iostream>
using namespace std;
int main()
{
// x declared here so it can be used after the loop
int x;
if ( x == 5 )
break; // break loop only if x is 5
return 0;
}
Output
1234
Broke out of loop at x of 5
Press any key to continue
6
Amer Daradkah
N-)
#include <iostream>
using namespace std;
int main()
{
for ( int x = 1; x <= 10; x++ ) {
if ( x == 5 )
continue; // skip remaining code in loop
// only if x is 5
return 0;
}
Output
1 2 3 4 6 7 8 9 10
Used continue to skip printing the value 5
Press any key to continue
O-)
#include <iostream>
using namespace std;
int cube( int y );
int main()
{
int x;
return 0;
}
Output
1
8
27
64
125
7
Amer Daradkah
P-)
#include <iostream>
using namespace std;
double PI = 3.14159;
double sphereVolume(double r )
{ return 4.0 / 3.0 * PI * r * r * r; } // function prototype +Function definition
int main()
{
double radius;
cout << "Enter the length of the radius of your sphere: ";
cin >> radius;
cout << "Volume of sphere with radius " << radius <<
" is " << sphereVolume( radius ) << endl;
return 0;
}
Output
Enter the length of the radius of your sphere: 1
Volume of sphere with radius 1 is 4.18879
Press any key to continue
Q-)
#include <iostream>
using namespace std;
int main()
{
int x, y;
8
Amer Daradkah
Output
Enter two integers: 1
3
The result is 3
Press any key to continue
R-)
#include <iostream>
using namespace std;
int square( int ); // function prototype
int main()
{
for ( int x = 1; x <= 10; x++ )
cout << square( x ) << " ";
// Function definition
int square( int y )
{
return y * y;
}
Output
1 4 9 16 25 36 49 64 81 100
Press any key to continue
S-)
#include <iostream>
using namespace std;
int main()
{
int a, b, c;
return 0;
9
Amer Daradkah
if ( y > max )
max = y;
if ( z > max )
max = z;
return max;
}
Output
T-)
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
cout << "Enter the side length of your cube: ";
double side;
return 0;
}
Output
Enter the side length of your cube: 3
Volume of cube with side 3 is 27
Press any key to continue
V-)
10
Amer Daradkah
int main()
{
return 0;
}
Output
NO Output
Press any key to continue
W-)
#include <iostream>
using namespace std;
int main()
{
for(int i=1;i<=5;i++)
{
for(int j=1 ; j<=i ; j++)
if (i==j||i==5||j==1)
cout<<"*";
else
cout<<" ";
cout<<endl;
}
return 0;
}
Output
*
**
**
* *
*****
Press any key to continue
X-)
#include <iostream>
using namespace std;
11
Amer Daradkah
int main()
{
int x=1;
while (x<=5)
{
x++;
cout<<"The Value of x is : "<<x<<endl;
}
cout<<"The Final Value Of x Is : "<<x<<endl;
return 0;
}
Output
The Value of x is : 2
The Value of x is : 3
The Value of x is : 4
The Value of x is : 5
The Value of x is : 6
The Final Value Of x Is : 6
Press any key to continue
Y-)
#include <iostream>
using namespace std;
int main()
{
return 0;
}
Output
The value of x is 1
The value of x is neither 1,3 nor 5
The value of x is 3
12
Amer Daradkah
The value of x is 5
The value of x is neither 1,3 nor 5
The value of x is neither 1,3 nor 5
The value of x is 5
The value of x is neither 1,3 nor 5
Press any key to continue
Z-)
#include <iostream>
using namespace std;
int main()
{
return 0;
}
Output
12456
The final Value of i is :7
Press any key to continue
a-)
#include <iostream>
using namespace std;
int main()
{
int i=1 ;
for(;i<=10;i++);
return 0;
}
Output
b-)
13
Amer Daradkah
#include <iostream>
using namespace std;
int main()
{
for(int i=1 ; i<=5 ; i++)
{
switch (i)
{
case 3:
cout<<"i=3\n";
break;
case 5:
cout<<"i=3\n";
continue ;
case 4:
cout<<"Just";
default :
cout<<"*** i = "<<i<<endl;
}
}
return 0;
}
Output
*** i = 1
*** i = 2
i=3
Just*** i = 4
i=3
Press any key to continue
c-)
#include <iostream>
using namespace std;
int main()
{
int i = 5;
for( ; ; i+=5)
{
cout<< i <<endl;
if(i=1000)
break;
}
return 0;
}
Output
5
Press any key to continue
14
Amer Daradkah
d-)
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
double x = -5.49;
cout << ceil(x) <<endl << fabs(x) << endl << floor(x)<<endl;
return 0 ;
}
Output
-5
5.49
-6
Press any key to continue
e-)
#include <iostream>
#include<iomanip>
using namespace std;
int main ()
{
return 0;
}
Output
3
Press any key to continue
f-)
#include <iostream>
#include<iomanip>
bool prime(int) ; // Prototype for Function prime
using namespace std;
15
Amer Daradkah
int main()
{
int count=0;
for(int loop=2;loop<=100;loop++)
{
if(prime(loop))
{
cout << setw(5) << loop; // calling statement
count ++;
if(count % 10==0)
cout << "\n";
} //end if
} // end for
return 0;
}
return false;
return true;
}
Output
2 3 5 7 11 13 17 19 23 29
31 37 41 43 47 53 59 61 67 71
73 79 83 89 97
Press any key to continue
g -)
#include <iostream>
using namespace std ;
void a( void ); // function prototype
void b( void ); // function prototype
void c( ); // function prototype
int main()
{
int x = 5; // local variable to main
cout << "local x in outer scope of main is " << x << endl;
16
Amer Daradkah
cout << "local x in inner scope of main is " << x << endl;
} // end new scope
cout << "local x in outer scope of main is " << x << endl;
return 0;
}
void a( void )
{
int x = 25; // initialized each time a is called
void b( void )
{
static int x = 50; // Static initialization only
// first time b is called.
cout << endl << "local static x is " << x
<< " on entering b" << endl;
++x;
cout << "local static x is " << x
<< " on exiting b" << endl;
}
void c( void )
{
cout << endl << "global x is " << x
<< " on entering c" << endl;
x *= 10;
cout << "global x is " << x << " on exiting c" << endl;
}
Output
17
Amer Daradkah
global x is 1 on entering c
global x is 10 on exiting c
global x is 10 on entering c
global x is 100 on exiting c
local x in main is 5
Press any key to continue
h -)
int main()
{
int x = 2, z = 4;
cout << "z = " << z << " before squareByReference" << endl;
squareByReference( z );
cout << "z = " << z << " after squareByReference" << endl;
return 0;
}
Output
x = 2 before squareByValue
Value returned by squareByValue: 4
x = 2 after squareByValue
18
Amer Daradkah
z = 4 before squareByReference
z = 16 after squareByReference
Press any key to continue
k -)
int main ( )
{
int x = 3, &y = x; // y is now an alias for x
cout << "x = " << x << endl << "y = " << y << endl;
y = 7;
cout << "x = " << x << endl << "y = " << y << endl;
return 0;
}
Output
x=3
y=3
x=7
y=7
Press any key to continue
l -)
int main ( )
{
int x = 3, &y; // Error: y must be initialized
cout << "x = " << x << endl << "y = " << y << endl;
y = 7;
cout << "x = " << x << endl << "y = " << y << endl;
return 0;
}
Output
NO Output
Error executing cl.exe.
m -)
#include <iostream>
using namespace std ;
int whatIsThis( int [ ], int );
19
Amer Daradkah
int main ( )
{
const int arraySize = 5;
int a[ arraySize ] = { 1, 2, 3, 4, 5 };
Result is 15
n -)
#include <iostream>
using namespace std ;
void someFunction( int [ ] , int );
int main ( )
{
const int arraySize = 5;
int a[ arraySize ] ={1, 2, 3, 4, 5};
Output
20
Amer Daradkah
p -)
#include <iostream>
using namespace std ;
#include <iomanip>
int main ( )
{
int i , n[ 10 ];
return 0;
}
Output
Element Value
0 2
1 3
2 4
3 5
4 6
5 7
6 8
7 9
8 10
9 11
Press any key to continue
q -)
#include <iostream>
using namespace std ;
#include <iomanip>
int main ( )
{
int n[ 10 ] = { 32, 27, 64, 18, 95, 14, 90, 70, 60, 37 };
return 0;
}
Output
21
Amer Daradkah
Element Value
0 32
1 27
2 64
3 18
4 95
5 14
6 90
7 70
8 60
9 37
Press any key to continue
r -)
int main ( )
{
const int arraySize = 10;
int j , s[ arraySize ];
return 0;
}
Output
Element Value
0 2
1 4
2 6
3 8
4 10
5 12
6 14
7 16
8 18
9 20
Press any key to continue
s -)
22
Amer Daradkah
return 0;
}
Output
t -)
int main ( )
{
const int x; // Error: x must be initialized
return 0;
}
Output
NO Output
Error executing cl.exe.
v -)
23
Amer Daradkah
int main ( )
{
cout << "First call to each function:\n";
staticArrayInit ( );
automaticArrayInit ( );
return 0;
}
Output
First call to each function:
24
Amer Daradkah
w -)
int main()
{
const int arraySize = 5;
int i, a[ arraySize ] = { 0, 1, 2, 3, 4 };
modifyElement( a[ 3 ] );
return 0;
}
25
Amer Daradkah
Output
Effects of passing entire array call-by-reference:
x -)
int main ( )
{
int a [ ] = { 10, 20, 30 };
tryToModifyArray( a );
cout << a[ 0 ] << ' ' << a[ 1 ] << ' ' << a[ 2 ] << '\n';
return 0 ;
}
26
Amer Daradkah
b[ 2 ] /= 2; // error
}
Output
NO Output
Error executing cl.exe.
y -)
int main ( )
{
const int arraySize = 10;
int a[ arraySize ] = { 2, 6, 4, 8, 10, 12, 89, 68, 45, 37 };
int i, hold;
Output
27
Amer Daradkah
z -)
#include <iostream>
using namespace std;
#include <string>
int main ( )
{
string A1 = "Amer daradkah" ;
string A2 = " Osama omar";
string A3 = "Just University";
string A4 = A1 + " in " + A3 ;
cout << A4 << endl;
cout << "A2.length = "<< A1.length ( ) <<endl ; //*you must use ( ) empty
cout << "A2.size = "<< A2.size ( ) <<endl;
cout << "A2.size = "<<(A1+A2).size ( ) <<endl;
cout <<"A1.find('a') = "<<A1.find('a')<<endl;
cout <<"A2.find('o') = "<<A2.find('o')<<endl;
cout << "A3.substr(4,6) = "<< A3.substr (4,6)<<endl;
cout <<"A4.find (\" in \") = "<< A4.find (" in ")<< endl;
cout <<"A3.find ('s',3) = "<<A3.find ('s',3)<< endl;
cout <<"A4.find ('x') = "<<A4.find ('x')<< endl;
return 0;
}
Output
* To use rand ( ) you must use this order #include < cstdlib >
Eg. : [ 2 , 27 ]
Cout << 2 +rand ( ) % ( 27-2+1 ) ;
28
Amer Daradkah
29
Amer Daradkah
30
Amer Daradkah
Important Examples
į)
#include <iostream>
using namespace std ;
int a=5;
void f( int a ) // function prototype
{
a+=5;
cout<<a<<endl;
}
void f1( )
{ a+=2;
cout<<a<<" ";}
void main ( )
{
cout<<a<<endl;
}
31
Amer Daradkah
Output
5 15
10 9
7 11
Press any key to continue
įį )
#include<iostream>
using namespace std ;
void a1( );
void a2( ) ;
int x=2;
void main ( )
{
cout<<x ;
{
x+=2;
cout<< x ;
}
{
int x=9;
x++;
{
cout << ++x ;
a1( );
}
a1 ( );
a2 ( );
}
a2 ( );
a1 ( );
cout << endl ;
}
void a1 ( )
{
static int x=4;
cout << ++x;
}
void a2 ( )
{
cout<<--x;
}
Output
241156327
Press any key to continue
32
Amer Daradkah
įįį )
#include<iostream>
using namespace std ;
int x=20 ;
void f1( )
{
cout<< x<<" ";
x++ ;
}
void main ( )
{
f1( );
{
int x = 2 ;
cout << x << " ";
::x = x+1 ;
}
cout << x << " ";
::x = ::x+2 ;
f1( );
}
Output
20 2 3 5 Press any key to continue
IV )
#include<iostream>
using namespace std ;
void main ( )
{
int i = 7 ;
while ( i != 10 )
i++ ;
for ( i ; i < 12 ; i++ )
cout << i << " ";
Output
10 11 Press any key to continue
Good Luck
33
Amer Daradkah
Amer Daradkah
34