The Wayback Machine - https://web.archive.org/web/20240927154409/https://www.geeksforgeeks.org/c-program-to-illustrate-trigonometric-functions/
Open In App

C++ Program to Illustrate Trigonometric functions

Last Updated : 18 May, 2020
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

The math.h header contains methods for performing basic numeric operations such as the elementary exponential, logarithm, square root, and trigonometric functions. In order to use these functions you need to include header file math.h.
Note: All the functions take input in radians and not degrees

Below are the various trigonometric functions that can be used from the math.h header:

  1. sin: This function takes angle (in radians) as an argument and returns its sine value that could be verified using a sine curve.

    Example:




    // C++ program to illustrate
    // sin trigonometric function
      
    #include <iostream>
    #include <math.h>
    using namespace std;
      
    int main()
    {
        double x = 2.3;
        cout << "Sine value of x = 2.3: "
             << sin(x) << endl;
      
        return 0;
    }

    
    

    Output:

    Sine value of x = 2.3: 0.745705
    
  2. cos: This function takes angle (in radians) as an argument and return its cosine value that could be verified using cosine curve.

    Example:




    // C++ program to illustrate
    // cos trigonometric function
      
    #include <iostream>
    #include <math.h>
    using namespace std;
      
    int main()
    {
        double x = 2.3;
      
        cout << "Cosine value of x = 2.3: "
             << cos(x) << endl;
      
        return 0;
    }

    
    

    Output:

    Cosine value of x = 2.3: -0.666276
    
  3. tan: This function takes angle (in radians) as an argument and return its tangent value. This could also be verified using Trigonometry as Tan(x) = Sin(x)/Cos(x).

    Example:




    // C++ program to illustrate
    // tan trigonometric function
      
    #include <iostream>
    #include <math.h>
    using namespace std;
      
    int main()
    {
        double x = 2.3;
        cout << "Tangent value of x = 2.3: "
             << tan(x) << endl;
      
        return 0;
    }

    
    

    Output:

    Tangent value of x = 2.3: -1.11921
    
  4. acos: This function returns the arc cosine of argument. The argument to acos must be in the range -1 to 1; otherwise, a domain error occurs.

    Example:




    // C++ program to illustrate
    // acos trigonometric function
      
    #include <iostream>
    #include <math.h>
    using namespace std;
      
    int main()
    {
        double x = 1.0;
        cout << "Arc Cosine value of x = 1.0: "
             << acos(x) << endl;
      
        return 0;
    }

    
    

    Output:

    Arc Cosine value of x = 1.0: 0
    
  5. asin: This function returns the arcsine of argument. The argument to asin must be in the range -1 to 1; otherwise, a domain error occurs.

    Example:




    // C++ program to illustrate
    // asin trigonometric function
      
    #include <iostream>
    #include <math.h>
    using namespace std;
      
    int main()
    {
        double x = 1.0;
        cout << "Arc Sine value of x = 1.0: "
             << asin(x) << endl;
      
        return 0;
    }

    
    

    Output:

    Arc Sine value of x = 1.0: 1.5708
    
  6. atan: This function returns the arc tangent of arg.

    Example:




    // C++ program to illustrate
    // atan trigonometric function
      
    #include <iostream>
    #include <math.h>
    using namespace std;
      
    int main()
    {
        double x = 1.0;
        cout << "Arc Tangent value of x = 1.0: "
             << atan(x) << endl;
      
        return 0;
    }

    
    

    Output:

    Arc Tangent value of x = 1.0: 0.785398
    
  7. atan2: This function returns the arc tangent of (a)/(b).

    Example:




    // C++ program to illustrate
    // atan2 trigonometric function
      
    #include <iostream>
    #include <math.h>
    using namespace std;
      
    int main()
    {
        double x = 2.3, y = 1.0;
        cout << "Arc Tangent 2 value of x = 2.3 and y = 1.0: "
             << atan2(x, y) << endl;
      
        return 0;
    }

    
    

    Output:

    Arc Tangent 2 value of x = 2.3 and y = 1.0: 1.16067
    
  8. cosh: This function returns the hyperbolic cosine of argument provided. The value of the argument provided must be in radians.

    Example:




    // C++ program to illustrate
    // cosh trigonometric function
      
    #include <iostream>
    #include <math.h>
    using namespace std;
      
    int main()
    {
        double x = 57.3; // in degrees
        cout << "Hyperbolic Cosine of x=57.3: "
             << cosh(x) << endl;
      
        return 0;
    }

    
    

    Output:

    Hyperbolic Cosine of x=57.3: 3.83746e+24
    
  9. tanh: This function returns the hyperbolic tangent of argument provided. The value of argument provided must be in radians.

    Example:




    // C++ program to illustrate
    // tanh trigonometric function
      
    #include <iostream>
    #include <math.h>
    using namespace std;
      
    int main()
    {
        double x = 57.3; // in degrees
        cout << "Hyperbolic Tangent of x=57.3: "
             << tanh(x) << endl;
      
        return 0;
    }

    
    

    Output:

    Hyperbolic Tangent of x=57.3: 1
    

Below are the trigonometric functions all together:




// C++ program to illustrate some of the
// above mentioned trigonometric functions
  
#include <iostream>
#include <math.h>
using namespace std;
  
int main()
{
    double x = 2.3;
    cout << "Sine value of x = 2.3: "
         << sin(x) << endl;
    cout << "Cosine value of x = 2.3: "
         << cos(x) << endl;
    cout << "Tangent value of x = 2.3: "
         << tan(x) << endl;
  
    x = 1.0;
    cout << "Arc Cosine value of x = 1.0: "
         << acos(x) << endl;
    cout << "Arc Sine value of x = 1.0: "
         << asin(x) << endl;
    cout << "Arc Tangent value of x = 1.0: "
         << atan(x) << endl;
  
    x = 57.3; // in degrees
    cout << "Hyperbolic Cosine of x=57.3: "
         << cosh(x) << endl;
    cout << "Hyperbolic tangent of x=57.3: "
         << tanh(x) << endl;
  
    return 0;
}


Output:

Sine value of x = 2.3: 0.745705
Cosine value of x = 2.3: -0.666276
Tangent value of x = 2.3: -1.11921
Arc Cosine value of x = 1.0: 0
Arc Sine value of x = 1.0: 1.5708
Arc Tangent value of x = 1.0: 0.785398
Hyperbolic Cosine of x=57.3: 3.83746e+24
Hyperbolic tangent of x=57.3: 1


Similar Reads

Illustrate the difference in peak memory consumption between DFS and BFS
To understand this let's take a binary tree If we conduct BFS in this tree: In level 0 there is one node in the memory In level 1 there are two nodes in the memory In level 2 there are four nodes in the memory In level 3 there are eight nodes in the memory But in the case of DFS in this tree, you'll never have more than 4 nodes in memory The differ
1 min read
Hash Functions and Types of Hash functions
Hash functions are a fundamental concept in computer science and play a crucial role in various applications such as data storage, retrieval, and cryptography. In data structures and algorithms (DSA), hash functions are primarily used in hash tables, which are essential for efficient data management. This article delves into the intricacies of hash
4 min read
How to write C functions that modify head pointer of a Linked List?
Consider simple representation (without any dummy node) of Linked List. Functions that operate on such Linked lists can be divided into two categories: 1) Functions that do not modify the head pointer: Examples of such functions include, printing a linked list, updating data members of nodes like adding given a value to all nodes, or some other ope
4 min read
Virtual Functions and Runtime Polymorphism in C++
A virtual function is a member function that is declared in the base class using the keyword virtual and is re-defined (Overridden) in the derived class. It tells the compiler to perform late binding where the compiler matches the object with the right called function and executes it during the runtime. This technique falls under Runtime Polymorphi
8 min read
std::stol() and std::stoll() functions in C++
std::stol(): This function converts the string, provided as an argument in the function call, to long int. It parses str interpreting its content as an integral number of the specified base, which is returned as a value of type long int. Syntax: long int stol (const string& str, size_t* idx = 0, int base = 10) Parameters: The function accepts t
4 min read
Mathematics | Generating Functions - Set 2
Prerequisite - Generating Functions-Introduction and Prerequisites In Set 1 we came to know basics about Generating Functions. Now we will discuss more details on Generating Functions and its applications. Exponential Generating Functions - Let [Tex]h_0, h_1, h_2, ........., h_n, ...... [/Tex]e a sequence. Then its exponential generating function,
3 min read
C++ Mathematical Functions
C++ being a superset of C, supports a large number of useful mathematical functions. These functions are available in standard C++ to support various mathematical calculations. Instead of focusing on implementation, these functions can be directly used to simplify code and programs. In order to use these functions you need to include a header file-
5 min read
C String Functions
The C string functions are built-in functions that can be used for various operations and manipulations on strings. These string functions can be used to perform tasks such as string copy, concatenation, comparison, length, etc. The <string.h> header file contains these string functions. In this article, we will discuss some of the most commo
8 min read
Asynchronous Functions and the Node.js Event Loop
Asynchronous Functions Everyone knows JavaScript is asynchronous in nature and so is the Node. The fundamental principle behind Node is that an application is executed on a single thread or process and the events are thus handled asynchronously. If we consider any typical web server like Apache, it requires separate threads for each process until t
3 min read
What are Hash Functions and How to choose a good Hash Function?
Prerequisite: Hashing | Set 1 (Introduction) What is a Hash Function? A function that converts a given big phone number to a small practical integer value. The mapped integer value is used as an index in the hash table. In simple terms, a hash function maps a big number or string to a small integer that can be used as the index in the hash table. W
5 min read
Methods vs. Functions in C++ with Examples
A method is a procedure or function in OOPs Concepts. Whereas, a function is a group of reusable code which can be used anywhere in the program. This helps the need for writing the same code again and again. It helps programmers in writing modular codes. Methods: A method also works the same as that of function. A method is defined inside a class.
3 min read
Check if two strings are same or not without using library functions
Given two strings S1 and S2, the task is to check whether they are the same or not without using string library functions. Examples: Input: S1 = ”GeeksForGeeks”, S2 = ”GeeksForGeeks”Output: TrueExplanation:S1 and S2 are the same strings Input: S1 = ”GeeksForGeeks”, S2 = ”GeeksforGeeks”Output: False Approach: Follow the steps below to solve the prob
5 min read
Immediate Functions in C++
In this article, we will discuss the immediate function used in C++. Immediate Function: In C++20, an immediate function is a function where every call to the function either directly or indirectly produces a compile-time constant expression.These functions are declared by using a consteval keyword before their return type. Below given some importa
4 min read
Sum of Euler Totient Functions obtained for each divisor of N
Given a positive integer N, the task is to find the sum of the Euler Totient Function for all the divisor of the given number N. Examples: Input: N = 3Output: 3Explanation:Divisors of 3 are {1, 3}. The Euler totient function for the values 1 and 3 are 1 and 2 respectively.Therefore, the required sum is 1 + 2 = 3. Input: N = 6Output: 6 Naive Approac
3 min read
Variadic functions in C
Variadic functions are functions that can take a variable number of arguments. In C programming, a variadic function adds flexibility to the program. It takes one fixed argument and then any number of arguments can be passed. The variadic function consists of at least one fixed variable and then an ellipsis(...) as the last parameter. Syntax: int f
3 min read
Top 10 Most Used Inbuilt C++ functions for Competitive Programming
In this article, we will discuss about the 10 most used inbuilt functions of C++ which will help you to save time and make code concise as well during competitive programming. List of top 10 inbuilt functions in C++ pow()sqrt()min()max()swap()gcd()toupper()tolower()floor()ceil()1. pow( ) This function helps to find the value of a number raised to a
7 min read
C++ Functions - Pass By Reference
Several ways exist in which data (or variables) could be sent as an argument to a function. Two of the common ones are Passing by Value and Passing by Reference. Passing by reference allows a function to modify a variable without creating a copy. We have to declare reference variables. The memory location of the passed variable and parameter is the
5 min read
Passing Pointers to Functions In C++
Prerequisites: Pointers in C++Functions in C++ Passing Pointers to functions means declaring the function parameter as a pointer, at the function calling passing the address of the variable and that address will be stored by a parameter that is declared as a pointer. To change the value of any variable in the function we have to pass the address of
4 min read
C++ - Difference Between Functors and Functions
In C++, we have multiple options to operate over the data like in the case where we can use functions and functors. Although both seem to be similar in a few ways but have multiple differences between them. Let's check the differences between functions and functors in C++. What are functions? Functions are pieces of code that can be called from any
3 min read
Useful Inbuilt Functions in C++
In-built functions in C++ are those functions that are part of C++ standard libraries. The purpose of inbuilt functions is to provide common and essential functionality that is frequently required in the programming. In this article, we will look at some of the commonly used inbuilt functions in C++. 1. sqrt() FunctionThe sqrt() function is used to
7 min read
Important functions of STL Components in C++
C/C++ Code // C++ code #include <iostream> #include <utility> using namespace std; int main() { // Declaring the PAIR1 of int and char // IF pair is not initialized then , // default value of int/double is 0 and // for string/char it is NULL pair<int, char> PAIR1; cout << PAIR1.first << " "; // NULL value the
15+ min read
Higher Order Functions in C++
Higher-order functions are functions that take functions as an argument. It is used in functional languages which is not used in C++ are, although this is slowly changing since C++11 gave us lambdas and 'std::function'... and frequently people don't realize that 'std::function' is not a tool that fills all use cases. In this article, we will explai
3 min read
Explain some string functions of PHP
In the programming world, a string is considered a data type, which in general is a sequence of multiple characters that can contain whitespaces, numbers, characters, and special symbols. For example, "Hello World!", "ID-34#90" etc. PHP also allows single quotes(' ') for defining a string. Every programming language provides some in-built functions
7 min read
Pass Array to Functions in C++
In C++, a collection of elements stored in contiguous memory locations and having the same data type is called an array. Passing arrays to functions is done to perform various operations on array elements without messing up with the main code. In C++, an array can be passed in a function using a pointer or reference. Understanding the different app
5 min read
Can Virtual Functions be Inlined in C++?
Virtual functions are member functions that are declared in the base class using the keyword virtual and can be overridden by the derived class. They are used to achieve Runtime polymorphism or say late binding or dynamic binding. Inline functions are used to replace the function calling location with the definition of the inline function at compil
2 min read
Const member functions in C++
Constant member functions are those functions that are denied permission to change the values of the data members of their class. To make a member function constant, the keyword const is appended to the function prototype and also to the function definition header. Like member functions and member function arguments, the objects of a class can also
6 min read
Inline Functions in C++
C++ provides inline functions to reduce the function call overhead. An inline function is a function that is expanded in line when it is called. When the inline function is called whole code of the inline function gets inserted or substituted at the point of the inline function call. This substitution is performed by the C++ compiler at compile tim
9 min read
Pass Array to Functions in C
In C, the whole array cannot be passed as an argument to a function. However, you can pass a pointer to an array without an index by specifying the array's name. Arrays in C are always passed to the function as pointers pointing to the first element of the array. Syntax In C, we have three ways to pass an array as a parameter to the function. In th
6 min read
Macros vs Functions
A macro is a name given to a block of C statements as a pre-processor directive. Being a pre-processor, the block of code is communicated to the compiler before entering into the actual coding (main () function). A macro is defined with the pre-processor directive. Macros are pre-processed which means that all the macros would be processed before y
4 min read
Return From Void Functions in C++
Void functions are known as Non-Value Returning functions. They are "void" due to the fact that they are not supposed to return values. True, but not completely. We cannot return values but there is something we can surely return from void functions. Void functions do not have a return type, but they can do return values. Some of the cases are list
2 min read
Practice Tags :