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.
- //check Armstrong number
- public void ArmStrongNumber()
- {
- // sum of the cube of digit of input number equal to input number is called armstrong number.
- // Suppose the input number is 153
- // Cube of digit number 1 = 1, 5 = 125 , 3 = 27
- // Sum of cube = 1 + 125 + 27 = 153
- // Hence the input number 153 is armstrong number.
- Console.Write("Enter the number: ");
- int number = Convert.ToInt32(Console.ReadLine()); // read the number
- int sum = 0; // initialize with 0
- int temp;
- int digit;
- Console.Write("Cube of digit of the enter number:");
- for (temp = number; number != 0; number = number / 10) // divide the number by 10
- {
- digit = number % 10; // find the digit for given number
- Console.Write(digit * digit * digit + " "); // print the cube of digit
- sum = sum + (digit * +digit * digit); // Add and store the cube of digit
- }
- Console.WriteLine("");
- Console.Write("Sum of digits of enter number: " + sum); // print the sum of armstrong number
- Console.WriteLine("");
- if (sum == temp) // check given number and sum of armstrong number is equal
- {
- Console.Write(temp + " is ArmStrong Number"); // If yes then it is armstrong number
- }
- else
- {
- Console.Write(temp + " is not ArmStrong Number"); // else it is not a armstrong number
- }
- }
Output

Perfect Number
A perfect number is a positive integer that is equal to the sum of its positive divisors, excluding the number itself.
- // check perfect number or not
- public void PerfectNumber()
- {
- // A perfect number is a positive integer that is equal to the sum of its positive divisors, excluding the number itself
- // Suppose Input number is 28
- // Positive divisor excluding 28 is = 1 2 4 7 14
- // Sum of Positive divisor = 1 + 2 + 4 + 7 + 14 = 28
- // Hence 28 is a perfect number.
- Console.Write("Enter the number: ");
- int number = Convert.ToInt32(Console.ReadLine()); // read the number
- int sum = 0; // initialize with 0
- Console.Write("Number is divisible by: ");
- for (int i = 1; i < number; i++) // check number less than given number
- {
- if (number % i == 0) // check given number is divisible by how many number.
- {
- Console.Write(i + " "); // print the perfect divisor
- sum += i; // add number which divide the given number
- }
- }
- Console.WriteLine("");
- Console.Write("Sum of perfect divisor is :" + sum);
- Console.WriteLine("");
- if (sum == number)// check sum and original number.
- {
- Console.Write(number + " is a perfect number");
- }
- else
- {
- Console.Write(number + " is not a perfect number");
- }
- }
Output

Natural Number
The natural numbers are a part of the number system which includes all the positive integers from 1 to infinity.
It is always greater than zero
- //Print the n terms of square natural number and their sum without function.
- public void NaturalNumberSquare()
- { // Natural numbers are a part of the number system which includes all the positive integers from 1 till infinity.
- // It is always greater than zero
- // Example of natural number is 1, 2, 3........infinity.
- Console.Write("Enter the number of natural number: ");
- int number = Convert.ToInt32(Console.ReadLine()); // read n number of natural number
- int sum = 0; // initialize sum value
- for (int i = 1; i <= number; i++) // loop each natural number
- {
- Console.Write(" " + i * i + " "); // print the square of natural number
- sum += i * i; // Add the square of natural number
- }
- Console.WriteLine("");
- Console.Write("Sum of square of natural number upto {0} term: {1}", number, sum);
- }
Output

Print Floyd's Triangle
Floyd's triangle is a right-angled triangular array of natural numbers.
- //print the Floyd's Triangle.
- public void FloydTriangle()
- {
- // Floyd's triangle is a right-angled triangular array of natural numbers
- Console.Write("Enter the number of rows: ");
- int number = Convert.ToInt32(Console.ReadLine()); // read number of rows
- int a, b; // declare two temperary variable
- for (int rows = 1; rows <= number; rows++)
- {
- if (rows % 2 == 0) // check number is divisible by 2. If divisible set a=1 and b=0
- {
- a = 1;
- b = 0;
- }
- else // else reverse the value of a and b.
- {
- a = 0;
- b = 1;
- }
- for (int cols = 1; cols <= rows; cols++) // loops to handle each cols value
- {
- if (cols % 2 == 0) // check number is divisible by 2
- {
- Console.Write(" " + a); // if yes print a value
- }
- else // else b value
- {
- Console.Write(" " + b);
- }
- }
- Console.WriteLine("");
- }
- }
Output

Sum of Series
Print the sum of series [2+ 22 + 222 + 2222 + ...]
- // Print the sum of series [ 2 + 22 + 222 + 2222 + 22222 ...].
- public void SumOfSeries()
- {
- Console.Write("Enter the term value: ");
- int term = Convert.ToInt32(Console.ReadLine()); // read the term value
- Console.Write("Enter the number of term: ");
- int num = Convert.ToInt32(Console.ReadLine()); // read the number of term
- int sum = 0;
- int actualTerm = term;
- for (int i = 0; i < num; i++) // loop the number of term
- {
- Console.Write(" " + term); // print the term
- sum += term; // add the term and store into variable sum
- term = term * 10 + actualTerm; // miltiply the term wih 10 and add the term i.e 2 2*10+2 22*10+2 222*10+2
- }
- Console.WriteLine("");
- Console.Write("Sum of series: " + sum);
- }
Output

Print Pattern increased by one
Print the pattern like a right-angle triangle, with the number increased by 1.
- //Print pattern like right angle triangle with number increased by 1
- public void TrianglePatternIncreasedByOne()
- {
- Console.Write("Enter the number or rows: ");
- int number = Convert.ToInt32(Console.ReadLine()); // read number of rows
- int rows, cols, printNum = 1;
- for (rows = 0; rows <= number; rows++) // this loops will handle the rows condition
- {
- for (cols = 0; cols < rows; cols++) // this loops will handle the column condition
- {
- Console.Write(printNum++ + " "); // print the number and increment it by 1
- }
- Console.WriteLine(" ");
- }
- }
Output

Print Pyramid increased by one
Print the pyramid with the number increased by 1.
- // print pattern like pyramid with number increased by 1
- public void PyramidPatternIncreadByOne()
- {
- Console.Write("Enter the number or rows: ");
- int number = Convert.ToInt32(Console.ReadLine()); // read number of rows
- int rows, cols, temp, space = number, printNum = 1;
- for (rows = 0; rows <= number; rows++)
- {
- for (cols = space; cols >= 1; cols--) // print spaces
- {
- Console.Write(" ");
- }
- for (temp = 1; temp <= rows; temp++) // print number of value in rows
- {
- Console.Write(printNum++ + " ");
- }
- Console.WriteLine(" "); // enter new line and print space
- space--; // decrement the space by one
- }
- }
- }
Output
You can download the solution from the attachment.

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