Advertisement
kdaud

Used for loop in breaking down the Problem

Feb 25th, 2019
206
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.16 KB | None | 0 0
  1. package LeapYear;
  2.  
  3. import java.util.Scanner;
  4.  
  5. @interface KakumiriziDaud
  6. {
  7.     String name() default " ";
  8.     String email () default " ";
  9. }
  10. @KakumiriziDaud(name = "Kakumirizi Daud", email = "dkakumirizii@gmail.com")
  11. public class Sqrt {
  12.     public void getTheSqrt( int n )
  13.     {
  14.         // double a = (eventually the main method will plug values into a)
  15.         double a = (double) n;
  16.         double x = 1;
  17.  
  18.         // For loop to get the square root value of the entered number.
  19.         for( int i = 0; i < n; i++)
  20.         {
  21.             x = 0.5 * ( x+a / x );
  22.         }
  23.  
  24.         System.out.println("The sqrt of " + a + " is " + x);
  25.         System.out.println();
  26.         System.out.println("Coders never Quit! Regardless weather the codes are running or not");
  27.     }
  28.  
  29.     // Begin method main..
  30.     public static void main(String[] args) {
  31.         // Creating an object from "Scanner" to input data.
  32.         Scanner Sa = new Scanner(System.in);
  33.         Sqrt S = new Sqrt();
  34.  
  35.         //Asking the user to input the number we want to find its square root.
  36.         System.out.println("Enter a positive number : ");
  37.         int n = Sa.nextInt();
  38.  
  39.         // Calling the method that'll calculate the square root of the given number.
  40.         S.getTheSqrt( n );
  41.  
  42.     } // End method main
  43.  
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement