Basic Programming C# Examples

C# Program to Find Sum of Series[1 X^2/2!+X^4/4!-…]

The Program in  C# Program to Find Sum of Series[1 X^2/2!+X^4/4!-…] is given below:

using System;

namespace SumOfSeries
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello Codeauri Family, enter the value of X here:");
            double x = double.Parse(Console.ReadLine());
            Console.WriteLine("Similarly, enter the number of terms in the series:");
            int n = int.Parse(Console.ReadLine());

            double sum = 0;
            int sign = 1;

            for (int i = 0; i < n; i++)
            {
                double factorial = 1;
                for (int j = 1; j <= 2 * i; j++)
                {
                    factorial *= j;
                }

                sum += sign * Math.Pow(x, 2 * i) / factorial;
                sign = -sign;
            }

            Console.WriteLine("Okay, the sum of the series is: " + sum);
            Console.ReadLine();
        }
    }
}

Output:

Hello Codeauri Family, enter the value of X here:
7
Similarly, enter the number of terms in the series:
7
Okay, the sum of the series is: 7.17000588933314

Pro-Tips💡

Here are the step by step execution of above program:

using System; – This line is a directive that specifies the namespace System to be used in the code. The System namespace contains classes and methods commonly used in C# programs, including the Console class we’ll be using later.

namespace SumOfSeries – This line declares the namespace for the program, which is SumOfSeries. A namespace is a way of grouping related classes and methods in C#.

class Program – This line declares a class named Program. In C#, classes are used to define custom types and encapsulate data and behavior.

static void Main(string[] args) – This line declares the Main method, which is the entry point of the program. The static keyword means that the method can be called without creating an instance of the class. The void keyword indicates that the method doesn’t return a value, and the string[] args parameter is an array of strings that can be passed to the program as command-line arguments.

Console.WriteLine("Hello Codeauri Family, enter the value of X here:"); – This line writes a message to the console, asking the user to enter the value of x.

double x = double.Parse(Console.ReadLine()); – This line reads a line of text from the console and converts it to a double type. The value of x is then stored in the x variable.

Console.WriteLine("Similarly, enter the number of terms in the series:"); – This line writes another message to the console, asking the user to enter the number of terms in the series.

int n = int.Parse(Console.ReadLine()); – This line reads a line of text from the console and converts it to an int type. The value of n is then stored in the n variable.

double sum = 0; – This line declares a double variable named sum and initializes it to 0. This variable will store the sum of the series.

int sign = 1; – This line declares an int variable named sign and initializes it to 1. This variable will keep track of the sign of each term in the series.

for (int i = 0; i < n; i++) – This line starts a for loop that will run n times. The loop variable i is declared and initialized to 0, and it will be incremented by 1 in each iteration of the loop. The loop will continue to run as long as i is less than n.

double factorial = 1; – This line declares a double variable named factorial and initializes it to 1. This variable will store the factorial of 2i in each iteration of the loop.

for (int j = 1; j <= 2 * i; j++) – This line starts a nested for loop that will calculate the factorial of 2i. The loop variable j is declared and initialized to 1, and it will be incremented by 1 in each iteration of the loop. The loop will continue to run as long as j is less than or equal to 2 * i.

Learn C-Sharp ↗

C-sharp covers every topic to learn about C-Sharp thoroughly.

Learn C Programming ↗

C-Programming covers every topic to learn about C-Sharp thoroughly.

Learn C++ Programming↗

C++ covers every topic to learn about C-Sharp thoroughly.

Codeauri is Code Learning Hub and Community for every Coder to learn Coding by navigating Structurally from Basic Programming to Front-End Development, Back-End Development to Database, and many more.

Related Posts

Programming Languages( Types, Pros and Cons)-Codeauri

What are the types or levels of Programming Languages? The types or levels of programming languages are divided into two types: Types Of Programming Languages: 1.Machine-level Language :1st…

C# Program to Find Sum of Rows & Columns of a Matrix

The Program in C# Program to Find Sum of Rows & Columns of a Matrix is given below: Output: Hello Codeauri Family,enter the number of rows and columns…

C# Program to Calculate Determinant of Given Matrix

The Program in C# Program to Calculate Determinant of Given Matrix is given below: Output: Hello Codeauri Family, enter the number of rows and columns of the matrix…

C# Program to Find Sum of right Diagonals of a Matrix

The Program in C# Program to Find Sum of right Diagonals of a Matrix is given below: Output: Hello Codeauri Family, enter the number of rows and columns…

C# Program to Find Transpose of Given Matrix

The Program in C# Program to Find Transpose of Given Matrix is given below: Output: Hello Codeauri Family, enter the number of rows and columns in the matrix:22Enter…

C# Program for Multiplication of two square Matrices

The Program in C# Program for Multiplication of two square Matrices is given below: Output: Hello Codeauri Family, enter the number of rows/columns in the matrices:2Enter the elements…

Leave a Reply

Your email address will not be published. Required fields are marked *

Your Journey into Code Begins Now: Discover the Wonders of Basic Programming

X