Write a C program to enter any number and find the sum of first and last digit of the number using for loop. How to find sum of first and last digit of any number in C programming using loop. Calculating sum of first and last digit of any number in C.

Input number: 1234

Output sum of first and last digit: 5

Before moving on to this program it is highly recommended that you must have know how to find first and last digit. If not please first see how to find first and last digit of any number then continue to this program as this program just adds the result of program I mentioned.

Happy coding ;)

**Example:**Input number: 1234

Output sum of first and last digit: 5

### Required knowledge

Basic C programming, For loopBefore moving on to this program it is highly recommended that you must have know how to find first and last digit. If not please first see how to find first and last digit of any number then continue to this program as this program just adds the result of program I mentioned.

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

/** * C program to find sum of first and last digit of a number */ #include <stdio.h> int main() { int i, n, sum=0; /* Reads a number from user */ printf("Enter any number to find sum of first and last digit: "); scanf("%d", &n); /* Checks whether the number is greater than 2 digits */ if(n>10) { /* Adds last digit to sum */ sum += n % 10; } /* Finds the first digit by dividing n by 10 until first digit is left */ while(n>=10) { n = n / 10; } /* Adds first digit to sum */ sum += n; printf("Sum of first and last digit = %d", sum); return 0; }

Output

Enter any number to find sum of first and last digit: 1234

Sum of first and last digit = 5

Sum of first and last digit = 5

Happy coding ;)

### You may also like

- Loop programming exercises index.
- C program to find first and last digit of any number.
- C program to find the sum of digits of any number.
- C program to find product of digits of any number.
- C program to check whether a number is divisible by 5 and 11 or not.
- C program to perform all arithmetic operations.
- C program to print table of any number.
- C program to print all even numbers between 1 to n.
- C program to print all alphabets from a to z.
- C program to check whether a number is Prime number or not.
- C program to print all Prime numbers between 1 to n.
- C program to print Pascal triangle up to n rows.
- C program to print different star (*) patterns series.

THANKS

ReplyDelete