In this post, I’ll show you how to calculate square root of a number with the different programming languages with out using square method or function.
Pseudocode : Calculate the Square Root of a Number
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
BEGIN NUMBER root=1, counter=0,num OUTPUT "Enter a number for calculate the root" INPUT num WHILE sayac < sayi+1 THEN i=i+1 root=(num/root+root)/2 END WHILE OUTPUT root END |
Java Code: The following code shows how to get the square root without using the function in Java.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
package javaexamples; import java.util.Scanner; public class JavaExamples { public static void main(String[] args) { Scanner reader = new Scanner(System.in); System.out.print("Enter Number: "); double number = reader.nextDouble(); double result; double squareRoot = number / 2; do { result = squareRoot; squareRoot = (result + (number / result)) / 2; } while ((result - squareRoot) != 0); System.out.println("Reult:"+squareRoot); } } |
C# Code: The following code shows how to get the square root without using the function in C#.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
class Program { static void Main(string[] args) { Console.Write("Enter a number:"); double number = Convert.ToDouble(Console.ReadLine()); if (number > 0) { double root = number / 3; int i; for (i = 0; i < 32; i++) root = (root + number / root) / 2; Console.WriteLine("Square root:{0}", root); } else { Console.WriteLine(Double.NaN); } Console.ReadLine(); } } |
C Code: The following code shows how to get the square root without using the function in C.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
#include <stdio.h> #include <math.h> main() { float number; printf("Enter a Number :"); scanf("%f",&number); float result; float squareRoot = number / 2.0; do { result = squareRoot; squareRoot = (result + (number / result)) / 2.0; } while ((result - squareRoot) != 0); printf("%.2f",squareRoot); } |
Output:
JavaScript Code: The following code shows how to get the square root without using the function in JavaScript.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
<div id="result"></div> <input type="text" id="number" > <script> var number=document.getElementById("number"); var result=document.getElementById("result"); number.oninput=function(){ result.innerHTML= squareRoot(this.value); } /*create custom square root function*/ function squareRoot(number){ var square = 1,i=0; while(true) { i = i + 1; square = (number / square + square) / 2; if (i == number + 1) { break; } } return square; } </script> |
PHP Code: The following code shows how to get the square root without using the function in PHP.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
<?php /*create custom function*/ function squareRoot($number){ if ($number > 0) { $root = $number / 3; for ($i = 0; $i < 32; $i++) $root = ($root + $number / $root) / 2; return $root; } else { return NAN; } } //output echo squareRoot(4) . "<br>"; echo squareRoot(25) . "<br>"; echo squareRoot("Mark") . "<br>"; |