In olden days finding square roots seemed to be difficult but nowadays it can be easily done using in-built functions available across many languages.
Assume that you happen to hear the above words and you want to give a try in finding the square root of any given integer using in-built functions. So here's your chance.
Input
The first line of the input contains an integer T, the number of test cases. T lines follow. Each line contains an integer N whose square root needs to be computed.
Output
For each line of the input, output the square root of the input integer, rounded down to the nearest integer, in a new line.
Constraints
1<=T<=20
1<=N<=10000
Input:
3
10
5
10000
Output:
3
2
100
C
#include <stdio.h> int main(void) { int t, n, i; scanf("%d", &t); while (t--) { scanf("%d", & n); for (i = 1;(i*i)<=n; i++) { } printf("%d\n", i-1); i = 0; } return 0; }
C++
#include <iostream> using namespace std; int main() { int t, n, i; cin >> t; while (t--) { int count = 0; cin >> n; for (i = 1; i <= n; i++) { if ((i * i) <= n) count++; else break; } cout << count; cout << endl; } return 0; }
python
for _ in range(int(input())): print(int(int(input())**0.5))
java
import java.util.*; import java.lang.*; import java.io.*; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; class SquareRoot { static int sqrt(int n) { if(n==0||n==1) { return n; } int start=1; int end=n; int ans=0; while(start<=end) { int mid=(start+end)/2; if(mid*mid==n) { return mid; } else if(mid*mid>n) { end= mid-1; } else { start=mid+1; ans=mid; } } return ans; } public static void main(String[] args) throws NumberFormatException, IOException{ { // TODO Auto-generated method stub InputStreamReader is= new InputStreamReader(System.in); BufferedReader br= new BufferedReader(is); int n= Integer.parseInt(br.readLine()); for(int i=0;i<n;i++) { int x= Integer.parseInt(br.readLine()); System.out.println(sqrt(x)); } } }
No comments:
Post a Comment