0

Wap to print 1st and last digit of any no.

in java

7th Jul 2017, 10:14 PM
shakti
2 ответов
+ 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.
8th Jul 2017, 2:34 AM
Rrestoring faith
Rrestoring faith - avatar
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; }
7th Jul 2017, 11:04 PM
Andrés04_ve
Andrés04_ve - avatar