Calculate Square Root of a Number in C/C++

Below is the source code of C and C++ to calculate the square root of number input by the user. The <math.h> library is used for sqrt ( ) function.

C Source Code

   1:  #include <stdio.h>
   2:  #include <conio.h>
   3:  #include <math.h>
   4:  void main ()
   5:  {
   6:   
   7:  clrscr ();
   8:  float num,sq;
   9:  printf ("Enter the number to calculate square root : ");
  10:  scanf ("%f",&num);
  11:  sq = sqrt (num);
  12:  printf ("\n\nThe Square root of %.1f is %.2f" , num,sq);
  13:  getch ();
  14:  }

C++ Source Code

   1:  #include <iostream.h>
   2:  #include <conio.h>
   3:  #include <math.h>
   4:  void main ()
   5:  {
   6:  clrscr ();
   7:  float num,sq;
   8:  cout<<"Enter the number to calculate square root : ";
   9:  cin>>num;
  10:  sq = sqrt (num);
  11:  cout<<endl<<endl<<"The Square root of "<<num<<" is " <<sq;
  12:  getch ();
  13:  }

Output

image

Be Sociable, Share!

One response to Calculate Square Root of a Number in C/C++

Leave a Reply