Home » Programming » C program to find sum of first and last digit of a number

# C program to find sum of first and last digit of a number

In this example, we will see how we can write a c program to find sum of first and last digit of a number. This is an interesting program to learn about the basics of logical ideas. It might seem kind of confusing to find just the first and last digit of a number but with a simple logic it becomes really easy. Basically, this can be solved using two methods; using loops and without using loops.

## How will we find first and last digit?

Let us assume a number with five digits, 54321. We will use this number to visualize the working of our algorithm. Also, let us assume rightmost digit as first digit and leftmost digit as last digit. You can assume either way as you like.

Starting, how will we find first digit? Just modulo divide it with 10. We will get remainder which is the first digit of the number.

```// First number
54321 % 10 = 1 ```

Then, we have to find last digit. At this moment, we are known to the length of the number i.e. number of digits. So, we can divide number with 10000 and the result is the last digit.

```// Last digit
54321 / 10000 = 5```

If the number is of 4 digits we have to divide it by 1000 and by 100 if the number have 3 digits. So, we can see from above pattern that we have to divide number by 10 with exponent number of digits – 1.

`last_digit = num / pow(10, number_of_digit -1 )`

But what happens when we don’t know the length of the number? So, in this condition firstly we have to find the number of digits. We can find this by finding the log with base 10 of the number. One important thing to keep in mind that it gives value which is number of digit – 1.

```num = 4321
// log10(num) gives total number of digits - 1
number_of_digit    = (int) (log10(num) + 1); // exponent = 3```

Since, we log10() returns floating point value we have to change it integer. We will find power of 10 with the number of digit – 1 and use it to divide the number to find last digit.

`last_digit = num / pow(10, number_of_digit - 1) // last_digit = 4321 / pow(10, 3) = 4`

## Algorithm to find sum of first and last digit of a number without using loops

This algorithm is used to find the sum of first and last digit of a number.

```Step 1: Start.
Step 2: Take a number as input in num.
Step 3: To find first digit modulo divide number by 10. Remainder is the first digit.
Step 4: To find last digit,
Step 4a: Find the number of digits.
Step 4b: Divide number by 10 with exponent number of digits - 1. Result is the last digit.
Step 5: Add first digit and last digit.
Step 6: Display sum.
Step 7: End.```

In short,

```Start,
num = Take a number as input from user
first_digit = num % 10
number_of_digit = (int) (log10(num) + 1)
last_digit = num / pow(10, number_of_digit - 1)
sum = first_digit + last_digit
Display sum
End.```

## C program to find sum of first and last digit of a number without using Loops

```// considering first digit as the right most digit

#include<stdio.h>
#include<math.h>

void main(){
long int num;
int first_digit, last_digit, number_of_digit;
int sum;
printf("Enter a number: ");
scanf("%ld", &num);
first_digit = num % 10;         // getting first digit of inputted number
number_of_digit = (int) (log10(num) + 1); // log10(num) gives total number of digits - 1
last_digit = num / pow(10, number_of_digit - 1);   // getting last digit of inputted number
sum = first_digit + last_digit;
printf("\nThe sum of first and last digit of %ld = %d", num, sum);
}```

Note: Functions `pow()` and `log10()` are present in `math.h` header file.

Output

```Enter a number: 56382

The sum of first and last digit of 56382 = 7```

### Explanation

• At first, we will take a number as input. We have used `long int` since we don’t know how many digits a number have. It have higher range than `int`.
```printf("Enter a number: ");
scanf("%ld", &num);```
• After that, to find first digit we find remainder of number when divided by 10. This can be done using `modulo operator`.
`first_digit = num % 10;`
• Then, we have to find last digit. Firstly, we find the number of digits in that number which we can find by calculating `log base 10` value of a number. Then we divide number by 10 with the power `number of digits - 1`. We can find power of 10 using `pow()`.
```number_of_digit = (int) (log10(num) + 1); // log10(num) gives total number of digits - 1
last_digit = num / pow(10, number_of_digit - 1);   // getting last digit of inputted number```
• Finally, we add first digit and last digit and display sum.
```sum = first_digit + last_digit;
printf("\nThe sum of first and last digit of %ld = %d", num, sum);```

### Limitation

Use of extra header file: This method uses extra header file math.h. If we have to solve this problem without any extra header file we cannot use this method.

## How will we find sum of first and last digit using loops?

Difference between method with loops and without loops only lies in finding last digit. In method without loops, we are using mathematical formula to find the number of digits and dividing it by with the 10 with power number of digits -1. But in method with loops we divide the number repeatedly until we get the last digit only.

## Algorithm to find sum of first and last digit of a number using loops

```Step 1: Start.
Step 2: Take a number as input in num.
Step 3: To find first digit modulo divide number by 10. Remainder is the first digit.
Step 4: To find last digit,
Step 4a: Repeat step 4b until num >= 10
Step 4b: last_digit = last_digit / 10;
Step 5: Add first digit and last digit.
Step 6: Display sum.
Step 7: End.```

## C Program find sum of first and last digit of a number using loops

```// considering first digit as the right most digit

#include<stdio.h>

void main(){
long int num;
int first_digit, last_digit, number_of_digit;
int sum;
printf("Enter a number: ");
scanf("%ld", &num);
first_digit = num % 10;         // getting first digit of inputted number
last_digit = num;
// getting last digit of inputted number
while(last_digit >= 10){
last_digit = last_digit / 10;
}
sum = first_digit + last_digit;
printf("\nThe sum of first and last digit of %ld = %d", num, sum);
}```

Output

```Enter a number: 7635

The sum of first and last digit of 7635 = 12```

### Explanation

As said earlier, most of the program is same as other method. So, we will explain those parts only where difference lies between them i.e. to find last digit.

At first we assign num to last_digit since we will be dividing num repeatedly until we get last digit only.

`last_digit = num;`

After that we use a loop. This loop will run until we get only last digit only i.e. `num >= 10`.

```while(last_digit >= 10){
. . .
}```

Then, we divide last_digit by 10 to get the last digit. This step will eliminate one digit from last_digit (from left) each time we divide it by 10.

`last_digit = last_digit / 10;`

Finally, we add first_digit and last_digit and display sum.

More Examples