0

this is the following code:

#include <stdio.h>
#include <math.h>  //to use 'sin()' function

int main() {
    double i = 0;

    //printing the values of sine table 
    printf("SINE TABLE :\n\n");
    while (i <= 90) {
        printf("sin(%.0lf)=%lf\n", i, sin(i * 3.1415 / 180.0));
        i += 15;
    }

    double angle_degree = 90;  
    //printing the values of cosine table 
    printf("COSINE TABLE :\n\n");
    while (angle_degree >= 0) {
        printf("cos(%.0lf)=%lf\n", angle_degree, cos(angle_degree * 3.1415 / 180.0));
        i -= 15;
    }
    return 0;
}

I am getting the correct value for some code i.e It printing the sine table correctly.

SINE TABLE :

sin(0)=0.000000  
sin(15)=0.258819 
sin(30)=0.500000
sin(45)=0.707107
sin(60)=0.866025
sin(75)=0.965926
sin(90)=1.000000

but for cosine table the loop getting the value of cos(90)=0.0000

can you please tell me what is wrong in my program , it will help me to print the correct cosine table....?

chqrlie
  • 131,814
  • 10
  • 121
  • 189
AVI RAJ
  • 55
  • 1
  • 11
  • In the second while loop, `i-=15;` should be `angle_degree-=15;`. – JASLP doesn't support the IES Sep 01 '21 at 06:45
  • 2
    Unrelated: `3.1415` is a poor approximation for π. If you're on a POSIX system prefer `M_PI` (defined in ``) or, in plain Standard C, use `#define PI acos(-1)` ... or `3.1416` or `3.14159265358979`... – pmg Sep 01 '21 at 06:59

2 Answers2

2

Your program runs an infinite loop because you decrement i instead of angle_degree.

Also note that your approximation of π is inaccurate. You should use the constant M_PI defined on POSIX systems or a more precise approximation.

Here is a modified version:

#include <math.h>
#include <stdio.h>

#ifndef M_PI
#define M_PI 3.14159265358979323846264338327950288
#endif

int main() {
    double i = 0;

    //printing the values of sine table 
    printf("SINE TABLE:\n\n");
    while (i <= 90) {
        printf("sin(%.0f)=%f\n", i, sin(i * M_PI / 180.0));
        i += 15;
    }

    double angle_degree = 90;  
    //printing the values of cosine table 
    printf("COSINE TABLE:\n\n");
    while (angle_degree >= 0) {
        printf("cos(%.0f)=%f\n", angle_degree, cos(angle_degree * M_PI / 180.0));
        angle_degree -= 15;
    }
    return 0;
}
chqrlie
  • 131,814
  • 10
  • 121
  • 189
  • I recommend use M_PI instead 3.1415 like that: printf("sin(%.0lf)=%lf\n", i, sin(i * M_PI / 180.0)); – declonter Sep 01 '21 at 07:08
  • 1
    @declonter: indeed `3.1415` is quite inaccurate. Note however that `M_PI` is not part of the C Standard, but it is widely available as it is standardized in POSIX. – chqrlie Sep 01 '21 at 12:06
1

Because you are using angle_degree not i.

You can also use for loop to iterate this, you'll less likely to do this kind of mistake.

#include <stdio.h>
#include <math.h>

int main()
{
    for(double i=0;i<=90;i+=15){
        printf("sin(%.0lf)=%lf\n", i, sin(i * M_PI  / 180.0));
    }

    for(double j=90;j>=0;j-=15){
        printf("cos(%.0lf)=%lf\n", j, cos(j * M_PI  / 180.0));
    }
    
    return 0;
}

You can also use M_PI which is more accurate than 3.1415. Because when you calculate cos(90°) using 3.1415, you will get 0.000046

  cos(90*M_PI/180)=0;
  cos(90*3.1415/180)=0.000046

You can look base on this answer https://stackoverflow.com/a/9912169/5215171