C Program to Find First Digit of a Number

In this post, we will learn how to find the first digit of a number using the C Programming language.

This program asks the user to enter a number, then it finds the first digit of the entered number using the following approaches:

  1. Using While Loop
  2. Using User-defined Functions
  3. Using Log10() and pow()

So, without further ado, let’s begin this tutorial.

C Program to Find First Digit of a Number

// C Program to Find First Digit of a Number
#include <stdio.h>
int main(){
    int num, firstdigit;
    
    // Asking for input
    printf("Enter a number: ");
    scanf("%d", &num);
    
    firstdigit = num;
    
    while (firstdigit >= 10){
        firstdigit = firstdigit / 10;
    }
    
    // Displaying output
    printf("First digit of the entered number is: %d", firstdigit);
    return 0;
}

Output

Enter a number: 512
The first digit of the entered number is: 5

How Does This Program Work?

    int num, firstdigit;

In this program, we have declared two int data type variables named num and firstdigit.

    // Asking for input
    printf("Enter a number: ");
    scanf("%d", &num);

Then, the user is asked to enter a number. This number gets stored in the num named variable. scanf() function is used to take input from the user.

   firstdigit = num;

We assign the value of num to firstdigit.

    while (firstdigit >= 10){
        firstdigit = firstdigit / 10;
    }

Then, we used a while loop to find the first digit of a number.

Suppose, the user enters a number 512. Then,

  • First Iteration: while (512 >= 10)
  • firstdigit = firstdigit / 10 = 512 / 10 =51
  • Second Iteration: while (51 >= 10)
  • firstdigit = firstdigit / 10 = 51 / 10 = 5
  • Third Iteration: while (5 >= 10)
  • Here, the condition is false. Therefore, the while loop terminates and gives us the first digit of the number. The first digit of the entered number gets stored in the firstdigit named variable.
    // Displaying output
    printf("First digit of the entered number is: %d", firstdigit);

Finally, the first digit of the entered number is displayed on the screen using printf() function.

C Program to Find First Digit of a Number Using Function

// C Program to Find the First Digit of a Number Using Function
#include <stdio.h>

int compute_first(int n){
    while (n >= 10){
        n = n / 10;
    }
    return n;
}

int main(){
    int num, firstdigit;
    
    // Asking for input
    printf("Enter a number: ");
    scanf("%d", &num);
    
    // Calling out user-defined function
    firstdigit = compute_first(num);
    
    // Displaying output
    printf("The First digit of the entered number is: %d", firstdigit);
    return 0;
}

Output

Enter a number: 768
The first digit of the entered number is: 7

C Program to Find First Digit of a Number Using log10() and pow()

// C Program to Find First Digit of a Number Using log10() and pow()
#include <stdio.h>
#include <math.h>

int main(){
    int num, count, firstdigit;
    
    // Asking for input
    printf("Enter a positive number: ");
    scanf("%d", &num);
    
    count = log10(num);
    
    firstdigit = num / pow(10, count);
    
    // Displaying output
    printf("The First digit of the entered number is: %d", firstdigit);
    return 0;
}

Output

Enter a positive number: 1245
The first digit of the entered number is: 1

Conclusion

I hope after going through this post, you understand how to find the first digit of a number using the C Programming language.

If you have any doubt regarding the program, then contact us in the comment section. We will be delighted to solve your query.

Also Read:

Leave a Comment

Your email address will not be published. Required fields are marked *