TCS Digital Capability Assessment
Write a program to check if, for a given number N, its power of 4 ends with the number itself or not.
Example: 5 ^ 4 = 5 * 5 * 5 * 5 = 625 and 625 ends with number itself i.e., 5
Instructions:
The system does not allow any kind of hard coded input value/values.
The written program code by the candidate will be verified against the inputs that are supplied from the system.
For more clarification, please read the following points carefully till the end.
Input Format:
Input contains an integer ‘N’ denoting the number.
Output Format:
If the input number whose power of 4 ends with the number itself, then print “TRUE”, otherwise, print “FALSE”.
If the user enters a negative integer, then the result should display “Wrong Input”.
Constraints :
1 <= N <= 10 ^ 8
Example 1:
Input:
5
Output:
TRUE
Explanation: 5 ^ 4 = 5 * 5 * 5 * 5 = 625 and 625 ends with number itself i.e., 5
Example 2:
Input:
25
Output:
TRUE
Explanation: 25^4 = 25 * 25 * 25 *25 = 390625 and 390625 ends with number itself i.e., 25
Example 3:
Input:
7
Output:
FALSE
Example 4:
Input:
-6
Output:
Wrong Input
Explanation: Since input doesn’t match input constraint i.e., 1<=N<=10^8
SOLUTION
Program: TCS Digital Capability Assessment Solution in Java
import java.util.Scanner;
public class PowerOfFour {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
int no = sc.nextInt();
long powerFour = (long) Math.pow(no, 4);
String inputNoAsString = String.valueOf(no);
String outputNoAsString = String.valueOf(powerFour);
String endDigit = outputNoAsString.substring(
outputNoAsString.length() - inputNoAsString.length(), outputNoAsString.length());
if(no < 0) {
System.out.println("Wrong Input");
} else if (inputNoAsString.equals(endDigit)) {
System.out.println("TRUE");
} else {
System.out.println("FALSE");
}
}
}