Write a function that takes a positive integer as input and returns the leading digit in its decimal representation. For example, the leading digit of 234567 is 2.
#include <stdio.h>
int leading_digit(int x) {
int d=0;
while (x > 0) {
d = x % 10;
x /= 10;
}
return d;
}
int main() {
int x, d;
printf("Enter a positive number: ");
scanf("%d", &x);
d = leading_digit(x);
printf("The leading digit is %d", d);
return 0;
}
Comments
Leave a comment