Editorial
If the number is negative, we will change its sign, making it positive - this will not change the first digit. We divide the number by 10 until there is only one digit left - this will be the first digit of the original number.
Implementation of the algorithm
We read an integer n
.
scanf("%lld", &n);
If the number is negative, then we make it positive.
if (n < 0) n = -n;
We divide the number by 10 until it contains only the first digit.
while(n > 9) n /= 10;
We print the first digit of the number.
printf("%lld\n", n);
Java implementation
import java.util.*; public class Main { public static void main(String[] args) { Scanner con = new Scanner(System.in); long n = con.nextLong(); if (n < 0) n = -n; while(n > 9) n /= 10; System.out.println(n); con.close(); } }