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.
1
Expert's answer
2012-08-30T11:41:30-0400
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 the first!

Leave a comment

LATEST TUTORIALS
APPROVED BY CLIENTS