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

Lecture3

The document outlines a C++ programming course focused on mathematical functions and conditional statements, intended for 2nd-year students at Ninevah University. It includes examples of using functions from the math.h library, such as sin, pow, sqrt, log, exp, and abs, along with explanations of one-way and two-way selection statements. Students are required to write at least five functions from the math.h library with their syntax and purpose, and the document provides various examples of conditional statements in C++.

Uploaded by

Stive Brack
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Lecture3

The document outlines a C++ programming course focused on mathematical functions and conditional statements, intended for 2nd-year students at Ninevah University. It includes examples of using functions from the math.h library, such as sin, pow, sqrt, log, exp, and abs, along with explanations of one-way and two-way selection statements. Students are required to write at least five functions from the math.h library with their syntax and purpose, and the document provides various examples of conditional statements in C++.

Uploaded by

Stive Brack
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

C++ Programming

Ninevah University
College of Electronics Engineering
Department of Electronic Engineering
MEDICAL INSTRUMENTATION

2nd Year
2024 – 2025
8:30-10:30

Lecturer
Prof Dr. Qais Thanon
Lecture #3

All the lectures of this course will upload at the


Google classroom

10/6/2024
Mathematical Functions
Mathematical calculations can be done in C++ programming language
using the mathematical functions which are included in math.h
library.
Let’s learn each of them one by one :−

Calling syntax
double x = sin(ang);
#include <iostream.h> #include <iostream.h>
#include <math.h> #include <math.h>
void main(){ void main(){
double x = 45.3, y; double x = 45.3, y;
y = tan(x); y = tan(x * M_PI/180.0);
cout << y; cout << y;
} }
The angle should be in RAD
2
10/6/2024
Mathematical Functions
Power
The pow function is used to calculate the power of the base raised to the
power of exponent.
Calling syntax
double y = pow(a, n); y = an

#include <iostream.h>
#include <math.h>
void main(){ y = 2.85
double x = 2.8, y;
y = pow(x, 5); y = 172.10368
cout << y;
}

n m 7
y = a y = 2.85

3
10/6/2024
Mathematical Functions
Sqrt ( square root)
sqrt function in C++ returns the square root of the double
integer inside the parameter list.
Calling syntax double y = sqrt(x);
Log The logarithm function is used to find the natural log of
the given number.
Calling syntax double x = log(n); x = log(n)

exp The exponential function is used to returns the (Euler’s


number) e (or 2.71828) raised to the given argument.
Calling syntax double x = exp(n); x = e(n)

abs The abs function returns the absolute value of the integer
value.
Calling syntax int x = abs(n); x = 𝒏

4
10/6/2024
Each student should
write, at least, five
functions from
"math.h" with the
syntax and purpose
of each function

5
10/6/2024
Conditional Statements
One-Way (if) Selection
if Selection statements:

if (expression) ;

{statement;}
statement;

Relational operators
if (A == 5)
==
if (A != B)
!=
>
if (A > B)
if (A >= B)
>=
<
if (A < 50)

<= if (A <= 50)


6
10/6/2024
Ex: The following code fragment prints if (x == 100)
x is 100 only if the value stored in the x cout << "x is 100";
variable is indeed 100:
if (x == 100)
If we want more than a single statement {
to be executed in case that the cout << "x is ";
condition is true we can specify a block cout << x;
using braces { }:
}

If there are more than one relational operators logical operators should used.
#include <iostream.h>
void main () {
Ex: Write a C++ program to int A,B;
enter two Boolean numbers then, cin >>A ;
cin >>B ;
print phrase "A and B“ if A and if ((A==1)&&(B==1))
B equal to 1, or print phrase "A Or {cout << "A And B"<<'\n';}
B" if A equal to 1 and B equal to if ((A==1)||(B==0))
{cout << "A or B"<<'\n';}
0.
} &&
7
10/6/2024
if-else Selection statements:
Two-Way (if…else) Selection

if (expression)
statement1;
else
statement2;

Ex: The following code fragment prints x is


100 only if the value stored in the x variable
is indeed 100 , but if it has not –and only if
not- it prints out x is not 100.

if (x == 100)
cout << "x is 100";
else
cout << "x is not 100";
8
10/6/2024
Nested if-else Selection statements:
if (Condition1){

if(Condition2){
Statement1;
}
else {
Statement2;
}
}
else {
if(Condition3)
{
Statement3;
}
else
{
Statement4;
}
} 9
10/6/2024

You might also like