Write a program to compute the sum recursively in C++

Program Sum Recursively in C++

Write a program to compute the sum recursively in C++

Object: We can define the sum from 1 to x (i.e. 1 + 2 + … + x) recursively as follows for integer x ≥ 1: 1, if x = 1 x + sum from 1 to x-1 if x > 1 Write a program to compute the sum 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10  recursively. C++ projects for beginners with source code

Code:

#include<iostream>
#include<conio.h>
using namespace std;
int readd(int nom);

void main()
{
       int num;
       cout<<“Enter any Positive Number: “;
       cin>>num;
       cout<<“Sum recursively: “<<readd(num);
       getch();
}
int readd(int nom)
{
       if(nom!=0)
       {
             return nom+readd(nom-1);
       }
       else
       {
             return nom;
       }
}

#include<iostream>
#include<conio.h>
using namespace std;
int readd(int nom);

void main()
{
	int num;
	cout<<"Enter any Positive Number: ";
	cin>>num;
	cout<<"Sum recursively: "<<readd(num);
	getch();
}
int readd(int nom)
{
	if(nom!=0)
	{
		return nom+readd(nom-1);
	}
	else
	{
		return nom;
	}
}

Output:

Write a program to compute the sum recursively in C++

1 Write a C++ program to check triangle by entering 3 angles
2 Write a C++ program that asks for the number of units sold and computes the total cost of the purchase.
3 Write a program that asks the user to enter a number of seconds.
4 Write a C++ program that will ask user to enter two integral numbers. The numbers should be swapped without using any third variable.

3 thoughts on “Write a program to compute the sum recursively in C++”

  1. Pingback: Basic Concepts of Programming Languages C++ Programming Examples

  2. It’s hard to find knowledgeable people on this topic, but you sound like you know what you’re talking about! Thanks

  3. Pingback: C++ projects for beginners with source code - Codeboks

Leave a Comment

Your email address will not be published. Required fields are marked *