Q:

Write a program in C# Sharp to find the sum of first n natural numbers using recursion

0

Write a program in C# Sharp to find the sum of first n natural numbers using recursion. 
Test Data :
How many numbers to sum : 10
Expected Output :
The sum of first 10 natural numbers is : 55

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

using System;
class RecExercise3
{
static void Main(string[] args) 
    {
    Console.Write("\n\n Recursion : Sum of first n natural numbers :\n");
    Console.Write("--------------------------------------------------\n");
	Console.Write(" How many numbers to sum : ");
	int n = Convert.ToInt32(Console.ReadLine());    
    Console.Write(" The sum of first {0} natural numbers is : {1}\n\n", n,SumOfTen(1,n));
    }
static int SumOfTen(int min, int max) 
    {
    return CalcuSum(min, max);
    }
static int CalcuSum(int min, int val) 
    {
    if (val == min)
        return val;
    return val + CalcuSum(min, val - 1);
    }
}

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now