W8L1 in Build Functions
W8L1 in Build Functions
Programming
(CS-110)
Course Instructors:
Dr. Momina Moetesum
Ms. Shakeela Bibi
1
In-built Functions
Functions HeaderFiles
pow() <cmath>
sqrt() <cmath>
min() <algorithm>
max() <algorithm>
swap() <utility>
gcd() <numeric>
__gcd() <algorithm>
toupper() <cctype>/<ctype>
tolower() <cctype>/<ctype>
floor() <cmath>
ceil() <cmath> 2
// CPP program to illustrate power function
Pow() #include<iostream>
#include <cmath>
This function helps to find #include<iomanip>
using namespace std;
the value of a number int main()
raised to another number. {
It always takes to values double x = 6.1, y = 4.8;
of double data type as
parameters (Also accepts // Storing the answer in result.
int data type) and the
result is of double data double result = pow(x, y);
type. // Printing the result upto 2 decimal place
OUTPUT: 5882.79 cout<< fixed << setprecision(2) << result << endl;
return 0;
} 3
Sqrt() // CPP Program to demonstrate errors in double sqrt()
#include <cmath>
#include <iostream>
This function helps to find using namespace std;
the square root of any // Driver Code
number. It takes floating int main()
pointer or integer data {
type as an argument. The int x = 24;
double answer;
result is returned after
rounding it according to answer = sqrt(x);
the required data type.
// Printing square root of 24.
cout << answer << endl;
OUTPUT: 4.89898 }
return 0;
4
Min() // C++ program to demonstrate the use of
std::min
#include <algorithm>
This function helps to find
#include <iostream>
the minimum between using namespace std;
two numbers. It takes two
numbers of the same int main()
data type as arguments {
and returns the value of int a = 5;
the minimum. int b = 7;
int main()
int toupper(int ch) {
char ch='m';
OUTPUT: cout<<ch<<endl;
m putchar(toupper(ch));
return 0;
M
}
9
Tolower() // C++ program to illustrate toupper()
method
This function is used for #include <cctype>
converting an uppercase #include <iostream>
character to lowercase. using namespace std;
int main()
{
OUTPUT: char ch='M';
M cout<<ch<<endl;
m putchar(tolower(ch));
return 0;
}
10
Floor() // C++ program to demonstrate floor function
#include <cmath>
#include <iostream>
This function returns the using namespace std;
largest possible integer
value which is less than or // Driver function
equal to a given argument. It int main()
takes a floating number as {
an argument and returns an // Using floor function which returns
integer value. // floor of input value
cout << "Floor is: " << floor(2.3) << "\n";
cout << "Floor is: " << floor(-2.3) << "\n";
OUTPUT:
Floor is: 2 return 0;
Floor is: -3 }
11
// C++ program to demonstrate ceil
Ceil() function
#include <cmath>
This function is just the opposite #include <iostream>
of floor(), It returns the smallest using namespace std;
possible integer value which is
greater than or equal to the
given argument. It takes a // Driver function
floating value as an argument int main()
and returns an integer value. {
// Using ceil function which return
// floor of input value
OUTPUT:
cout << " Ceil is: " << ceil(2.3) << "\n";
Ceil is: 3 cout << " Ceil is: " << ceil(-2.3) << "\n";
Ceil is: -2
return 0;
}
12
13
<iomanip>
14
Further Exploration
• https://www.geeksforgeeks.org/precision-of-floating-point-numbers-
in-c-floor-ceil-trunc-round-and-setprecision/
• https://www.geeksforgeeks.org/ctype-hcctype-library-in-c-c-with-
examples/
15
Acknowledgment
16