-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram (1).cs
33 lines (28 loc) · 994 Bytes
/
Program (1).cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
/*Задача 66: Задайте значения M и N. Напишите программу, которая найдёт сумму натуральных элементов в промежутке от M до N.
M = 1; N = 15 -> 120
M = 4; N = 8. -> 30*/
int FindSumOfNumsFromMtoN(int m, int n)
{
if (n < m)
return 0;
else
return n + FindSumOfNumsFromMtoN(m, n - 1);
}
Console.WriteLine("Programm will show all natural numbers from M till N.");
Console.WriteLine("Please, enter M:");
int numberStart = int.Parse(Console.ReadLine()!);
Console.WriteLine("Please, enter N:");
int numberFinish = int.Parse(Console.ReadLine()!);
int result = FindSumOfNumsFromMtoN(numberStart, numberFinish);
if (result == 0)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine($"Incorrect input.");
Console.ResetColor();
}
else
{
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine($"Result is: {result}.");
Console.ResetColor();
}