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

W8L1 in Build Functions

This document provides an overview of common in-built functions in C++ like pow(), sqrt(), min(), max(), swap(), gcd(), toupper(), tolower(), floor(), and ceil(). It includes the purpose of each function, the header files required, and examples demonstrating how to use the functions. Some key points covered are that pow() and sqrt() find exponents and square roots, min() and max() compare values, swap() interchanges variables, and toupper() and tolower() convert case. Floor() rounds down and ceil() rounds up floating point numbers to integers. The document aims to help programmers learn and utilize these essential math, comparison, character and other utility functions in C++.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

W8L1 in Build Functions

This document provides an overview of common in-built functions in C++ like pow(), sqrt(), min(), max(), swap(), gcd(), toupper(), tolower(), floor(), and ceil(). It includes the purpose of each function, the header files required, and examples demonstrating how to use the functions. Some key points covered are that pow() and sqrt() find exponents and square roots, min() and max() compare values, swap() interchanges variables, and toupper() and tolower() convert case. Floor() rounds down and ceil() rounds up floating point numbers to integers. The document aims to help programmers learn and utilize these essential math, comparison, character and other utility functions in C++.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

Fundamentals of Computer

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;

cout << std::min(a, b) << "\n";


OUTPUT:5 return 0;
}
5
// C++ program to demonstrate use of max()
#include <algorithm>
Max() #include <iostream>
using namespace std;
It helps in finding the
maximum between two int main()
values. This function takes {
two values of the same data
type as arguments and int a = 112, b = 123;
returns the value of the
maximum element. // Comparing a and b
cout << std::max(a, b) << "\n";
OUTPUT:
123 // Returns the first one if both the numbers
7 // are same
cout << std::max(7, 7);
return 0;
}
6
// C++ program for illustration
Swap() // of swap() function
#include <iostream>
#include <utility>
using namespace std;
This function is used for
swapping two numbers. It takes int main()
two values of the same data {
type as arguments and swaps int a = 10;
int b = 20;
their value. cout << "Value of a before: " << a << endl;
cout << "Value of b before: " << b << endl;

Compiler Versions: // swap values of the variables


swap(a, b);
#include <algorithm> (until C++11) cout << "Value of a now: " << a << endl;
#include <utility> (since C++11) cout << "Value of b now: " << b << endl;
#include <string_view> (since C++17) return 0;
}
7
Gcd() // CPP program to illustrate
// gcd function of C++ STL
This function is used to find the #include <algorithm>
GCD of two numbers. It takes #include <iostream>
two values of the same data #include<numeric> //for C++17
type as arguments and returns using namespace std;
the GCD of them.
int main()
#include <algorithm> {
#include <numeric> (for C++17)
int a = 6, b = 20;
// int ans = __gcd(a, b);
__gcd(value1, value2); [for C++14] int ans = gcd(a, b); //for C++17
gcd(value1, value2); [for C++17]

cout << "gcd(6, 20) = " << ans << endl;


OUTPUT: gcd(6, 20) = 2 return 0;
}
8
Toupper() // C++ program to illustrate toupper()
method
#include <cctype>
This function is used for #include <iostream>
converting a lowercase
character to uppercase. using namespace std;

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

• Content of these slides are taken from:


• https://www.geeksforgeeks.org/
• https://www.tutorialspoint.com/
• https://www.programiz.com/

16

You might also like