dimanche 4 janvier 2015

Represnt short form of year in java


The problem is to represent any year in its short form. For example, if you want to find the short form of year 4 to the powern7. 4^7=16384. So the short form is 84.My code is given below. it does not work for the last condition if my base is 123456 and power is 123456 import java.util.Scanner;


public class Main {



public static void main(String[] args) {

System.out.println(lastTwoDig(4,7));
System.out.println(lastTwoDig(5,5));
System.out.println(lastTwoDig(6,4));
System.out.println(lastTwoDig(123456 ,123456));
}
public static String lastTwoDig(long x,long y){
double year = (double) Math.pow(x, y);
String yearString = Double.toString(year);
String lastTwo = yearString.substring( yearString.length()-4,yearString.length()-2);
if(yearString.length()>2){
return lastTwo;
}
return yearString;
}


}





Aucun commentaire:

Enregistrer un commentaire