Program to Find Sum of First Digit and Last Digit of a Number

Program


#include<stdio.h>
#include<conio.h>

int main()
{
	 long number, firstDigit, lastDigit, sum;
	 clrscr();
	 printf("Enter any number: ");
	 scanf("%ld", &number);
	
	 /* Finding last digit */
	 lastDigit = number%10;
	
	 /* Finding first digit */
	 while(number>9)
	 {
	  	number = number/10;
	 }
	 firstDigit = number;
	
	 /* Finding sum */
	 sum = firstDigit + lastDigit;
	
	 /* Displaying result */
	 printf("Sum of first digit and last digit is: %ld", sum);
	
	 getch();
	 return(0);
}

Output

Run 1:
-------------
Enter any number: 87986 ↲
Sum of first digit and last digit is: 14

Run 2:
-------------
Enter any number: 88 ↲
Sum of first digit and last digit is: 16

Note: ↲ indicates ENTER is pressed.