Sunday, May 22, 2016

Sum of Digits in C++

A simple program that I wrote using C++ language as my programming language that will ask the user to give a number and then our program will add the sum of digits in a given number by our user. The code is very to understand.

Add me at Facebook my address is jakerpomperada@gmail.com and jakerpomperada@yahoo.com

My mobile number here in the Philippines is 09173084360.


Sample Program Output


Program Listing

#include <iostream>
#include <stdlib.h>

using namespace std;


int main()
{
   int number=0, process=0, sum = 0, remainder=0;

   cout <<"\n\n";
   cout <<"\t Sum of Digits in C++ ";
   cout <<"\n\n";
   cout <<"Kindly give a number : ";
   cin >> number;

   process = number;

   while (process != 0)
   {
      remainder = (process % 10);
      sum = sum  + remainder;
      process = (process / 10);
   }

   cout <<"\n\n";
   cout <<"The total sum of digits is " << sum << ".";
   cout <<"\n\n";
   cout <<"End of Program";
   cout <<"\n\n";
   system("pause");
}

No comments:

Post a Comment