Given an integer input, the Objective is to check whether the square of the number ends with the same number or not.
import java.util.*;
class HelloWorld {
public static void main(String[] args) {
int n=sc.nextInt();
int square=(int)Math.pow(n,2);
System.out.print(square%10==n?"yes":"no");
}
}
This program determines if a given integer satisfies the condition where the last digit of its square equals the number itself.
For example:
- Input:
- Square =
- Last digit =
- Output = Yes
- Input:
- Square =
- Last digit ≠
- Output = N0
How It Works
Input:
The program reads an integer .Calculate the Square:
Use to compute the square of .
Explicitly cast the result tointsince returns adouble.Check the Last Digit:
Use the modulus operator (%) to extract the last digit of the square.
Compare this last digit with .Output:
Print "Yes" if the last digit of the square equals , otherwise print "No."
Example Input and Output
Example 1
Input:
5
Process:
- Last digit =
Output:
Yes, the square of 5 ends with 5
Example 2
Input:
7
Process:
- Last digit =
Output:
No, the square of 7 does not end with 7
Edge Cases
Single-digit Numbers:
All single-digit numbers are directly checked for their properties.Zero:
Input = , Square = , Output = Yes (since ends with ).
No comments:
Post a Comment