Untitled
unknown
plain_text
3 years ago
991 B
7
Indexable
namespace Cwiczenie3
{
class Program
{
static int Metoda3(int n)
{
int mnożenie = 0, suma = 0;
if (n > 2)
{
for (int i = 2; i <= n; i++)
{
mnożenie = ((i - 1) * i);
suma += mnożenie;
}
}
return suma;
}
static void Main(string[] args)
{ /* Napisz metodę rekurencyjną int Metoda3(int n) sumującą elementy ciągu 1*2+ 2*3 + 3*4 + … + (n-1)*n.
* Zademonstruj jej działanie w programie ilustrującym.
* Jeżeli podana będzie wartość n mniejsze niż 2 zwracamy 0; */
Console.WriteLine("Podaj n: ");
int n = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Suma {0} elementów ciągu wynosi: ", n);
Console.WriteLine(Metoda3(n));
Console.ReadKey();
}
}
}Editor is loading...