C Program to Sum of First and Last Digits of a Four-Digit number

How to write a C Program to Sum of First and Last Digits of a Four-Digit number in C Programming Language ?


Solution:

This program is based on the "sum of digits" program discussed previously.
This program also has some usage of the Modulus Operator.

If a four-digit number is input through the keyboard,
write a program to obtain the sum of the first and the last digit of this number.
/*HINT: If a number is divided using % , then the number to the right side of the decimal point is the result. (This applies only to integers.) */

  1. #include<stdio.h>
  2.  
  3. main ()
  4.  
  5. {
  6.  
  7. int number, last_digit, first_digit, total;
  8.  
  9. printf (" Enter the number which is to be operated on: ");
  10.  
  11. scanf ("%d", &number);
  12.  
  13.  
  14.  
  15. last_digit = number % 10;
  16.  
  17. total = last_digit;
  18.  
  19.  
  20.  
  21. first_digit = (number / 1000) % 10;
  22.  
  23. total = total + first_digit;
  24.  
  25.  
  26.  
  27. printf ("The total of the first and the last digit of the entered number is: %d", total);
  28.  
  29.  
  30.  
  31. }


Learn More :