Home » C++ programming language

cos() function with example in C++

C++ cos() function: Here, we are going to learn about the cos() function with example of cmath header in C++ programming language?
Submitted by IncludeHelp, on April 27, 2019

C++ cos() function

cos() function is a library function of cmath header, it is used to find the cosine of the given number (angle), it accepts a number (x) and returns the cosine of angle x radians.

Syntax of cos() function:

    cos(x);

Parameter(s): x – is the value of an angle in radians whose cosine to be calculated.

Return value: double – it returns double type value that is the cosine of given angle x radians.

Example:

    Input:
    float x = 2.45;
    
    Function call:
    cos(x);    
    
    Output:
    -0.770231

C++ code to demonstrate the example of cos() function

// C++ code to demonstrate the example of 
// cos() function

#include <iostream>
#include <cmath>
using namespace std;

// main() section
int main()
{
    float x;
    
    x = -10.23;
    cout<<"cos("<<x<<"): "<<cos(x)<<endl;

    x = 0;
    cout<<"cos("<<x<<"): "<<cos(x)<<endl;    

    x = 2.45;
    cout<<"cos("<<x<<"): "<<cos(x)<<endl;    
    

    return 0;
}

Output

cos(-10.23): -0.692952
cos(0): 1
cos(2.45): -0.770231

Reference: C++ cos() function




Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.