#include <iostream>
#include <cmath>
using namespace std;
int main()
{
double x = 10.25, result;
result = sqrt(x);
cout << "Square root of " << x << " is " << result << endl;
return 0;
}
The pow() function computes a base number raised to the power of exponent number.
#include <iostream>
#include <cmath>
using namespace std;
int main ()
{
long double base = 4.4, result;
int exponent = -3;
result = pow(base, exponent);
cout << base << "^" << exponent << " = " << result << endl;
// Both arguments int
// pow() returns double in this case
int intBase = -4, intExponent = 6;
double answer;
answer = pow(intBase, intExponent);
cout << intBase << "^" << intExponent << " = " << answer;
return 0;
}
When you run the program, the output will be:
4.4^-3 = 0.0117393 -4^6 = 4096
The abs() function in C++ returns the absolute value of the argument.
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
double x = -87.91, result;
result = abs(x);
cout << "abs(" << x << ") = |" << x << "| = " << result << endl;
return 0;
}
When you run the program, the output will be:abs(-87.91) = |-87.91| = 87.91The strlen() function in C++ returns the length of the given string.
#include <cstring>
#include <iostream>
using namespace std;
int main()
{
char str1[] = "This a string";
char str2[] = "This is another string";
int len1 = strlen(str1);
int len2 = strlen(str2);
cout << "Length of str1 = " << len1 << endl;
cout << "Length of str2 = " << len2 << endl;
if (len1 > len2)
cout << "str1 is longer than str2";
else if (len1 < len2)
cout << "str2 is longer than str1";
else
cout << "str1 and str2 are of equal length";
return 0;
}
When you run the program, the output will be:Length of str1 = 13 Length of str2 = 22 str2 is longer than str1The toupper() function in C++ converts a given character to uppercase.
#include <cctype>
#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
int main()
{
char str[] = "John is from USA.";
cout << "The uppercase version of \"" << str << "\" is " << endl;
for (int i=0; i<strlen(str); i++)
putchar(toupper(str[i]));
return 0;
}
When you run the program, the output will be:The uppercase version of "John is from USA." is JOHN IS FROM USA.The isupper() function in C++ checks if the given character is a uppercase character or not.
#include <cctype>
#include <iostream>
#include <cstring>
using namespace std;
int main()
{
char str[] = "This Program Converts ALL UPPERCASE Characters to LOWERCASE";
for (int i=0; i<strlen(str); i++)
{
if (isupper(str[i]))
/* Converting uppercase characters to lowercase */
str[i] = str[i] + 32;
}
cout << str;
return 0;
}
When you run the program, the output will be:this program converts all uppercase characters to lowercase
No comments:
Post a Comment