18 Important functions in C++

Functions:-

/*In this post we are going to learn about some important functions in C++.Hope you will learn something*/

We are going to learn about:-

  1. cin
  2. cout
  3. strlen
  4. atof
  5. atoi
  6. abs
  7. pow
  8. strand/rand
  9. sqrt
  10. floor
  11. ceil
  12. max/min
  13. toupper/tolower
  14. strcmp
  15. strcat
  16. memcpy
  17. getline
  18. isdigit

Functions in C++:-

Cin:-

● cin is used to read input from the standard input device, which is usually the keyboard. It is typically used in conjunction with the extraction operator (>>) to read data from the user.

For example, the following code would prompt the user
to enter their name and then store that name in a variable called name:

std::string name;
std::cout << “Enter your name: “;
std::cin >> name;

Cout:-

● cout is used to write output to the standard output device, which is usually the console. It is typically used in conjunction with the insertion operator (<<) to write data to the console.

For example, the following code would print the value of the
variable name to the console:
Cout:-

std::string name = “John Doe”;
std::cout << “Your name is: ” << name << std::endl;

strlen:-

● strlen is a function that returns the length of a string. The strlen() function
takes a pointer to a string as its argument and returns the number of characters in the string, not including the terminating null character.


For example, the following code uses the strlen() function to print the length of the string “Hello, world!” to the console:

#include <stdio.h>
int main()
{
char str[] = “Hello, world!”;
int len = strlen(str);
printf(“The length of the string is %d\n”, len);
return 0;
}

atof:-

● atof is a function that converts a string to a floating-point number. The atof() function takes a pointer to a string as its argument and returns the floating-point
value represented by the string.

For example, the following code uses the atof() function to convert the string “123.45”
to a floating-point number and then prints the number to the console:

#include <stdio.h>
int main()
{
char str[] = “123.45”;
float f = atof(str);
printf(“The floating-point number is %f\n”, f);
return 0;
}

atoi:-

● atoi is a function that converts a string to an integer. The atoi() function takes a pointer to a string as its argument and returns the integer value represented by
the string.

For example, the following code uses the atoi() function to convert the string “123” to an integer and then prints the number to the console:

#include <stdio.h>
int main()
{
char str[] = “123”;
int i = atoi(str);
printf(“The integer number is %d\n”, i);
return 0;
}

abs:-

● abs is a function that returns the absolute value of a number. The abs() function takes an integer or a floating-point number as its argument and returns the absolute value of the number.

For example, the following code uses the abs() function to print the absolute value of -10 to the console:

#include <stdio.h>
int main()
{
int n = -10;
int abs_n = abs(n);
printf(“The absolute value of %d is %d\n”, n, abs_n);
return 0;
}

pow:-

● pow is a function that raises a number to a power. The pow() function takes two arguments, the base and the exponent, and returns the value of the base raised to the power of the exponent.

For example, the following code uses the pow() function to calculate 2 raised to the power of 3 and then prints the result to the console:

#include <stdio.h>
int main()
{
double x = 2;
double y = pow(x, 3);
printf(“2 raised to the power of 3 is %f\n”, y);
return 0;
}

COMING SOON…

Leave a comment

Design a site like this with WordPress.com
Get started