Download as pdf or txt
Download as pdf or txt
You are on page 1of 34

Military Institute of Science and

Technology
Department of Naval Architecture and Marine Engineering

Lecture – 04
Topics- Arithmetic Operators and Expressions,
Decisions & For Loop

Course Code: NAME 430


Course Title: Application of Computer Programming for
Optimization of Ship Design
Arithmetic Operators
• Operators are symbols that helps us to perform specific
mathematical and logical computations on operands. In other
words we can say that an operator operates the operands.
For example, consider the below statement:
• c = a + b; Here, ‘+’ is the operator known as addition operator
and ‘a’ and ‘b’ are operands. The addition operator tells the
compiler to add both of the operands ‘a’ and ‘b’. C/C++ has
many built-in operator types and they can be classified as:
• Arithmetic Operators: These are the operators used to perform
arithmetic/mathematical operations on operands. Examples: (+,
-, *, /, %,++,–).
• Arithmetic operator are of two types:
• Unary Operators: Operators that operates or works with a single
operand are unary operators. For example: (++ , – –)
• Binary Operators: Operators that operates or works with two
operands are binary operators .For example: (+ , – , * , /)
Arithmetic Operators
• There are 5 binary arithmetic operators. Binary operators are
operators that take a left and right operand.

Operator Symbol Form Operation


Addition + x+y x plus y
Subtraction - x-y x minus y
Multiplication * x*y x multiplied by y
Division / x/y x divided by y
Modulus The remainder of x
% x%y
(Remainder) divided by y
Operator Precedence
Operator precedence determines which operator is performed
first in an expression with more than one operators with different
precedence. For example 10 + 20 * 30 is calculated as 10 + (20 *
30) and not as (10 + 20) * 30.

Associativity is used when two operators of same precedence


appear in an expression. Associativity can be either Left to Right
or Right to Left. For example ‘*’ and ‘/’ have same precedence
and their associativity is Left to Right, so the expression “100 /
10 * 10” is treated as “(100 / 10) * 10”.

Precedence and Associativity are two characteristics of


operators that determine the evaluation order of subexpressions
in absence of brackets.
Define Constant Values
There are two ways in C++ to define a constant:-

➢ Using #define
Example:
# include <iostream>
#define PI 3.14159

➢ Using const keyword to declare constants


const variable type identifier = value;
Example:
const int length = 10;
const float PI = 3.14159;
Arithmetic Operators
Class Task 1

Write a program in C++ that will take your height


in feet and inches (ex. 5’ 10’’) and convert it to
metres.
[1 metre = 3.281 feet]
Assignment Operators
Operator Form Meaning
= a=b
+= a+=b a = a+b
-= a-=b a = a-b
*= a*=b a = a*b
/= a/=b a = a/b
%= a%=b a=a%b
Assignment Operators
Mathematical Functions
C++ standard library header file <cmath> contains function
prototypes for math library functions.
Mathematical Functions
Class Task 2

Billy is standing 150m away from a wind turbine.


His eye level is 2m from the ground. If his angle of
elevation is 25 ͦ to the top of the turbine,
determine the height of the wind turbine using a
C++ program.
#include <iostream>
#include <cmath>
#define pi 3.14159
using namespace std;

int main(){
float base, x, angle, el, height;

cout << "Enter the distance from WT: ";


cin >> base;

Solve >> cout <<"Enter the eye level from ground: ";
cin >> el;

cout << "Enter the angle of elevation: ";


cin >> angle;

x = base*tan(angle* pi/180); //value of tan()


should be converted in radians
height = x+el;

cout << "The height of the wind turbine is " <<


height << "m";
}
Relational Operators
Two expressions can be compared using relational and equality
operators. For example, to know if two values are equal or if one
is greater than the other.

The result of such an operation is either true or false.

Operator Description
== Equal to
!= Not equal to
< Less than
> Greater than
<= Less than or equal to
>= Greater than or equal to
Unary Arithmetic Operator
• Increment: The ‘++’ operator is used to increment the value of an
integer. When placed before the variable name (also called pre-
increment operator), its value is incremented instantly. For example,
++x.
And when it is placed after the variable name (also called post-
increment operator), its value is preserved temporarily until the
execution of this statement and it gets updated before the execution
of the next statement. For example, x++.
• Decrement: The ‘– –‘ operator is used to decrement the value of an
integer. When placed before the variable name (also called pre-
decrement operator), its value is decremented instantly. For example,
– –x.
And when it is placed after the variable name (also called post-
decrement operator), its value is preserved temporarily until the
execution of this statement and it gets updated before the execution
of the next statement. For example, x– –.
Increment Operator

count = count +1

• count += 1

++count
Prefix and Postfix
Increment Prefix ++a Adds 1 to x =10, y=10
the y=++x
y=11, x=11
existing
Postfix a++ value of its x =10, y=10
operand y=x++
y=10, x=11

Decremen Prefix --a Subtracts x =10, y=10


t 1 from the y=--x
y=9, x=9
existing
value if its
Postfix a-- x =10, y=10
operand y=x--
y=10, x=9
Prefix Increment Operator
The prefix increment operator adds one to its
operand. This incremented value is used in the
expression to get the result of the expression.
Postfix Increment Operator
With postfix increment operator the original,
unincremented value of the operand is used in the
expression to get the value of the expression.
Following the completion of the statement, the
increment takes place.
Increment Operator
Write the output of the following program:
The for Loop
The for Loop
Class Task 3
Finding power of any number (Both the number and the
power should be user input)
Solve >>
Class Task 4
Display multiplication table of any integer up to 10
Solve >>
#include <iostream>
using namespace std;

int main()
{
int n;

cout << "Enter a positive integer: ";


cin >> n;

for (int i = 1; i <= 10; ++i) {


cout << n << " * " << i << " = " << n * i<< endl;
}

return 0;
}
Class Task 5
Checking a number prime or not
Solve >>
Class Task 6
Display factors of a number
Solve >>
Class Task 7
Display Fibonacci Series
Solve >>
Class Task 8
Check a number Palindrome or not
Solve >>
Assignment
1. Display prime numbers between two intervals
2. Display first 10 odd/even natural numbers and their sum
3. Write a program in C++ to find the complex roots of the quadratic equation
4x2 - 2x + 3 = 0
4. Write a program in C++ to find all the numbers between 200 to 300 that are
divisible by 8 and find their sum.
5.

You might also like