m2skills

squareroot c++

Apr 18th, 2017
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.85 KB | None | 0 0
  1. /*program to find out square root of a number*/
  2.  
  3. #include <iostream>
  4.  
  5. using namespace std;
  6. class root
  7. {
  8.     private:
  9.             float square,squareroot,num,error;
  10.     public:
  11.             // this method takes the input from the user
  12.             void getdata()
  13.             {
  14.                 cout<<"\nENTER THE NUMBER WHOSE SQUARE IS TO BE FOUND :";
  15.                 cin>>square;
  16.                 again:
  17.                     cout<<"ENTER THE INITIAL GUESS :";
  18.                     cin>>squareroot;
  19.                     squareroot = squareroot%square; //guess should be smaller than the entered number
  20.                     error=0.000001;
  21.             }
  22.  
  23.             void findroot()
  24.             {
  25.                 do
  26.                 {
  27.                     num=square/squareroot;      //calculating quotient
  28.                     if(((num<squareroot)&&(squareroot-num<error)) || ((num>squareroot)&&(num-squareroot<error)) || (squareroot==num))
  29.                     {
  30.                         cout<<"\nTHE SQUARE ROOT OF "<<square<<" IS : "<<squareroot;
  31.                         break;
  32.                     }
  33.                     else
  34.                     {
  35.                         squareroot=(squareroot+num)/2;
  36.                     }
  37.                 }while(1);
  38.             }
  39. };
  40.  
  41. int main()
  42. {
  43.     root r1;
  44.     int cont=0;
  45.     do
  46.     {
  47.         r1.getdata();
  48.         r1.findroot();
  49.         cout<<"\nDO YOU WANT TO CHECK AGAIN (1/0):";
  50.         cin>>cont;
  51.     }while(cont==1);
  52.     return 0;
  53. }
  54. /*****************************OUTPUT***************************************
  55.  
  56. ENTER THE NUMBER WHOSE SQUARE IS TO BE FOUND :81
  57. ENTER THE INITIAL GUESS :7
  58.  
  59. THE SQUARE ROOT OF 81.000000 IS : 9.000000
  60.  
  61. DO YOU WANT TO ENTER ANOTHER NUMBER (1/0) : 1
  62.  
  63. ENTER THE NUMBER WHOSE SQUARE IS TO BE FOUND :65
  64. ENTER THE INITIAL GUESS :4
  65.  
  66. THE SQUARE ROOT OF 65.000000 IS : 8.062258
  67.  
  68. DO YOU WANT TO ENTER ANOTHER NUMBER (1/0) : 1
  69.  
  70. ENTER THE NUMBER WHOSE SQUARE IS TO BE FOUND :56
  71. ENTER THE INITIAL GUESS :2
  72.  
  73. THE SQUARE ROOT OF 56.000000 IS : 7.483315
  74.  
  75. DO YOU WANT TO ENTER ANOTHER NUMBER (1/0) : 0
  76.  
  77. Process returned 0 (0x0)
  78. Press any key to continue.
  79.  
  80. */
Add Comment
Please, Sign In to add comment