Q. Write An Algorithm and Program to Find Square root of a Number.
Here you will find an Algorithm and Program to Find Square root of an Number.
Square root : In mathematics, a square root of the number x is a number y such that y^2 = x; In other words, a number y whose square is x. For example, 4 and −4 are square root of 16.
For Example :-
Input : Enter the number Num: 16
Output : 4
Explanation : As we can see that input integer number : Num = 16. Output will be the square root of given number : 4.
Algorithm to Find Square Root of a Number
\\Algorithm to Find Square root of an number START Step 1: [ Take Input ] Read: Number Num Step 2: Create a variable (counter) i = 1. Step 3: Run a loop until i*i <= Num , where Num is the given number. Increment i by 1 Step 4: If number is perfect square then return the output otherwise return the floor of the square root of the number is i – 1. END
Program to Find Square Root of a Number
//C Program to Find Square root of an integer #include <stdio.h> int main() { // Enter the number int num=15; // Take care of some base cases, // i.e when the given number is 0 or 1. if (num == 0 || num == 1) printf("Square root of given number %d is %d",num, num); else { // Staring from 1, try all numbers until // i*i is greater than or equal to Num. int i = 1, temp = 1; while (temp <= num) { i++; temp = i * i; } printf("Square root of given number %d is %d",num, i-1); } }
//C++ Program to Find Square root of an integer #include <iostream> using namespace std; int main() { // Enter the number int num=15; // Take care of some base cases, // i.e when the given number is 0 or 1. if (num == 0 || num == 1) cout<<"Square root of given number "<<num<<" is "<<num; else { // Staring from 1, try all numbers until // i*i is greater than or equal to num. int i = 1, temp = 1; while (temp <= num) { i++; temp = i * i; } cout<<"Square root of given number "<<num<<" is "<<i-1; } }
//Java Program to Find Square root of an integer public class LFC { public static void main(String[] args) { // Enter the number int num=15; // Take care of some base cases, // i.e when the given number is 0 or 1. if (num == 0 || num == 1) System.out.print("Square root of given number "+num+" is "+num); else{ // Staring from 1, try all numbers until // i*i is greater than or equal to num. int i = 1, temp = 1; while (temp <= num) { i++; temp = i * i; } i=i-1; System.out.print("Square root of given number "+num+" is "+i); } } }
#Python Program to Find Square root of an integer # Enter the number int num=15; # Take care of some base cases, # i.e when the given number is 0 or 1. if (num == 0 or num == 1): print("Square root of given number", num, " is ", num) else: # Staring from 1, try all numbers until # i*i is greater than or equal to num. i = 1; temp = 1 while (temp <= num): i += 1 temp = i * i print("Square root of given number", num, " is ", i-1)
//C# Program to Find Square root of an integer using System; class LFC { static void Main() { // Enter the number int num=15; // Take care of some base cases, // i.e when the given number is 0 or 1. if (num == 0 || num == 1) Console.WriteLine("Square root of given number "+num+" is "+num); else { // Staring from 1, try all // numbers until i*i is // greater than or equal to num. int i = 1, temp = 1; while (temp <= num) { i++; temp = i * i; } i=i-1; Console.WriteLine("Square root of given number "+num+" is "+i); } } }
//PHP Program to Find Square root of an integer <?php // Enter the number $num=15; // Take care of some base cases, // i.e when the given number is 0 or 1. if ($num == 0 || $num == 1) echo "Square root of given number $num is $num"; else{ // Staring from 1, try all // numbers until i*i is // greater than or equal to num. $i = 1; $temp = 1; while ($temp <= $num) { $i++; $temp = $i * $i; } $i=$i-1; echo "Square root of given number $num is $i"; }
//C Program to Find Square root of an integer #include <stdio.h> int main() { // Enter the number int num=15; // Take care of some base cases, // i.e when the given number is 0 or 1. if (num == 0 || num == 1) printf("Square root of given number %d is %d",num, num); else { // Staring from 1, try all numbers until // i*i is greater than or equal to Num. int i = 1, temp = 1; while (temp <= num) { i++; temp = i * i; } printf("Square root of given number %d is %d",num, i-1); } }
Output
Enter the number Num: 15 Output : Square root of given number 15 is 3
Recommended Programs
Program to find factorial of a numberProgram to count number of digits in a number