Write a C program to input a number and find sum of first and last digit of the number using for loop. How to find sum of first and last digit of a number in C programming using loop. Logic to find sum of first and last digit of a number without using loop in C program.

**Example**

**Input**

Input number: 12345

**Output**

Sum of first and last digit: 6

## Required knowledge

Basic C programming, While loop, For loop

## Logic to find sum of first and last digit using loop

Step by step descriptive logic to find sum of first and last digit using loop.

- Input a number from user. Store it in some variable say
`num`. - To find last digit of given number we modulo divide the given number by
`10`. Which is`lastDigit = num % 10`

. - To find first digit we divide the given number by
`10`till`num is greater than``0`. - Finally calculate sum of first and last digit i.e.
`sum = firstDigit + lastDigit`

.

## Program to find sum of first and last digit using loop

```
/**
* C program to find sum of first and last digit of a number using loop
*/
#include <stdio.h>
int main()
{
int num, sum=0, firstDigit, lastDigit;
/* Input a number from user */
printf("Enter any number to find sum of first and last digit: ");
scanf("%d", &num);
/* Find last digit to sum */
lastDigit = num % 10;
/* Copy num to first digit */
firstDigit = num;
/* Find the first digit by dividing num by 10 until first digit is left */
while(num >= 10)
{
num = num / 10;
}
firstDigit = num;
/* Find sum of first and last digit*/
sum = firstDigit + lastDigit;
printf("Sum of first and last digit = %d", sum);
return 0;
}
```

## Program to find sum of first and last digit without using loop

```
/**
* C program to find sum of first and last digit of a number
*/
#include <stdio.h>
#include <math.h>
int main()
{
int num, sum, digits, firstDigit, lastDigit;
sum = 0;
/* Input a number from user */
printf("Enter any number to find sum of first and last digit: ");
scanf("%d", &num);
/* Find last digit */
lastDigit = num % 10;
/* Find total number of digits - 1 */
digits = (int) log10(num);
/* Find first digit */
firstDigit = (int) (num / pow(10, digits));
/* Calculate the sum */
sum = firstDigit + lastDigit;
printf("Sum of first and last digit = %d", sum);
return 0;
}
```

In the above program I have used two mathematical function `pow()`

and `log10()`

. Both the function are present in `math.h`

header file.

`pow()`

function is used to find power of two number.`log10()`

function is used find log base 10 value of a number. We can use the result of`log10()`

to find total digits count of a number - 1.

Output

Enter any number to find sum of first and last digit: 12345 Sum of first and last digit = 6

Happy coding 😉

### Recommended posts

- Loop programming exercises index.
- C program to swap first and last digit of any number.
- C program to calculate sum of digits of a given number.
- C program to find product of digits of a given number.
- C program to find reverse of any number.
- C program to print reverse of a number.
- C program to check palindrome number.

doubt, write here. I willhelp my best.<pre><code> ----Your Source Code---- </code></pre>