Introduction
This article is a continuation of Number series.
To set up the environment for C# in visual studio code or read more number series, please refer to my previous article Number Series Codes using C#.
Geometric Progression Series (G. P)
The general form of a GP is a, ar, ar2, ar3 and so on. The nth term of a GP series is Tn = arn-1, where a = first term and r = common ratio = Tn/Tn-1) .
The sum of infinite terms of a GP series S∞= a/(1-r) where 0< r<1. If a is the first term, r is the common ratio of a finite G.P.
G.P series calculated using formula {a, ar, ar2, ar3, ... } where a is first number and r is common ration
- //print the sum of Geometric Progresson
- public void GPSeries()
- {
- // The general form of a GP is a, ar, ar2, ar3 and so on. The nth term of a GP series is Tn = arn-1, where a = first term and r = common ratio = Tn/Tn-1) .
- // The sum of infinite terms of a GP series S∞= a/(1-r) where 0< r<1. If a is the first term, r is the common ratio of a finite G.P.
- // suppose first number = 1, terms = 5, common ratio = 2
- // Number of G.P Series are 1, 2, 4, 8, 16, 32
- // The sum of the G.P series: 32 {a, ar, ar2, ar3, ... } where a is first number and r is common ration
- double gProgess = 0, sum = 0;
- Console.Write("Enter the first number: ");
- int firstNum = Convert.ToInt32(Console.ReadLine()); // read the first number and save into firstNum variable
- Console.Write("Enter the number or terms: ");
- int nTerm = Convert.ToInt32(Console.ReadLine()); // read the terms
- Console.Write("Enter the common ratio : ");
- int ratio = Convert.ToInt32(Console.ReadLine()); // read the common ratio
- int firstValue = 1; // initialize the first value
- Console.Write("Number for the G.P Series:");
- Console.Write(firstValue + " "); // print first value.
- for (int i = 1; i <= nTerm; i++) // loop till nTerm.
- {
- gProgess = Math.Pow(ratio, i); // find the g.p progression
- Console.Write("{0} ", gProgess); // print the progression
- }
- sum = (firstNum * (1 - (Math.Pow(ratio, nTerm + 1)))) / (1 - ratio); // calculate the sum and save into variable sum.
- double term = firstNum * (Math.Pow(ratio, nTerm - 1)); // calculate the term and store into variable term.
- Console.Write("\nThe tn terms of G.P. : {0}\n\n", term); // print the term.
- Console.Write("\nThe Sum of the G.P. series : {0}\n\n", sum);// print the sum.
- }
Output

Arithmetic Progression Series (A. P)
An arithmetic progression (AP) or arithmetic sequence is a sequence of numbers such that the difference between the consecutive terms is constant.
A.P series with common difference of 3 for 10 items is {1 , 4, 7, 10, 13, 16, 19 , 22, 25, 28}
- // Print sum of Arithmetic Progression(A.P) series
- public void printAPSeries()
- {
- //An arithmetic progression (AP) or arithmetic sequence is a sequence of numbers such that the difference between the consecutive terms is constant.
- // A.P series with common difference of 3 for 10 items is {1 , 4, 7, 10, 13, 16, 19 , 22, 25, 28}
- // Sum of Above series is = 145
- Console.Write("Enter the starting number: ");
- int startNum = Convert.ToInt32(Console.ReadLine()); // read items
- Console.Write("Enter the number of items: ");
- int items = Convert.ToInt32(Console.ReadLine()); // read items
- Console.Write("Enter the common difference: ");
- int diff = Convert.ToInt32(Console.ReadLine()); // read difference number
- int sum = (items * (2 * startNum + (items - 1) * diff)) / 2; // calculate the sum
- int lastNum = startNum + (items - 1) * diff; // calculate the new last number
- Console.Write("\nThe Sum of the A.P. series are : \n");
- for (int i = startNum; i <= lastNum; i = i + diff)
- {
- if (i != lastNum)
- {
- Console.Write("{0} + ", i);
- }
- else
- {
- Console.Write("{0} = {1} \n\n", i, sum);
- }
- }
- }
Output

Strong Number
A strong number is a special number whose sum of the factorial of digits is equal to the original number.
- // check whether a number is Strong Number or not.
- public void CheckStrongNumber()
- {
- // Strong number is a special number whose sum of factorial of digits is equal to the original number.
- // Suppose input number is 145
- // Factorial of digita of input numbers is 1 = 1, 4 = 24 , 5 = 120
- // Sum of factorial number is: 1 + 24 + 120 = 145
- // Hence the input number 145 is a armstrong number.
- Console.Write("Enter the number: ");
- int number = Convert.ToInt32(Console.ReadLine()); // read number
- int fact, temp, sum = 0, digit, i;
- temp = number; // save the original number to temp variable.
- Console.Write("Factorial of digits: ");
- for (i = number; i > 0; i = i / 10) // divide the input number by 10
- {
- fact = 1;
- for (digit = 1; digit <= i % 10; digit++) // To get digit from number perform number % 10;
- {
- fact = fact * digit; // Calculate the factorial of given digits.
- }
- Console.Write("{0} = {1}", digit - 1, fact + ", "); // print the factorial fo digits
- sum = sum + fact; // Add the factorial of digits.
- }
- Console.WriteLine("");
- Console.Write("sum of factorial of digits: " + sum);
- Console.WriteLine("");
- if (sum == temp) // check sum of factorial of digits is equal to input number.
- {
- Console.Write(temp + " is a Strong number"); // If yes, then input number is strong number
- }
- else
- {
- Console.Write(temp + " is a Strong number");// else it is not strong number.
- }
- }
Output

Pascal's Triangle
The Pascal's triangle is a triangular array of binomial coefficients.
- // print pascal triangle for given range
- public void PascalTriangle()
- {
- // 1
- // 1 1
- // 1 2 1
- Console.Write("Enter the number of rows: ");
- int number = Convert.ToInt32(Console.ReadLine()); // read number of rows
- int space, rows, cols, pasDigit = 1;
- for (rows = 0; rows < number; rows++) // loop each rows
- {
- for (space = 1; space < number - rows; space++) // this loop for printing space number - rows;
- {
- Console.Write(" ");
- }
- for (cols = 0; cols <= rows; cols++) // loops each cols
- {
- if (cols == 0 || rows == 0) // check rows or cols value is 0.
- {
- pasDigit = 1; // if yes pasDigit will be 1;
- }
- else
- {
- pasDigit = pasDigit * (rows - cols + 1) / cols; // else pasDigit = pasDigit * (row -cols + 1) / cols;
- }
- Console.Write(pasDigit + " ");
- }
- Console.WriteLine("");
- }
- }
Output

Armstrong Number
The sum of the cube of the digit of input number equal to the input number is called an Armstrong number.







Chaman GautamPosted Apr 12, 2020, 11:28 PM
Nice article sir