C Programming Code Examples C > For Loops and While Loops Code Examples C program to find first and last digit of any number C program to find first and last digit of any number Write a C program to input a number from user and find first and last digit of number using loop. How to find first and last digit of a number in C programming. Logic to find last digit of a number Before I explain logic to find first digit, let us first learn to find last digit of a number. To find last digit of a number in programming we use modulo operator %. A number when modulo divided by 10 returns its last digit. Suppose if n = 1234 then lastDigit = n % 10 => 4 Let us implement the above logic to find last digit. #include <stdio.h> int main() { int n, lastDigit; /* Input number from user */ printf("Enter any number: "); scanf("%d", &n); /* Get the last digit */ lastDigit = n % 10; printf("Last digit = %d", lastDigit); return 0; }