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
  1. //print the sum of Geometric Progresson
  2. public void GPSeries()
  3. {
  4. // 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) .
  5. // 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.
  6. // suppose first number = 1, terms = 5, common ratio = 2
  7. // Number of G.P Series are 1, 2, 4, 8, 16, 32
  8. // The sum of the G.P series: 32 {a, ar, ar2, ar3, ... } where a is first number and r is common ration
  9. double gProgess = 0, sum = 0;
  10. Console.Write("Enter the first number: ");
  11. int firstNum = Convert.ToInt32(Console.ReadLine()); // read the first number and save into firstNum variable
  12. Console.Write("Enter the number or terms: ");
  13. int nTerm = Convert.ToInt32(Console.ReadLine()); // read the terms
  14. Console.Write("Enter the common ratio : ");
  15. int ratio = Convert.ToInt32(Console.ReadLine()); // read the common ratio
  16. int firstValue = 1; // initialize the first value
  17. Console.Write("Number for the G.P Series:");
  18. Console.Write(firstValue + " "); // print first value.
  19. for (int i = 1; i <= nTerm; i++) // loop till nTerm.
  20. {
  21. gProgess = Math.Pow(ratio, i); // find the g.p progression
  22. Console.Write("{0} ", gProgess); // print the progression
  23. }
  24. sum = (firstNum * (1 - (Math.Pow(ratio, nTerm + 1)))) / (1 - ratio); // calculate the sum and save into variable sum.
  25. double term = firstNum * (Math.Pow(ratio, nTerm - 1)); // calculate the term and store into variable term.
  26. Console.Write("\nThe tn terms of G.P. : {0}\n\n", term); // print the term.
  27. Console.Write("\nThe Sum of the G.P. series : {0}\n\n", sum);// print the sum.
  28. }
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}
  1. // Print sum of Arithmetic Progression(A.P) series
  2. public void printAPSeries()
  3. {
  4. //An arithmetic progression (AP) or arithmetic sequence is a sequence of numbers such that the difference between the consecutive terms is constant.
  5. // A.P series with common difference of 3 for 10 items is {1 , 4, 7, 10, 13, 16, 19 , 22, 25, 28}
  6. // Sum of Above series is = 145
  7. Console.Write("Enter the starting number: ");
  8. int startNum = Convert.ToInt32(Console.ReadLine()); // read items
  9. Console.Write("Enter the number of items: ");
  10. int items = Convert.ToInt32(Console.ReadLine()); // read items
  11. Console.Write("Enter the common difference: ");
  12. int diff = Convert.ToInt32(Console.ReadLine()); // read difference number
  13. int sum = (items * (2 * startNum + (items - 1) * diff)) / 2; // calculate the sum
  14. int lastNum = startNum + (items - 1) * diff; // calculate the new last number
  15. Console.Write("\nThe Sum of the A.P. series are : \n");
  16. for (int i = startNum; i <= lastNum; i = i + diff)
  17. {
  18. if (i != lastNum)
  19. {
  20. Console.Write("{0} + ", i);
  21. }
  22. else
  23. {
  24. Console.Write("{0} = {1} \n\n", i, sum);
  25. }
  26. }
  27. }
Output

Strong Number

A strong number is a special number whose sum of the factorial of digits is equal to the original number.
  1. // check whether a number is Strong Number or not.
  2. public void CheckStrongNumber()
  3. {
  4. // Strong number is a special number whose sum of factorial of digits is equal to the original number.
  5. // Suppose input number is 145
  6. // Factorial of digita of input numbers is 1 = 1, 4 = 24 , 5 = 120
  7. // Sum of factorial number is: 1 + 24 + 120 = 145
  8. // Hence the input number 145 is a armstrong number.
  9. Console.Write("Enter the number: ");
  10. int number = Convert.ToInt32(Console.ReadLine()); // read number
  11. int fact, temp, sum = 0, digit, i;
  12. temp = number; // save the original number to temp variable.
  13. Console.Write("Factorial of digits: ");
  14. for (i = number; i > 0; i = i / 10) // divide the input number by 10
  15. {
  16. fact = 1;
  17. for (digit = 1; digit <= i % 10; digit++) // To get digit from number perform number % 10;
  18. {
  19. fact = fact * digit; // Calculate the factorial of given digits.
  20. }
  21. Console.Write("{0} = {1}", digit - 1, fact + ", "); // print the factorial fo digits
  22. sum = sum + fact; // Add the factorial of digits.
  23. }
  24. Console.WriteLine("");
  25. Console.Write("sum of factorial of digits: " + sum);
  26. Console.WriteLine("");
  27. if (sum == temp) // check sum of factorial of digits is equal to input number.
  28. {
  29. Console.Write(temp + " is a Strong number"); // If yes, then input number is strong number
  30. }
  31. else
  32. {
  33. Console.Write(temp + " is a Strong number");// else it is not strong number.
  34. }
  35. }
Output

Pascal's Triangle

The Pascal's triangle is a triangular array of binomial coefficients.
  1. // print pascal triangle for given range
  2. public void PascalTriangle()
  3. {
  4. // 1
  5. // 1 1
  6. // 1 2 1
  7. Console.Write("Enter the number of rows: ");
  8. int number = Convert.ToInt32(Console.ReadLine()); // read number of rows
  9. int space, rows, cols, pasDigit = 1;
  10. for (rows = 0; rows < number; rows++) // loop each rows
  11. {
  12. for (space = 1; space < number - rows; space++) // this loop for printing space number - rows;
  13. {
  14. Console.Write(" ");
  15. }
  16. for (cols = 0; cols <= rows; cols++) // loops each cols
  17. {
  18. if (cols == 0 || rows == 0) // check rows or cols value is 0.
  19. {
  20. pasDigit = 1; // if yes pasDigit will be 1;
  21. }
  22. else
  23. {
  24. pasDigit = pasDigit * (rows - cols + 1) / cols; // else pasDigit = pasDigit * (row -cols + 1) / cols;
  25. }
  26. Console.Write(pasDigit + " ");
  27. }
  28. Console.WriteLine("");
  29. }
  30. }
Output

Armstrong Number

The sum of the cube of the digit of input number equal to the input number is called an Armstrong number.
  1. //check Armstrong number
  2. public void ArmStrongNumber()
  3. {
  4. // sum of the cube of digit of input number equal to input number is called armstrong number.
  5. // Suppose the input number is 153
  6. // Cube of digit number 1 = 1, 5 = 125 , 3 = 27
  7. // Sum of cube = 1 + 125 + 27 = 153
  8. // Hence the input number 153 is armstrong number.
  9. Console.Write("Enter the number: ");
  10. int number = Convert.ToInt32(Console.ReadLine()); // read the number
  11. int sum = 0; // initialize with 0
  12. int temp;
  13. int digit;
  14. Console.Write("Cube of digit of the enter number:");
  15. for (temp = number; number != 0; number = number / 10) // divide the number by 10
  16. {
  17. digit = number % 10; // find the digit for given number
  18. Console.Write(digit * digit * digit + " "); // print the cube of digit
  19. sum = sum + (digit * +digit * digit); // Add and store the cube of digit
  20. }
  21. Console.WriteLine("");
  22. Console.Write("Sum of digits of enter number: " + sum); // print the sum of armstrong number
  23. Console.WriteLine("");
  24. if (sum == temp) // check given number and sum of armstrong number is equal
  25. {
  26. Console.Write(temp + " is ArmStrong Number"); // If yes then it is armstrong number
  27. }
  28. else
  29. {
  30. Console.Write(temp + " is not ArmStrong Number"); // else it is not a armstrong number
  31. }
  32. }
Output

Perfect Number

A perfect number is a positive integer that is equal to the sum of its positive divisors, excluding the number itself.
  1. // check perfect number or not
  2. public void PerfectNumber()
  3. {
  4. // A perfect number is a positive integer that is equal to the sum of its positive divisors, excluding the number itself
  5. // Suppose Input number is 28
  6. // Positive divisor excluding 28 is = 1 2 4 7 14
  7. // Sum of Positive divisor = 1 + 2 + 4 + 7 + 14 = 28
  8. // Hence 28 is a perfect number.
  9. Console.Write("Enter the number: ");
  10. int number = Convert.ToInt32(Console.ReadLine()); // read the number
  11. int sum = 0; // initialize with 0
  12. Console.Write("Number is divisible by: ");
  13. for (int i = 1; i < number; i++) // check number less than given number
  14. {
  15. if (number % i == 0) // check given number is divisible by how many number.
  16. {
  17. Console.Write(i + " "); // print the perfect divisor
  18. sum += i; // add number which divide the given number
  19. }
  20. }
  21. Console.WriteLine("");
  22. Console.Write("Sum of perfect divisor is :" + sum);
  23. Console.WriteLine("");
  24. if (sum == number)// check sum and original number.
  25. {
  26. Console.Write(number + " is a perfect number");
  27. }
  28. else
  29. {
  30. Console.Write(number + " is not a perfect number");
  31. }
  32. }
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
  1. //Print the n terms of square natural number and their sum without function.
  2. public void NaturalNumberSquare()
  3. { // Natural numbers are a part of the number system which includes all the positive integers from 1 till infinity.
  4. // It is always greater than zero
  5. // Example of natural number is 1, 2, 3........infinity.
  6. Console.Write("Enter the number of natural number: ");
  7. int number = Convert.ToInt32(Console.ReadLine()); // read n number of natural number
  8. int sum = 0; // initialize sum value
  9. for (int i = 1; i <= number; i++) // loop each natural number
  10. {
  11. Console.Write(" " + i * i + " "); // print the square of natural number
  12. sum += i * i; // Add the square of natural number
  13. }
  14. Console.WriteLine("");
  15. Console.Write("Sum of square of natural number upto {0} term: {1}", number, sum);
  16. }
Output

Print Floyd's Triangle

Floyd's triangle is a right-angled triangular array of natural numbers.
  1. //print the Floyd's Triangle.
  2. public void FloydTriangle()
  3. {
  4. // Floyd's triangle is a right-angled triangular array of natural numbers
  5. Console.Write("Enter the number of rows: ");
  6. int number = Convert.ToInt32(Console.ReadLine()); // read number of rows
  7. int a, b; // declare two temperary variable
  8. for (int rows = 1; rows <= number; rows++)
  9. {
  10. if (rows % 2 == 0) // check number is divisible by 2. If divisible set a=1 and b=0
  11. {
  12. a = 1;
  13. b = 0;
  14. }
  15. else // else reverse the value of a and b.
  16. {
  17. a = 0;
  18. b = 1;
  19. }
  20. for (int cols = 1; cols <= rows; cols++) // loops to handle each cols value
  21. {
  22. if (cols % 2 == 0) // check number is divisible by 2
  23. {
  24. Console.Write(" " + a); // if yes print a value
  25. }
  26. else // else b value
  27. {
  28. Console.Write(" " + b);
  29. }
  30. }
  31. Console.WriteLine("");
  32. }
  33. }
Output

Sum of Series

Print the sum of series [2+ 22 + 222 + 2222 + ...]
  1. // Print the sum of series [ 2 + 22 + 222 + 2222 + 22222 ...].
  2. public void SumOfSeries()
  3. {
  4. Console.Write("Enter the term value: ");
  5. int term = Convert.ToInt32(Console.ReadLine()); // read the term value
  6. Console.Write("Enter the number of term: ");
  7. int num = Convert.ToInt32(Console.ReadLine()); // read the number of term
  8. int sum = 0;
  9. int actualTerm = term;
  10. for (int i = 0; i < num; i++) // loop the number of term
  11. {
  12. Console.Write(" " + term); // print the term
  13. sum += term; // add the term and store into variable sum
  14. 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
  15. }
  16. Console.WriteLine("");
  17. Console.Write("Sum of series: " + sum);
  18. }

Output

Print Pattern increased by one

Print the pattern like a right-angle triangle, with the number increased by 1.
  1. //Print pattern like right angle triangle with number increased by 1
  2. public void TrianglePatternIncreasedByOne()
  3. {
  4. Console.Write("Enter the number or rows: ");
  5. int number = Convert.ToInt32(Console.ReadLine()); // read number of rows
  6. int rows, cols, printNum = 1;
  7. for (rows = 0; rows <= number; rows++) // this loops will handle the rows condition
  8. {
  9. for (cols = 0; cols < rows; cols++) // this loops will handle the column condition
  10. {
  11. Console.Write(printNum++ + " "); // print the number and increment it by 1
  12. }
  13. Console.WriteLine(" ");
  14. }
  15. }
Output

Print Pyramid increased by one

Print the pyramid with the number increased by 1.
  1. // print pattern like pyramid with number increased by 1
  2. public void PyramidPatternIncreadByOne()
  3. {
  4. Console.Write("Enter the number or rows: ");
  5. int number = Convert.ToInt32(Console.ReadLine()); // read number of rows
  6. int rows, cols, temp, space = number, printNum = 1;
  7. for (rows = 0; rows <= number; rows++)
  8. {
  9. for (cols = space; cols >= 1; cols--) // print spaces
  10. {
  11. Console.Write(" ");
  12. }
  13. for (temp = 1; temp <= rows; temp++) // print number of value in rows
  14. {
  15. Console.Write(printNum++ + " ");
  16. }
  17. Console.WriteLine(" "); // enter new line and print space
  18. space--; // decrement the space by one
  19. }
  20. }
  21. }
Output
You can download the solution from the attachment.