# C Program to Find First and Last Digit of a Number

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

We will use following approaches to find the first and last digit of a number:

1. Using Loop
2. Without Using Loop
3. Using Functions

So, without further ado, letâ€™s begin this tutorial.

## C Program to Find First and Last Digit of a Number

```// C Program to Find First and Last Digit of a Number
#include <stdio.h>
int main(){
int num, firstDigit, lastDigit;

printf("Enter a number: ");
scanf("%d", &num);

// Finding last digit of a number
lastDigit = num % 10;

// Finding first digit of a number
firstDigit = num;
while (firstDigit >= 10){
firstDigit = firstDigit / 10;
}

// Displaying output
printf("First digit of the entered number is: %d \n", firstDigit);
printf("Last digit of the entered number is: %d", lastDigit);

return 0;
}
```

Output

``````Enter a number: 247
First digit of the entered number is: 2
Last digit of the entered number is: 7
``````

## How Does This Program Work?

```    int num, firstDigit, lastDigit;
```

In this program, we have declared three int data type variables named num, firstDigit and lastDigit.

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

The user is asked to enter a positive number. This number gets stored in the num named variable.

```    // Finding last digit of a number
lastDigit = num % 10;
```

We find the last digit of the entered number with the help of (%) Modulus operator. When the entered number is divided by 10, then it returns the remainder which is the last digit of a number.

```    // Finding first digit of a number
firstDigit = num;
while (firstDigit >= 10){
firstDigit = firstDigit / 10;
}
```

We find the first digit of the entered number with the help of (/) division operator. We divide the entered number by 10 until the number is greater than 10. When the number becomes less than 10, we get the first digit.

```    // Displaying output
printf("First digit of the entered number is: %d \n", firstDigit);
printf("Last digit of the entered number is: %d", lastDigit);
```

At the end, the first and last digit of the entered number is printed on the screen using printf() function.

## C Program to Find First and Last Digit of a Number Without Using Loops

```// C Program to Find First and Last Digit of a Number Without Using Loops
#include <stdio.h>
int main(){
int num, firstDigit, lastDigit, count;

printf("Enter a positive number: ");
scanf("%d", &num);

// Finding last digit
lastDigit = num % 10;

// Finding first digit
count = (int)log10(num);
firstDigit = (int)(num / pow(10, count));

// Displaying output
printf("First Digit of the entered number is: %d \n", firstDigit);
printf("Last Digit of the entered number is: %d", lastDigit);
return 0;
}
```

Output

``````Enter a positive number: 548
First Digit of the entered number is: 5
Last Digit of the entered number is: 8
``````

## C Program to Find First and Last Digit of a Number Using Functions

```// C Program to Find First and Last Digit of a Number Using Functions
#include <stdio.h>

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

int last_digit(int n){
return n % 10;
}

int main(){
int num, firstDigit, lastDigit;

printf("Enter a number: ");
scanf("%d", &num);

// Calling out user-defined functions
firstDigit = first_digit(num);
lastDigit = last_digit(num);

// Displaying output
printf("The first digit of the entered number is: %d \n", firstDigit);
printf("The last digit of the entered number is: %d", lastDigit);
return 0;
}
```

Output

``````Enter a number: 456
The first digit of the entered number is: 4
The last digit of the entered number is: 6
``````

## Conclusion

I hope after going through this post, you understand how to find the first and the last digit of a number using 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.