C Programming Code Examples C > For Loops and While Loops Code Examples Program to find first and last digit Program to find first and last digit Logic to find first and last digit of a number Input a number from user. Store it in some variable say num. Find last digit using modulo division by 10 i.e. lastDigit = num % 10. To find first digit we have simple formula firstDigit = n / pow(10, digits - 1). Where digits is total number of digits in given number. /* C program to find first and last digit of a number */ #include <stdio.h> #include <math.h> int main() { int n, firstDigit, lastDigit, digits; /* Input a number from user */ printf("Enter any number: "); scanf("%d", &n); /* Find last digit */ lastDigit = n % 10; /* Total number of digits - 1 */ digits = (int)log10(n); /* Find first digit */ firstDigit = (int)(n / pow(10, digits)); printf("First digit = %d\n", firstDigit); printf("Last digit = %d\n", lastDigit); printf("C Programming Language | Happy Codings :)"); return 0; }