Basic Programming C# Examples

C# Program to find Sum of A.P. Series

The Program in C# Program to find Sum of A.P. Series is given below:

using System;

namespace AP_Series_Sum
{
    class Program
    {
        static void Main(string[] args)
        {
            int a, d, n;
            double sum;

            Console.WriteLine("Hello Codeauri Family,enter the first term of the AP series: ");
            a = Convert.ToInt32(Console.ReadLine());

            Console.WriteLine("Similarly, enter the common difference of AP series: ");
            d = Convert.ToInt32(Console.ReadLine());

            Console.WriteLine("Then, the number of terms in the AP series: ");
            n = Convert.ToInt32(Console.ReadLine());

            sum = (n * (2 * a + (n - 1) * d)) / 2;

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

            Console.ReadLine();
        }
    }
}

Output:

Hello Codeauri Family,enter the first term of the AP series: 1
Similarly, enter the common difference of AP series:
3
Then, the number of terms in the AP series:
2
Okay, the sum of the AP series is: 5

Pro-Tips💡

Here are the step by step execution of above program:

  1. The first line using System; is a directive that tells the compiler to include the System namespace in the program, which is required to use the Convert class.
  2. The program has a class named Program that contains the Main method. The Main method is the entry point of the program.
  3. In the Main method, three integer variables a, d, and n are declared to store the first term of the AP series, the common difference, and the number of terms in the series, respectively.
  4. The sum variable is declared as a double to store the sum of the AP series.
  5. The Console.WriteLine method is used to prompt the user to enter the first term of the AP series, the common difference, and the number of terms in the series.
  6. The Convert.ToInt32 method is used to convert the input string to an integer and store it in the appropriate variable.
  7. The sum of the AP series is calculated using the formula sum = (n * (2 * a + (n - 1) * d)) / 2.
  8. The Console.WriteLine method is used to display the sum of the AP series.
  9. The Console.ReadLine method is used to wait for the user to press the Enter key before closing the program.

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