1

I am novice at programming in C++. I want to write a program using while loop which displays the trigonometric table for sin, cos and Tan. It takes angles in degrees with a difference of 5 and displays the result. This it what I tried,

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

int main()
{
    int num;
    cout<< "Angle   Sin     Cos     Tan"<<endl;
    cout<< "..........................."<<endl;
    num=0;

    while (num<=360)
    {
        cout <<setw(3)<<num<<"    "
            <<setw(3)<<setprecision(3)<<sin(num)<<"    "
            <<setw(3)<<setprecision(3)<<cos(num)<<"    "
            <<setw(5)<<setprecision(3)<<tan(num)<<endl;
        num=num+5;
    }
}

Unfortunately, I could not change radians into degrees in while loop and the display does not look promising even for radians. How can I resolve it ?

crashmstr
  • 28,043
  • 9
  • 61
  • 79
Roshan Shrestha
  • 178
  • 2
  • 10
  • 1
    The back-tick is for in-line formatting only. Use 4 spaces for blocks of code (and make sure to have a consistent and readable indentation for all code). – crashmstr Feb 15 '16 at 17:37

2 Answers2

5

To convert degrees to radiant you have to multiply by pi and to divide by 180.0:

#define M_PI 3.14159265358979323846    

int num = 0;
while (num<=360)
{
    double numRad = num * M_PI/180.0;

    std::cout <<std::setw(3)<<num<<"    "
        <<std::setprecision(3)<<std::fixed
        <<std::setw(6)<< std::sin( numRad ) <<"    "
        <<std::setw(6)<< std::cos( numRad ) <<"    ";
    if ( num != 90 && num != 270 )
        std::cout<<std::setw(6)<< std::tan( numRad ) <<std::endl;
    else
        std::cout<< "infinitely" <<std::endl;

    num=num+5;
}

To use constant M_PI see How to use the PI constant in C++

Community
  • 1
  • 1
Rabbid76
  • 202,892
  • 27
  • 131
  • 174
  • Thanks a lot ,but the diplay still doesnot look that impressive. Can something be done to it. – Roshan Shrestha Feb 15 '16 at 17:42
  • @RoshanShrestha Use `std::fixed`. I edited my answer. – Rabbid76 Feb 15 '16 at 17:55
  • Regarding the linked-to answer: 99.9% of the time, a platform's version of math.h will have `M_PI` defined, but note that it is not part of the standard, and thus there is no guarantee it will be there. This answer does a good job in defining it manually. – AndyG Feb 15 '16 at 17:57
  • Why Tan gives extremely large value at 90 instead of giving infinity or NaN as in fortran.Can we make it print infinity or undefined at 90 for Tan ? – Roshan Shrestha Feb 15 '16 at 18:00
  • @RoshanShrestha The result of *tan(90°)* is infinitely. You can do somthing like `double t = num == 90 ? 0.0 : tan(numRad);`, but then your table is not correct for a value of *90°*. – Rabbid76 Feb 15 '16 at 18:06
  • Ok thanks, I will leave it as it is then. Thanks a lot to you guys for helping me out. :-) – Roshan Shrestha Feb 15 '16 at 18:09
1

To convert degrees to radians, use numRad = M_PI / 180.0 where M_PI should be a constant that holds the value od Pi. If you do not have such a constant defined in a header file, just define it yourself, like #define PI 3.14159265

The functions sin, cos and tan always require arguments in radians.

Frank Puffer
  • 8,135
  • 2
  • 20
  • 45