 # C Programming Code Examples

## C > For Loops and While Loops Code Examples

### find sum of first and last digit of any number

``` find sum of first and last digit of any number 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. Input a number from user. Store it in some variable say j. To find last digit of given number we modulo divide the given number by 10. Which is lastDigit = j % 10. To find first digit we divide the given number by 10 till j is greater than 0. Finally calculate sum of first and last digit i.e. sum = firstDigit + lastDigit. #include <stdio.h> int main() { int j, sum=0, firstDigit, lastDigit; /* Input a number from user */ printf("Enter any number to find sum of first and last digit: "); scanf("%d", &j); /* Find last digit to sum */ lastDigit = j % 10; /* Copy j to first digit */ firstDigit = j; /* Find the first digit by dividing j by 10 until first digit is left */ while(j >= 10) { j = j / 10; } firstDigit = j; /* Find sum of first and last digit*/ sum = firstDigit + lastDigit; printf("Sum of first and last digit = %d", sum); return 0; } ``` 