Program 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; } }
Pingback: Basic Concepts of Programming Languages C++ Programming Examples
It’s hard to find knowledgeable people on this topic, but you sound like you know what you’re talking about! Thanks
Pingback: C++ projects for beginners with source code - Codeboks