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
-
- public void GPSeries()
- {
-
-
-
-
-
-
- double gProgess = 0, sum = 0;
- Console.Write("Enter the first number: ");
- int firstNum = Convert.ToInt32(Console.ReadLine());
-
- Console.Write("Enter the number or terms: ");
- int nTerm = Convert.ToInt32(Console.ReadLine());
-
- Console.Write("Enter the common ratio : ");
- int ratio = Convert.ToInt32(Console.ReadLine());
-
- int firstValue = 1;
-
- Console.Write("Number for the G.P Series:");
- Console.Write(firstValue + " ");
- for (int i = 1; i <= nTerm; i++)
- {
- gProgess = Math.Pow(ratio, i);
- Console.Write("{0} ", gProgess);
- }
- sum = (firstNum * (1 - (Math.Pow(ratio, nTerm + 1)))) / (1 - ratio);
- double term = firstNum * (Math.Pow(ratio, nTerm - 1));
- Console.Write("\nThe tn terms of G.P. : {0}\n\n", term);
- Console.Write("\nThe Sum of the G.P. series : {0}\n\n", 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}
-
- public void printAPSeries()
- {
-
-
-
- Console.Write("Enter the starting number: ");
- int startNum = Convert.ToInt32(Console.ReadLine());
- Console.Write("Enter the number of items: ");
- int items = Convert.ToInt32(Console.ReadLine());
-
- Console.Write("Enter the common difference: ");
- int diff = Convert.ToInt32(Console.ReadLine());
- int sum = (items * (2 * startNum + (items - 1) * diff)) / 2;
- int lastNum = startNum + (items - 1) * diff;
- 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.
-
- public void CheckStrongNumber()
- {
-
-
-
-
-
-
- Console.Write("Enter the number: ");
- int number = Convert.ToInt32(Console.ReadLine());
- int fact, temp, sum = 0, digit, i;
- temp = number;
- Console.Write("Factorial of digits: ");
- for (i = number; i > 0; i = i / 10)
- {
- fact = 1;
- for (digit = 1; digit <= i % 10; digit++)
- {
- fact = fact * digit;
- }
- Console.Write("{0} = {1}", digit - 1, fact + ", ");
- sum = sum + fact;
- }
- Console.WriteLine("");
- Console.Write("sum of factorial of digits: " + sum);
- Console.WriteLine("");
- if (sum == temp)
- {
- Console.Write(temp + " is a Strong number");
- }
- else
- {
- Console.Write(temp + " is a Strong number");
- }
- }
Output
Pascal's Triangle
The Pascal's triangle is a triangular array of binomial coefficients.
-
- public void PascalTriangle()
- {
-
-
-
- Console.Write("Enter the number of rows: ");
- int number = Convert.ToInt32(Console.ReadLine());
- int space, rows, cols, pasDigit = 1;
- for (rows = 0; rows < number; rows++)
- {
- for (space = 1; space < number - rows; space++)
- {
- Console.Write(" ");
- }
- for (cols = 0; cols <= rows; cols++)
- {
- if (cols == 0 || rows == 0)
- {
- pasDigit = 1;
- }
- else
- {
- pasDigit = pasDigit * (rows - 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.
-
- public void ArmStrongNumber()
- {
-
-
-
-
-
- Console.Write("Enter the number: ");
- int number = Convert.ToInt32(Console.ReadLine());
- int sum = 0;
- int temp;
- int digit;
- Console.Write("Cube of digit of the enter number:");
- for (temp = number; number != 0; number = number / 10)
- {
- digit = number % 10;
- Console.Write(digit * digit * digit + " ");
- sum = sum + (digit * +digit * digit);
- }
- Console.WriteLine("");
- Console.Write("Sum of digits of enter number: " + sum);
- Console.WriteLine("");
- if (sum == temp)
- {
- Console.Write(temp + " is ArmStrong Number");
- }
- else
- {
- Console.Write(temp + " is not 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.
-
- public void PerfectNumber()
- {
-
-
-
-
-
- Console.Write("Enter the number: ");
- int number = Convert.ToInt32(Console.ReadLine());
- int sum = 0;
- Console.Write("Number is divisible by: ");
- for (int i = 1; i < number; i++)
- {
- if (number % i == 0)
- {
- Console.Write(i + " ");
- sum += i;
- }
- }
- Console.WriteLine("");
- Console.Write("Sum of perfect divisor is :" + sum);
- Console.WriteLine("");
- if (sum == number)
- {
- Console.Write(number + " is a perfect number");
- }
- else
- {
- Console.Write(number + " is not a perfect number");
- }
- }
Output
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
-
- public void NaturalNumberSquare()
- {
-
-
- Console.Write("Enter the number of natural number: ");
- int number = Convert.ToInt32(Console.ReadLine());
- int sum = 0;
- for (int i = 1; i <= number; i++)
- {
- Console.Write(" " + i * i + " ");
- sum += i * i;
- }
- 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.
-
- public void FloydTriangle()
- {
-
- Console.Write("Enter the number of rows: ");
- int number = Convert.ToInt32(Console.ReadLine());
- int a, b;
- for (int rows = 1; rows <= number; rows++)
- {
- if (rows % 2 == 0)
- {
- a = 1;
- b = 0;
- }
- else
- {
- a = 0;
- b = 1;
- }
- for (int cols = 1; cols <= rows; cols++)
- {
- if (cols % 2 == 0)
- {
- Console.Write(" " + a);
- }
- else
- {
- Console.Write(" " + b);
- }
- }
- Console.WriteLine("");
- }
- }
Output
Print the sum of series [2+ 22 + 222 + 2222 + ...]
-
- public void SumOfSeries()
- {
- Console.Write("Enter the term value: ");
- int term = Convert.ToInt32(Console.ReadLine());
- Console.Write("Enter the number of term: ");
- int num = Convert.ToInt32(Console.ReadLine());
- int sum = 0;
- int actualTerm = term;
- for (int i = 0; i < num; i++)
- {
- Console.Write(" " + term);
- sum += term;
- term = term * 10 + actualTerm;
- }
- 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.
-
- public void TrianglePatternIncreasedByOne()
- {
- Console.Write("Enter the number or rows: ");
- int number = Convert.ToInt32(Console.ReadLine());
- int rows, cols, printNum = 1;
- for (rows = 0; rows <= number; rows++)
- {
- for (cols = 0; cols < rows; cols++)
- {
- Console.Write(printNum++ + " ");
- }
- Console.WriteLine(" ");
- }
- }
Output
Print Pyramid increased by one
Print the pyramid with the number increased by 1.
-
- public void PyramidPatternIncreadByOne()
- {
- Console.Write("Enter the number or rows: ");
- int number = Convert.ToInt32(Console.ReadLine());
- int rows, cols, temp, space = number, printNum = 1;
- for (rows = 0; rows <= number; rows++)
- {
- for (cols = space; cols >= 1; cols--)
- {
- Console.Write(" ");
- }
- for (temp = 1; temp <= rows; temp++)
- {
- Console.Write(printNum++ + " ");
- }
- Console.WriteLine(" ");
- space--;
- }
- }
- }
Output
You can download the solution from the attachment.