Problem:
Design an algorithm that finds the integer whose square is closest to but greater than the number input as data.
Solution:
package com.myprograms;
import java.util.Scanner;
public class FindNearestSquaretNumber {
int n;
int result;
public static void main(String[] args) {
FindNearestSquaretNumber findNearestSquaretNumber = new FindNearestSquaretNumber();
findNearestSquaretNumber.getTheNumber();
findNearestSquaretNumber.isGreaterThanInput();
findNearestSquaretNumber.printTheResult();
}
public void printTheResult(){
if(result != 0){
System.out.println("Nearest square root of n and gretaer than n value is: " + result);;
}
else {
System.out.println("could not found the greater than n value ");;
}
}
public void getTheNumber(){
Scanner s = new Scanner(System.in);
System.out.println("enter the number");
n = s.nextInt();
}
public int findNearestSquare(int num){
//find the square root
int num1 = (int) Math.sqrt(num);
// if square root square’s is num… given number is a Perfect Square return the same //number
if(num1 * num1 == num)
return num;
else{
//else calculate floor_square and ceil_square get absolute difference between num
// which is minimum that is the nearest square num
int floorSquare = num1 * num1;
int ceilingSquare = (num1 + 1) * (num1 + 1);
if(Math.abs(floorSquare-num) < Math.abs(ceilingSquare -num) )
return floorSquare;
else
return ceilingSquare;
}
}
public void isGreaterThanInput(){
result = findNearestSquare(n);
if(result < n){
result = 0;
}
}
}
Output:
enter the number
3
Nearest square root of n and gretaer than n value is: 4
Design an algorithm that finds the integer whose square is closest to but greater than the number input as data.
Solution:
package com.myprograms;
import java.util.Scanner;
public class FindNearestSquaretNumber {
int n;
int result;
public static void main(String[] args) {
FindNearestSquaretNumber findNearestSquaretNumber = new FindNearestSquaretNumber();
findNearestSquaretNumber.getTheNumber();
findNearestSquaretNumber.isGreaterThanInput();
findNearestSquaretNumber.printTheResult();
}
public void printTheResult(){
if(result != 0){
System.out.println("Nearest square root of n and gretaer than n value is: " + result);;
}
else {
System.out.println("could not found the greater than n value ");;
}
}
public void getTheNumber(){
Scanner s = new Scanner(System.in);
System.out.println("enter the number");
n = s.nextInt();
}
public int findNearestSquare(int num){
//find the square root
int num1 = (int) Math.sqrt(num);
// if square root square’s is num… given number is a Perfect Square return the same //number
if(num1 * num1 == num)
return num;
else{
//else calculate floor_square and ceil_square get absolute difference between num
// which is minimum that is the nearest square num
int floorSquare = num1 * num1;
int ceilingSquare = (num1 + 1) * (num1 + 1);
if(Math.abs(floorSquare-num) < Math.abs(ceilingSquare -num) )
return floorSquare;
else
return ceilingSquare;
}
}
public void isGreaterThanInput(){
result = findNearestSquare(n);
if(result < n){
result = 0;
}
}
}
Output:
enter the number
3
Nearest square root of n and gretaer than n value is: 4
enter the number
23
Nearest square root of n and gretaer than n value is: 25
No comments:
Post a Comment