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

int main(void)
{
    double r,d,x,y,pi;

    clrscr();
    printf("Input the value of r and degree: ");
    //scanf("%f",&r);
    //scanf("%f",&d);

    r = 12;
    d = 195;
    pi = 3.14;
    x = r * cos(d * pi/180);
    y = r * sin(d * pi/180);

    printf("The polar form is: (%f,%f)", x, y);
    getch();
}

In the 1st case with defined values of r and d the output comes correct but in 2nd case when I give the input the output doesn't match with the original answer. The code worked on codeblocks but isn't working on turbo c++.

what am I doing wrong in turbo c++?

Jens
  • 69,818
  • 15
  • 125
  • 179

2 Answers2

0

Mis-matched format specifier. Use "%lf" with a double *

double r,d,x,y,pi;
...
//scanf("%f",&r);
//scanf("%f",&d);
scanf("%lf",&r);
scanf("%lf",&d);

Better codes checks for success before using r.

if (scanf("%lf",&r) != 1) Handle_Error();

Output is Cartesian coordinates. @Jens. I'd also recommend a '\n'.

// printf("The polar form is: (%f,%f)", x, y);
printf("The Cartesian form is: (%f,%f)\n", x, y);

It appears Turbo-C requires "l" when printing double, so use

printf("The Cartesian form is: (%lf,%lf)\n", x, y);

Little reason to use a low precision pi. Suggest

pi = acos(-1.0);
chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256
  • You'll need `"%lf"` with turbo-c for the printf as well. – Michael Petch Jan 15 '18 at 20:25
  • @MichaelPetch Any reference to support that? Sure it is not optional? – chux - Reinstate Monica Jan 15 '18 at 20:26
  • Personal experience from 30 years ago and the fact I tested it out a couple days ago to see what the OP was seeing. When the `scanf` was changed to `%lf` the results were incorrect until they were also made `%lf`. I'll see if I can find anything in my old Turbo-C reference books about it. – Michael Petch Jan 15 '18 at 20:28
  • @MichaelPetch [Turbo C® Reference Guide Version 2.0](https://ia801902.us.archive.org/5/items/bitsavers_borlandturReferenceGuide1988_19310204/Turbo_C_Version_2.0_Reference_Guide_1988.pdf) – chux - Reinstate Monica Jan 15 '18 at 20:44
-1

In:

//scanf("%f",r);
//scanf("%f",d);

You need to pass the addresses of the variables, &r and &d.

SoronelHaetir
  • 14,104
  • 1
  • 12
  • 23