Answer to Question #290051 in C for thilak

Question #290051

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.



1
Expert's answer
2022-01-23T15:17:14-0500
#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;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS