Showing posts with label Last. Show all posts
Showing posts with label Last. Show all posts

C Program to Sum of The First and Last Digit Of 'n' Digit Number

How to write a C Program to Sum of The First and Last Digit Of  'n' Digit Number  in C Programming Language ?


  1. int main()
  2. {
  3. int num,len,last;
  4. printf("\nEnter n digit number \n");
  5. scanf("%d",&num);
  6. last=num%10;
  7. while(num>0)
  8. {
  9. num=num/10;
  10. len=len+1;
  11. }
  12. num=num/(pow(10,len-1));
  13. num=num%10;
  14. printf("the result is %d",last+num);
  15. getch();
  16. return 0;
  17.  
  18. }

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. }