In: Computer Science
Write a function sum_int( n ) to compute the sum of the integers from 1 up to and including n.
Dear Student,
here is the C++ program which implement this.
NOTE: Please not that the below program is tested on ubuntu 14.04 sytem and compiled under g++ compiler.
Program:
------------------------------------------------------------------------------------------------------------------------------------
#include <iostream>
#include <stdlib.h>
using namespace std;
//function prototype
int sum_int(int n);
int main()
{
int num;
cout<<"Enter a Natural number: ";
cin >> num;
cout<<"Sum of the integer from 1 to "<<num<<" Number is "<<sum_int(num)<<endl; //Function Calling
return 0;
}
//Recursive Function Prototype
int sum_int(int n)
{
if(n != 0)
return n + sum_int(n-1);
else
return n;
}
----------------------------------------------------------------------------------------------------------------------------------
Here i have attached the output of the program as a screen shot..
Output:
-----------------------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------------------------------
Kindly Check and Verify Thanks....!!!