Question

In: Computer Science

Write a function sum_int( n ) to compute the sum of the integers from 1 up...

Write a function sum_int( n ) to compute the sum of the integers from 1 up to and including n.

Solutions

Expert Solution

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....!!!


Related Solutions

Write MIPs program that will read two integers from the user and compute for the sum...
Write MIPs program that will read two integers from the user and compute for the sum and difference of the two integers. Ask the user whether he wants to repeat the program : "[Y/y] / [N/n] ?".
1. Write MIPS assembly code to sum “n” positive integers. Forexample, n=5, read 5 numbers...
1. Write MIPS assembly code to sum “n” positive integers. For example, n=5, read 5 numbers in memory (“.data” section) and add them together. 2. Write MIPS assembly code to calculate N-factorial. Read n from the memory. (Hint: use “.data” to define the content in a memory location)
Write a program that will sum the integers between a given range (limit your range from...
Write a program that will sum the integers between a given range (limit your range from 0 to 50). For example, if the user want to add the integers between (and including) 1 and 10, then the program should add: 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 Algorithm: Program title/description Ask the user to input a start value in the range 0 to 50. Remember to create the...
Write MIPs program that will read two intergers from the user and compute for the sum...
Write MIPs program that will read two intergers from the user and compute for the sum and difference of the two intergers. Ask the user whether he wants to repeat the program : "[Y/y] / [N/n] ?".
Question 1: Write a program to receive two integers as user input and then print: Sum...
Question 1: Write a program to receive two integers as user input and then print: Sum Difference Product Average Maximum of the two Minimum of the two You may use the min and max functions declared in the math class. ********************************************************************************** Question 2: An online bank wants you to create a program that will show a prospective customer, how the deposit will grow. Your program should read the initial balance and the annual interest rate. Interest rate is compounded monthly....
Write a function called alternate that takes two positive integers, n and m, as input arguments...
Write a function called alternate that takes two positive integers, n and m, as input arguments (the function does not have to check the format of the input) and returns one matrix as an output argument. Each element of the n-by-m output matrix for which the sum of its indices is even is 1. All other elements are zero. For example, here is an example run: >> alternate(4,5) ans = 1 0 1 0 1 0 1 0 1 0...
1. write a function to represent the volume of the box given that the sum of...
1. write a function to represent the volume of the box given that the sum of the height and perimeter of the base is equal to 240 inches. 2. what dimensions (lenght width height) of such a box will give the maximum volume? 3. what is the maximum volume?
C++ programing Write a main function that  reads a list of integers from a user, adds to...
C++ programing Write a main function that  reads a list of integers from a user, adds to an array using dynamic memory allocation, and then displays the array. The program also displays the the largest element in the integer array. Requirement: Using pointer notation.
The sum of two integers is 54 and their different is 10. Find the integers
The sum of two integers is 54 and their different is 10. Find the integers
Write a function called is_equal(). This function will take as an argument two integers. Call the...
Write a function called is_equal(). This function will take as an argument two integers. Call the is_equal function and supply two integers. You do not need to get the integers from the keyboard. Compare the two integers. If the two integers are equal, then print "equal". If the two integers are not equal, print "not equal". Call the function from your main code to execute the function. Sample output: equal Sample output: not equal
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT