Perfect Square Number in MATLAB
Perfect Square number:
In mathematics, a square number or perfect square is an integer that is the square of an integer.
Example :25(square of 5) , 36(square of 6) etc.
If you want to know more on Perfect number , check the below link:
https://en.wikipedia.org/wiki/Square_number
Algorithm to check perfect number:
Step 1:
Square root the number
Step 2:
Now you have to check whether that number is integer or decimal.
To do so , take ceil or round operation & check that the square root number & the number after performing ceil/round operation on the square root number are same or not.
Step 3:
If both are same , then " YES, the number is perfect square" or else "No , the number is not perfect square number".
x=input('Enter the number:');
y=sqrt(x);
z=ceil(y);
if(z==y)
disp('Yes, the number is perfect square number');
else
disp('No , the number is not a perfect square number');
end
You can use same algorithm in JAVA also , just syntax will
differ.
import java.lang.Math;
class Perfect
{
public static void main(String args[])
{
Scanner obj=new Scanner(System.in);
System.out.println("Enter the number:");
int m=obj.nextInt();
double z=Math.sqrt(m);
double y=Math.ceil(z);
if(y==z)
{
System.out.println("YES, the number is perfect square
number");
}
else
{
System.out.println("No, the number is not a perfect square number");
}
}
} Reference Video:
Difference between ceil & round function :
In mathematics, a square number or perfect square is an integer that is the square of an integer.
Example :25(square of 5) , 36(square of 6) etc.
If you want to know more on Perfect number , check the below link:
https://en.wikipedia.org/wiki/Square_number
Algorithm to check perfect number:
Step 1:
Square root the number
Step 2:
Now you have to check whether that number is integer or decimal.
To do so , take ceil or round operation & check that the square root number & the number after performing ceil/round operation on the square root number are same or not.
Step 3:
If both are same , then " YES, the number is perfect square" or else "No , the number is not perfect square number".
MATLAB code:
y=sqrt(x);
z=ceil(y);
if(z==y)
disp('Yes, the number is perfect square number');
else
disp('No , the number is not a perfect square number');
end
Explanation of the code:
You can use same algorithm in JAVA also , just syntax will
differ.
Java Program:
import java.util.Scanner;import java.lang.Math;
class Perfect
{
public static void main(String args[])
{
Scanner obj=new Scanner(System.in);
System.out.println("Enter the number:");
int m=obj.nextInt();
double z=Math.sqrt(m);
double y=Math.ceil(z);
if(y==z)
{
System.out.println("YES, the number is perfect square
number");
}
else
{
System.out.println("No, the number is not a perfect square number");
}
}
} Reference Video:
No comments