Write C++ program to find the sum of first and last digit of any number


techstudy.org

HP 15 Core i3 7th gen Laptop(4GB, 1TB HDD, Windows 10) | Rs. 31,490

Introduction

I have used CodeBlocks compiler for debugging purpose. But you can use any C++ programming language compiler as per your availability.


  1. #include <iostream>
  2. #include <math.h>
  3.  
  4. using namespace std;
  5.  
  6. int main()
  7. {
  8. int num, sum=0, firstDigit, lastDigit;
  9.  
  10. //Reading a number from user
  11. cout<<"Enter any number:";
  12. cin>>num;
  13.  
  14. lastDigit = num % 10;
  15.  
  16. firstDigit = num;
  17.  
  18. while(num >= 10)
  19. {
  20. num = num / 10;
  21. }
  22. firstDigit = num;
  23.  
  24. //Finding sum of first and last digit
  25. sum = firstDigit + lastDigit;
  26.  
  27. cout<<"Sum of first and last digit: "<<sum;
  28.  
  29. return 0;
  30. }

Result

C++ language techstudy.org

     





Related Topics




Popular Posts