C++ cin Compute the sum from user input number.

Description

C++ cin Compute the sum from user input number.

#include <iostream>
using namespace std;
int sum(int num);  
int main()/*from   w ww .j  a v  a  2 s .com*/
{
   int num, sumd;
   cout << "Please type a number: ";
   cin >> num;
   sumd = sum(num);
   cout << "The sum of the digits is " << sumd;
   return 0;
}
int sum(int num)
{
   int ctr;        
   int sumd=0;     
   if (num <= 0)  
   {
      sumd = num;
   }   
   else
   {
      for (ctr=1; ctr<=num; ctr++)
      {
         sumd += ctr;
      }
   }
   return(sumd);
}



PreviousNext

Related