Maybe some of you know how to get first digit of number. For example this question: "how to get '2' from this number "2378940048"? Literally in logic, we can do this question by dividing this number with 10 and it will remain 237894004.8 (in decimal) and we will get 237894004 (in integer), without 8 because integer does not recognize fraction /floating value. It's easy, isn't it?. Then, here it is!
- Using Method First-digit
package firstletter; import java.util.*; public class FirstLetter { public static void main(String[] args) { int a; //declare the variable Scanner input = new Scanner(System.in); //Input from the user System.out.print("Please enter your number: "); a = input.nextInt(); System.out.println("So the first digit of this '" +a+"' is: "+firstDigit(a)); }//end main method public static int firstDigit(int n) { //Condition to determine the value while (n < -9 || 9 < n) n /= 10; return Math.abs(n); //Math absolute is used to round the value }//end First digit method }
- Using Single Method in main
package firstletter; import java.util.*; public class FirstLetter { public static void main(String[] args) { int a; //declare the variable Scanner input = new Scanner(System.in); //Input from the user System.out.print("Please enter your number: "); a = input.nextInt(); //Condition to determine the value while (a < -9 || 9 < a ) { a /= 10; } System.out.println("So the first digit of this '" +a +"' is: "+a); }//end main method
Be Mindful With Your Antiques During Residential Home Moving wholesale white t shirts , cheap bulk t shirt sales , blank t shirts wholesale canada , mens tees wholesale , plain white tees in bulk , cheap tees bulk , bulk childrens clothing , baby designer clothes wholesale , baby dress wholesale , buy baby clothes
ReplyDeleteI have completed a web design then visit tech post today. All the information and knowledge is praise worthy.
ReplyDelete