# Answer to Question #13947 in C# for neil john

Question #13947

Create a program that solves for the sum of all positive integers starting from 1 until the number given by the user. Use a recursive fuction to address this problem. Assume that the user will always enter a positive number.

Example:

Enter number: 100

Sum of integers from 1 to 100 is 5050.

Example:

Enter number: 100

Sum of integers from 1 to 100 is 5050.

Expert's answer

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

namespace Solves_for_the_sum_of_all_positive_integers

{

class Program

{

static void Main(string[] args)

{

int number;

Console.Write("Enter number:");

number = int.Parse(Console.ReadLine());

Console.Write("Sum of integers from 1 to " + number.ToString() + " is " +

Sum(number));

Console.ReadLine();

}

static int Sum(int number) {

if (number > 0)

{

return number + Sum(number - 1);

}

else

{

return 1;

}

}

}

}

using System.Collections.Generic;

using System.Linq;

using System.Text;

namespace Solves_for_the_sum_of_all_positive_integers

{

class Program

{

static void Main(string[] args)

{

int number;

Console.Write("Enter number:");

number = int.Parse(Console.ReadLine());

Console.Write("Sum of integers from 1 to " + number.ToString() + " is " +

Sum(number));

Console.ReadLine();

}

static int Sum(int number) {

if (number > 0)

{

return number + Sum(number - 1);

}

else

{

return 1;

}

}

}

}

## Comments

## Leave a comment