0
Wap to print 1st and last digit of any no.
in java
2 odpowiedzi
+ 2
Last Digit:
myNum % 10;
First Digit:
myNum / (int) 10^(int)log(myNum))
// Can use Math class for exponents/logs
OR
you can go for some cheesey way, like converting the number to a String.
myNumAsString = String.valueOf(myNum);
// Can also use Integer.toString(myNum);
First Digit:
myNumAsString.charAt(0);
Last Digit:
myNumAsString.charAt(myNumAsString.length() - 1);
@Andres
That doesn't work and is kinda over complicating things.
0
I think it runs, let me try
int first, last;
for(int i=1; num/i>0; i*=10){
last=num-(num/i)*10;
if(num/(i+1)<=0)
first=num/i;
}