67 152
Assignments Done
99,4%
Successfully Done
In November 2018

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

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be first!

Leave a comment

Ask Your question

Submit
Privacy policy Terms and Conditions