Tech Study

Write C++ program to find sum of natural numbers in given range using recursion

Introduction

I have used CodeBlocks compiler for debugging purpose. But you can use any C++ programming language compiler as per your availability.

#include <iostream>
using namespace std;
 
// Function declaration
int sumofnnumbers(int start, int end);
 
int main()
{
    int start, end, sum;
 
    // Inputting lower and upper limit from user
    cout<<"Enter lower limit: ";
    cin>>start;
    cout<<"Enter upper limit: ";
    cin>>end;
 
    sum = sumofnnumbers(start, end);
 
    cout<<"Sum of natural numbers from "<<start <<" to "<<end << ": " <<sum;
 
    return 0;
}
 
//Recursively find the sum of natural number
 
int sumofnnumbers(int start, int end)
{
    if(start == end)
        return start;
    else
        return start + sumofnnumbers(start + 1, end);
}

Result

Write C++ program to find sum of natural numbers in given range using recursion
Write C++ program to find sum of natural numbers in given range using recursion

TaggedWrite C program to find sum of natural numbers in given range using recursion

Python Examples

Introduction: Python Examples are the basic programming concepts of python like python syntax,python data types,,python operators,python if else,python comments etc.. …

Read more

C String Functions

C String Functions perform certain operations, It provides many useful string functions which can come into action. The <string.h> header …

Read more