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.

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;
       }
}

Output:

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

M.A Siddiqui

Hi, I am M.A Siddiqui. I'm a Computer Science Student, In this website, I'm teaching you Multiple languages and provides Projects and other Subjects Notes.

You may also like...

Leave a Reply

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