Compute the sum from user input number. - C++ Function

C++ examples for Function:Function Return

Description

Compute the sum from user input number.

Demo Code

#include <iostream>
using namespace std;
int sum(int num);  
int main()//from   w w  w  . jav  a2 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);
}

Result


Related Tutorials