3

I searched the question similar to my problem Similar problem. But my problem is when using Turbo C compiler v3.0. Should I have to do some additional work for math.h file? please help.

int main (void){
    double result, a;
    clrscr();
    printf("Enter a # for square root.\n");
    scanf("%f",&a);
    printf("a = %f\n",a);
    result = sqrt(a);
    printf("a = %f  and square root is %f\n",a, result);
    getch();
    return 0;
    }

The Output is like this:

Enter a # for square root.

64

a = 0.000000

a = 0.000000 and square root is 0.000000

Community
  • 1
  • 1
AbdulAziz
  • 5,868
  • 14
  • 56
  • 77

4 Answers4

4

For scanf(), %f is for float. You need to use %lf for double:

printf("Enter a # for square root.\n");
scanf("%lf",&a);

This is in contrast to printf() where type-promotion allows %f to be used for both float and double.

Mysticial
  • 464,885
  • 45
  • 335
  • 332
  • Ok when I used data type float instead of double as suggested by @Emil Dumbazu, it works fine. Can you please explain why. Thank you – AbdulAziz Jan 29 '12 at 08:02
  • It works on my machine with just that change. Can you elaborate on what's wrong now? – Mysticial Jan 29 '12 at 08:03
  • Its the same problem. I mean it gave me the same result before. But when I changed data type, it work fine. – AbdulAziz Jan 29 '12 at 08:08
  • That's strange, you're using `%lf` right? It works fine in MSVC. Though I don't have Turbo C to try and repro your issue. – Mysticial Jan 29 '12 at 08:10
  • @Mysticial ok I did this. I am really new to programming and to stackoverflow as well :) – AbdulAziz Feb 01 '12 at 20:03
2

Try this :

   scanf("%lf",&a);

or change the variable a to float:

 float a;
Emil Dumbazu
  • 662
  • 4
  • 22
  • 37
2

In addition to using "%lf" as the scanf format, you need to have

#include <stdio.h>
#include <math.h>
#include <conio.h> /* I think */

The last one is for the clrscr() and getch() calls; they're non-standard, but I think they're declared in <conio.h>.

Without the #include <math.h>, the compiler is going to assume that sqrt() returns an int result rather than double.

(An aside: Why do you call clrscr()? What is the benefit of clearing the screen before doing anything else? The getch() call isn't strictly necessary either, but on some systems the default method of running a program results in the window being closed as soon as it terminates.)

Keith Thompson
  • 254,901
  • 44
  • 429
  • 631
  • Thank you for your explanation .Yes you are right, I includes all you have mention. I also includes math.h but still the same result. I use clrscr() usually to get run or get output on a clean screen. Is there any disadvantage for clrscr()? For getch() ,program get close as soon as it terminates, so I use getch(). – AbdulAziz Jan 29 '12 at 10:16
  • @AbdulAziz: It's poor form. Can you imagine if every time you ran, say, `dir`, `copy`, `move`, or `ren`, the system cleared the screen first? Also, if by "program get close as soon as it terminates", you mean that the *window* is closed as soon as it terminates, then run it through a command prompt window instead of double-clicking on some .exe file. – jamesdlin Jan 29 '12 at 10:45
  • @Keith Thank you for your guidance, but I am using Turbo C v3.0. So I have to access with 'RUN' command. – AbdulAziz Jan 31 '12 at 07:57
  • @AbdulAziz: You should be able to execute the program from a command prompt, but that's admittedly less convenient. – Keith Thompson Jan 31 '12 at 08:08
  • @Keith Thompson can you direct me how to execute the program in command prompt, or give me any link explaining it. – AbdulAziz Jan 31 '12 at 08:10
  • @AbdulAziz: It depends on where TurboC places executables. Someone else can probably answer better than I can. What version of Windows are you running? – Keith Thompson Jan 31 '12 at 08:18
  • @KeithThompson i am using windows 7 – AbdulAziz Feb 01 '12 at 19:02
  • @AbdulAziz: Press the Windows key, then click "Command Prompt" at the top of the start menu. `cd` to the directory where TurboC placed your executable (I don't know where that would be). Type the name of the executable to run it. – Keith Thompson Feb 01 '12 at 19:49
0

Declare variable a in float and variable result in double, and use math.h header file.
And use to take input ("%f", &a); for taking input
And then result = sqrt(a);
and print result in "%lf" format
it works perfectly in turbo c

Dada
  • 6,313
  • 7
  • 24
  • 43