C Program to Find the First Digit of a Number
In C programming, finding the first digit of a number is a common beginner exercise that helps you understand loops, arithmetic operations, and number manipulation. In this tutorial, we will cover multiple methods to find the first digit of any positive integer.
Why Learn to Find the First Digit?
Understanding how to extract digits from numbers is fundamental in programming. It improves your problem-solving skills and helps in tasks like validating numbers, checking conditions based on digits, or performing digit-based calculations.
Method 1: Using Division by 10 in a Loop
One way to find the first digit is to repeatedly divide the number by 10 until it becomes less than 10. This method works because dividing by 10 removes the last digit in each step.
#include <stdio.h>
int main() {
int num, firstDigit;
printf("Enter a number: ");
scanf("%d", &num);
while (num >= 10) {
num = num / 10; // Remove last digit
}
firstDigit = num;
printf("The first digit is: %d", firstDigit);
return 0;
}
Sample output: Enter a number: 4589 The first digit is: 4
Method 2: Using Logarithm Function
Another method is using the `log10()` function from the math library to determine the number of digits, then dividing the number by `10^(digits-1)` to get the first digit.
#include <stdio.h>
#include <math.h>
int main() {
int num, firstDigit, digits;
printf("Enter a number: ");
scanf("%d", &num);
digits = (int)log10(num); // Number of digits - 1
firstDigit = num / pow(10, digits);
printf("The first digit is: %d", firstDigit);
return 0;
}
Sample output: Enter a number: 98765 The first digit is: 9
Method 3: Converting Number to String
We can also convert the integer to a string and take the first character. Then, convert it back to integer if needed.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main() {
char str[20];
int firstDigit;
printf("Enter a number: ");
scanf("%s", str);
firstDigit = str[0] - '0'; // Convert first character to integer
printf("The first digit is: %d", firstDigit);
return 0;
}
Sample output: Enter a number: 12345 The first digit is: 1
Explanation
- Method 1 uses a loop to remove digits from the end until only the first digit remains.
- Method 2 uses logarithms to calculate the number of digits and divides accordingly.
- Method 3 treats the number as a string and directly reads the first character.
Tips for Beginners
- Always validate that the input number is positive if the method assumes so.
- Be careful with very large numbers in the logarithm method to avoid overflow.
- For string method, ensure the array is large enough to hold the number plus null character.
Practice Exercises
- Find the last digit of a number.
- Find the sum of digits of a number.
- Check if the first and last digits are the same.
- Reverse a number using loops.
Conclusion
Finding the first digit of a number in C can be done in multiple ways — using loops, mathematical functions, or string manipulation. These exercises strengthen understanding of loops, arithmetic operations, and type conversions. Practicing these methods will improve your ability to solve similar numeric problems in C programming.
Codecrown