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

2-Introduction To Programming - Lectuer1 DR Albert - Part 2

introduction to programming

Uploaded by

lodygamal002
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views

2-Introduction To Programming - Lectuer1 DR Albert - Part 2

introduction to programming

Uploaded by

lodygamal002
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

Example 3

Draw the flowchart that ask the user to enter a


radius of a circle then it calculates the area and
circumference of this circle
𝐴𝑟𝑒𝑎 = 𝜋𝑟 2
circumference =2𝜋𝑟
𝜋 =3.14
Simple C++ program Debugging Example C++ Arithmetic
operators Postfix and Prefix Assignment
#include<iostream>
Solution using namespace std;
int main( )
start {
float r , area,circum ;
output
“enter the radius” Cout<<“Enter radius”;
cin >> r ;
Input r
area=3.14*r*r;
area=3.14*r*r circum=2*3.14*r;
circum=2*3.14*r
cout<<“area”<<area;
output cout << “circum”<<circum;
area, circum
return 0;
end
}
Creating New Lines in Output

Programmer must specify new line


Line feeding
\n in string constant
cout << “\n Hello.”;

– endl we can jump

– cout << endl<<“we can jump “;


two lines.

Lesson 2.3
Constants

• Constant is the term that has a unique value and


can't be changed during the program execution.

• Declaration:

1. #define constant_name constant_value

Example: #define PI 3.14


Constants
# include <iostream>
# define PI 3.14
using namespace std;
int main ( )
{
float r, Area;
cout<< “ Please enter a radius: “ ;
cin>> r;
Area = PI * r * r ;
cout<< “ The area of the circle is ” << Area ;
return 0;
}
Debugging Example
#<include iostream> # include <iostream>
OK using namespace std; using namespace std;
int main ( ); int main ( )
( {
cout << ‘Hello world!’ cout << “Hello world!”;
cout << “Hello again”, endl; cout << “Hello again”<< endl;
// Next line will output // Next line will output a name!
a name! cout << “Sally Student”;
ccut << “Sally Student”; /* Next line will
/* Next line will output another name */
output another name /*
cout << John Senior; cout << “John Senior” ;
} }
Arithmetic operators

operation C++ operator expression


Addition + X+y
Subtraction - X-y
Multiplication * X*y
Division / x/y
Modulus % X%y
Priority

1 () Inner most pair first


2 */% From left to right
3 +- From Left to right
M=12-4/2+4*3
M= 12- 2 + 4*3
M=12-2 + 12
M=10+12=22
Shorthand Assignment Operators

+=,-=,*=,/=,%=,>>=,<<=,&=,|=,and ^=
Examples
Given:
int x=5,y=6,z=9,m;
float w=3.4 , k=4;
So.,
m=y/2; means m=3
m=x/2; means m=
m=5%2; means m=
m=6%2; means m=
m=7%2; means m=
M =W%2; means m= error
Postfix and Prefix Assignment
i++
++i
i--
--i
i++
first provides the value of i to
then adds I to i, while
++i
does the addition first
What is the output? What is the output?
Int x =5;
Int x =5; Cout<<“x=“<<++x<<endl;
Cout <<“x=“<<x++<<endl; X+=10;
Cout<<x;
X+=10;
Cout<<x; Solution
X=6
Solution 16

X=5
16
What is the output?
Int x =5,y=3,z=7;
z=z/2+ ++y*x--;
Cout <<“x=“<<x<<endl;
Cout <<“y=“<<y<<endl;
Cout <<“z=“<<z<<endl;

Solution
X=
Y=
Z=
What is the output? Z= z/2+ ++y *x--;
Int x =5,y=3,z=7;
variable= z/2+ ++y *x--;
Z=z/2+ ++y*x--;
variable=7/2 +
Cout <<“x=“<<x<<endl;
variable=7/2 + 4*
Cout <<“y=“<<y<<endl;
variable=7/2 + 4*5
Cout <<“z=“<<z<<endl;
X- - Screen
Memory X=4
Solution
X=5 ->4
X= Y=4
Y=3 ->4
Y=
Z=7 ->23 Z=23
Z=
TRY
int x=5,y,z;
y=x%3;
z=x+y%--y;
cout<<++x<<endl;
cout<<y++<<endl;
cout<<++z<<endl;
x+=y++*2;
z=x+y;
cout<<++x<<endl;
cout<<++y<<endl;
cout<<z++<<endl;
system("pause");
}

You might also like