0% found this document useful (0 votes)
40 views45 pages

Section 3

Uploaded by

Sayed Younis
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)
40 views45 pages

Section 3

Uploaded by

Sayed Younis
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/ 45

Introduction to

Programming
Using C#

Lab 3

By:
Eng.Omar Islam
Lab Objectives

• Train to analyze a problem, identify the


required inputs and outputs, and define the
steps to transform inputs into outputs.

• Provide hands-on practice in basic arithmetic


operations (addition, subtraction,
multiplication, division)
Problem Analysis and
Program Design
• Step 1: Identify the Inputs
– Determine what data needs to be provided by the user or pre-defined.

• Step 2: Identify the Outputs


– Determine what results or information the program should produce.

• Step 3: Determine Processing Steps


– Define the series of operations or calculations that the program must
perform to generate the output from the input.
Algorithm & Flowchart

Definitions:
• 1- Algorithm: An algorithm is a detailed, step-
by-step set of instructions to solve a specific
problem.

• 2- Flowchart: A flowchart is a diagram that


represents the flow of the algorithm visually
Example

- Seek out the Algorithm ,Flowchart and the code


for adding two numbers.
Algorithm

• Start
• Get the first number (Input)
• Get the second number (Input)
• Add the two numbers (Process)
• Display the sum (Output)
• End
Flowchart
Code
Expression

• Definition: is a piece of code that resolves to a


value.
• Example: adding two values to compute a third
one.
Operators and Operands

• Operators: An operator takes one or more input operands to output


a new expression

• Operands: object that is being worked on by an operation.

• Output: the new computed value!


Assign operator

• Basic assignment operator (=) is used to assign


values to variables. For example:
Arithmetic Operators
Increment & Decrement
Example

Let a = 3, b = 6 and c = 0, determine the value of


a, b and c after performing the following
operations (note that the questions are not
cumulative).
1. c = (a++) + (b--) + a + b
2. c = (a++) + (++b) + a + b
3. c = (a++) + (--b) + a + b
Ans
• a=3 b=6 c=0
• 1- c = (a++) + (b--) + a + b:

(a++): a will be incremented by one and the old value is used

(b--): b will be decremented by one and the old value is used

Solution:

c = 3 + 6 + 4 + 5 = 18

a=4

b=5

c = 18
Ans Cont.
• a=3 b=6 c=0
• c = (a++) + (++b) + a + b
(a++): a will be incremented by one and the old value will be used

(++b): b will be incremented by one and the new value will be used

Solution:

c = 3 + 7 + 4 + 7 = 21

a=4

b=7

c = 21
Ans Con.

• a=3 b=6 c=0


• c = (a++) + (--b) + a + b
• (a++): a will be incremented by one and the old value will be used

• (--b): b will be decremented by one and the new value will be used

Solution:

c = 3 + 5 + 4 + 5 = 17

a=4

b=5

c = 17
Practice 1

• Create a program that reads two numbers and


subtracts the second number from the first.

• Input: Two numbers (integers or floats).


Output: The difference between the two
numbers.
Ans
Practice 2

• Create a program that reads two numbers from


the user and multiplies them.

• Input: Two numbers (integers or floats).


Output: The product of the two numbers.
Ans
Practice 3

• Create a program that reads two numbers and


divides the first by the second. Assume that the
second number is non-zero.

• Input: Two numbers (integers or floats).


Output: The quotient of the two numbers.
Ans
Practice 4
• Create a simple calculator that allows the user
to perform all four arithmetic operations
(addition, subtraction, multiplication, and
division) on two numbers, Each operation
should be a separate section in the program.

• Input: Two numbers.


Output: The result of each arithmetic
operation (sum, difference, product, quotient).
Ans
Relational Operators
• Called: Comparison Operators,
• we use them to compare between two items, they
return one of two values true/false.
What if

• We want to check if 5 > 10 and 5 > 20


• in the same statement?
• We need a way to connect two expressions
together.
• Logical operators to the rescue!
Logical Operators

• Logical operators are used in such cases where


we have to combine the evaluation of the
expressions.

• Logical operators operates on boolean


expressions (true and false) and returns
boolean values.
Logical Operators Cont.
&&
 This is known as the “logical AND” operator. It is used to
check whether the condition on both sides of the operator is
true.

||
 This is known as the “Logical OR” operator. It checks whether
either of the condition on both sides is true.

!
 This is known as the “Logical NOT” operator. It just changes
the current boolean value to its opposite.
Example
Compound Assignment
Operator Precedence
Shifting

• Left Shift (<<) multiplies the number by 2^n (where n is the


number of positions shifted).
• 5<<2 = 20

• Right Shift (>>) divides the number by 2^n.


• 20>>2 = 5
Practice

• Suppose that a = 3, b = 4, c = 5, n = 0

• Show the order of evaluating the


following expression and determine the
value of n:
1. n = a-- + b * ++c
2. n = a++ + b / --c
3. n = a + b * --c > c/a
Suppose a = 3, b = 4, c = 5, n = 0

• Solution:
• n = ((a--) + (b * (++c)))
– Decrementing a
– Incrementing c
– Multiplication
– Addition
n = 27
Suppose a = 3, b = 4, c = 5, n = 0

• Solution:
• n = (a++) + (b / (--c))
– Incrementing a
– Decrementing c
– Dividing b and c
– Addition
• n=4
Suppose a = 3, b = 4, c = 5, n = 0

• Solution:
• n = (a + (b * (--c))) > (c/a)
– Decrementing c
– Multiplication of b * c
– Division c/a
– Addition
– Greater than
• n=1
Assignment 1

• Write and run a program that covert the


temperature reading in Fahrenheit into its
equivalent in Celsius using the formula:

C= 5/9 (F-32)
Assignment Answer
Assignment 1

• Create a program that calculates the area of a


rectangle given its length and width.

• Input: Length and width of the rectangle.


Output: Area of the rectangle.
Ans
Assignment 2

• Ask the user for a string representation of a


floating-point number and an integer, then
convert both and perform addition.
Ans.
Thank You
• int x = 5;
• int y = 2;

• int sum = x + y;
• Console.WriteLine("The SUmmation of {0} and {1} = {2}",x,y,result);

• int sub = x - y;
• Console.WriteLine(sub);

• //int x = 5;
• //int y = 2;

• //int sum = x + y;
• //Console.WriteLine("x + y = "+ sum);

• //int sub = x - y;
• //Console.WriteLine("x - y = "+sub);

• //int mul = x * y;
• //Console.WriteLine("x * y ="+ mul);

• //int div =x /y;


• //Console.WriteLine("x / y = "+ div);

• //int mod = x % y;
• //Console.WriteLine("x % y = "+ mod);

• //Console.WriteLine((++x)+y+x+y);

• //____________________________________________________________________________________________________________

• //int x = 10;
• //int y = 2;
• //bool z = false;

• //z = (x > y || x == y);


• //Console.WriteLine("x > y || x == y "+x);
• //z = (x >= y && x == y);
• //Console.WriteLine("x >= y && x == y "+ z);
• //z = (x < y || x != y);
• //Console.WriteLine("(x < y || x != y "+z);
• //z = !(x <= y);
• //Console.WriteLine("!(x <= y) "+ z);

You might also like