Calcular la sumatoria de n primero números
Solución
Se ingresa un número entero y retorna la suma de los números anteriores incluido el numero ingresado.
Ejemplo
- Ingresa: 5
- Salida: 15
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace sumatoriarec
{
class Program
{
static int sumatoria(int X,int N) {
if (N > 0)
{
X = sumatoria(X, N - 1);
X = X + N;
return X;
}
else {
X = 0;
return X;
}
}
static void Main(string[] args)
{
int X = 0;
Console.WriteLine("ingrese un numero\n");
int N = Convert.ToInt32(Console.ReadLine());
X = sumatoria(X, N);
Console.WriteLine(X);
Console.ReadLine();
}
}
}
1 comentarios:
Gracias, Por Compartir,,, Exelente... Like
Publicar un comentario