Write C++ program to print sum of digits enter by user


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. using namespace std;
  3.  
  4. int main()
  5. {
  6. int num, total;
  7.  
  8. //Reading number
  9. cout<<"Enter any number: "<<endl;
  10. cin>>num;
  11.  
  12. //Adding sum of digit in total variable
  13. for(total = 0; num > 0; num = num/10)
  14. total = total + (num%10);
  15.  
  16. //Printing sum of digit
  17. cout<<"Sum of digits: "<< total;
  18. return 0;
  19. }
  20.  

Result

C++ language techstudy.org

     





Related Topics




Popular Posts