Found 7197 Articles for C++

Pure Function in C++

Tapas Kumar Ghosh
Updated on 02-Jun-2025 14:18:35

3K+ Views

Pure functions always return the same result for the same argument values. They only return the result and there are no extra side effects like argument modification, I/O stream, output generation etc. Following are the examples of pure vs impure functions: Pure Function: sin(), strlen(), sqrt(), max(), pow(), floor() etc. Impure Function: rand(), time(), etc. There are many pure functions in C language such as strlen(), strcmp(), strchr(), strrchr(), strstr(), toupper(), tolower(), abs(), sin(), cos(), sqrt(), exp(), log(), pow(), fabs(), etc. Here we are explaining a few pure functions with ... Read More

iscntrl() function in C++

Arjun Thakur
Updated on 25-Jun-2020 09:22:26

134 Views

The iscntrl() function in C++ checks if a character is a control character or not. This function is defined in ctype.h.The syntax for iscntrl() function is given as follows −int iscntrl ( int ch );Here, ch is the character that needs to be checked.A program that demonstrates iscntrl() function by counting the number of control characters in a string is given as follows −Example Live Demo#include #include using namespace std; int main() {    char str[] = "Coding\tis\tfun";    int i, count = 0;    for(i=0; str[i]!='\0';i++) {       if(iscntrl(str[i]))       count++;    }    cout

C++ Program to Calculate Difference Between Two Time Period

Tapas Kumar Ghosh
Updated on 02-Jun-2025 14:19:25

2K+ Views

There are two time periods provided in the form of hours, minutes and seconds. Then their difference is calculated. For example: Time period 1 = 8:6:2 Time period 2 = 3:9:3 Time Difference is 4:56:59 C++ Program to Find Difference Between Two Times To find the time differences then check whether we need to borrow time, for example, adding 60 seconds if the second time has more seconds. Then just subtract the hours, minutes, and seconds of the second time from the first time to get the result. Example In this example, you will see the time difference of ... Read More

C++ Program to Swap Numbers in Cyclic Order Using Call by Reference

George John
Updated on 25-Jun-2020 09:26:39

1K+ Views

Three numbers can be swapped in cyclic order by passing them to a function cyclicSwapping() using call by reference. This function swaps the numbers in a cyclic fashion.The program to swap numbers in cyclic order using call by reference is given as follows −Example Live Demo#include using namespace std; void cyclicSwapping(int *x, int *y, int *z) {    int temp;    temp = *y;    *y = *x;    *x = *z;    *z = temp; } int main() {    int x, y, z;    cout > y >> z;    cout

Static Data Members in C++

Ankith Reddy
Updated on 25-Jun-2020 09:29:34

24K+ Views

Static data members are class members that are declared using the static keyword. There is only one copy of the static data member in the class, even if there are many class objects. This is because all the objects share the static data member. The static data member is always initialized to zero when the first class object is created.The syntax of the static data members is given as follows −static data_type data_member_name;In the above syntax, static keyword is used. The data_type is the C++ data type such as int, float etc. The data_member_name is the name provided to the ... Read More

isspace() function in C++

Arjun Thakur
Updated on 25-Jun-2020 09:31:06

4K+ Views

The isspace() function is a predefined function in ctype.h. It specifies whether the argument is a whitespace character or not. Some of the whitespace characters are space, horizontal tab, vertical tab etc.A program that implements isspace() function by counting the number of spaces in a string is given as follows −Example Live Demo#include #include using namespace std; int main() {    char str[] = "Coding is fun";    int i, count = 0;    for(i=0; str[i]!='\0';i++) {       if(isspace(str[i]))       count++;    }    cout

C++ Program to Perform Addition Operation Using Bitwise Operators

Tapas Kumar Ghosh
Updated on 30-Apr-2025 18:17:52

2K+ Views

Bitwise operators are used for representing binary integers, where the operator directly performs operations on the individual bits of integer values. To perform an addition operation using bitwise operators, use operators like AND, XOR, and NOT. The OR operator cannot perform addition on its own because, 1 | 1 results in 1, but we need 2 as the output. Therefore, you can use the other three operators to implement the logic of addition. You can see the tabular representation of biwise operators by taking binary bits as 0 and 1. ... Read More

C++ Program to Use rand and srand Functions

Chandu yadav
Updated on 25-Jun-2020 09:06:34

636 Views

Random numbers can be generated in C++ using the rand() function. The srand() function seeds the random number generator that is used by rand().A program that uses rand() and srand() is given as follows −Example Live Demo#include #include #include using namespace std; int main() {    srand(1);    for(int i=0; i

C++ Program to Sort Elements in Lexicographical Order (Dictionary Order)

Tapas Kumar Ghosh
Updated on 21-May-2025 17:28:52

2K+ Views

Lexicographical order denotes the way the words are ordered in a list, based on alphabetical order according to their alphabets. For example: List of words: Harry Adam Sam Lexicographical order of words: Adam Harry Sam Sorting String Based on Lexicographical Order To implement the string in lexicographical order, use the two different iterators: one to point at the current string, and the other to compare it with the next strings in the array. If the first iterator points to a string that is greater than the one pointed to by the second iterator, then we swap ... Read More

C++ Program to Add Complex Numbers by Passing Structure to a Function

Tapas Kumar Ghosh
Updated on 02-May-2025 15:25:14

859 Views

Complex numbers are numbers that are expressed as a+bi, where i is an imaginary number and a and b, are real numbers. Some of the example of complex numbers are 2 + 5i, 3 - 9i, 8 + 2i, etc. How to add complex number? Here, we will explain how to add two complex numbers. Let us assume we have two complex numbers, Z1 and Z2. // a and b are the real and imaginary parts of Z1 Z1 = a + bi // c and d are the real and imaginary parts of Z2 Z2 = c + ... Read More

Advertisements