Number Series #3
(1) + (1+2) + (1+2+3) + (1+2+3+4) + ... +
(1+2+3+4+...+n)
Output
Value of n: 5
Sum:35
The program for the series is written
in C# programming language and will accept a number as input. The loops will
iterate based on the number of entry.
Let’s find out a simple and easy way
to code these pattern.
Practical Implementation:
using System;
namespace patternProblem.Number
{
class Number3
{
static void Main(string[] args)
{
Console.WriteLine("Given series is:");
Console.WriteLine("(1) + (1+2) + (1+2+3) + (1+2+3+4) + ... +
(1+2+3+4+...+n)");
Console.WriteLine("----------------------------");
Console.WriteLine();
Console.WriteLine("Enter value of n: ");
int
n = Convert.ToInt32(Console.ReadLine());
Console.WriteLine();
int
sum = 0;
Console.WriteLine("-----Output-----");
Console.WriteLine();
for
(int i = 1; i <= n; i++)
{
for (int j = 1; j <= i; j++)
{
sum = sum + j;
}
}
Console.WriteLine("Sum of series: " + sum);
Console.ReadKey();
}
}
}
Output:
The input
number here is 5. So, the program will iterate five times. The output is shown
below:
Given series is:
(1) + (1+2) + (1+2+3) + (1+2+3+4) + ... +
(1+2+3+4+...+n)
----------------------------
Enter value of n:
5
-----Output-----
Sum of series: 35
Press any key to continue . . .
Keep learning and sharing...
No comments:
Post a Comment