xxxxxxxxxx
import java.util.Scanner;
public class JavaHungry {
public static void main(String args[]) {
Scanner scan = new Scanner(System.in);
int number = scan.nextInt();
System.out.println("Given number is: "+ number);
int firstDigit = 0;
int lastDigit = 0;
/*Below is the way to calculate the
Last Digit of a Number*/
lastDigit = number % 10;
System.out.println("Last Digit is: " + lastDigit);
/*Below is the way to calculate the
First Digit of a Number*/
while(number != 0)
{
firstDigit = number % 10;
number = number / 10;
}
System.out.println("First Digit is: " + firstDigit);
}
}
xxxxxxxxxx
import java.util.Scanner;
public class JavaHungry {
public static void main(String args[]) {
Scanner scan = new Scanner(System.in);
int number = scan.nextInt();
System.out.println("Given number is: "+ number);
int firstDigit = 0;
int lastDigit = 0;
/*Below is the way to calculate the
Last Digit of a Number*/
lastDigit = number % 10;
System.out.println("Last Digit is: " + lastDigit);
/*Below is the way to calculate the
First Digit of a Number*/
int totalDigits = (int) Math.log10(number);
firstDigit = (int) (number/ (int) Math.pow(10,totalDigits));
System.out.println("First Digit is: " + firstDigit);
}
}
xxxxxxxxxx
import java.util.Scanner;
public class FirstDigit {
public static void main(String[] args) {
Scanner s=new Scanner(System.in);
int num=s.nextInt();
System.out.println(Integer.parseInt(String.valueOf(Integer.toString(num).charAt(0))));
s.close();
}
}