Introduction
This article will help you learn and write code on some famous number series. For demonstration purposes, I used Visual Studio Code for the C# code.
To set up the environment for C# in Visual Studio Code, refer here.
Check even or odd number
- public void OddEvenNumber()
- {
- // Number which is divisible by 2 is known as Even number
- // Number which is not divisible by 2 is known as Odd number
- Console.Write("Enter the number: ");
- int number = Convert.ToInt32(Console.ReadLine());
- if (number % 2 == 0) // check number after division is zero
- {
- Console.Write(number + " is Even Number"); // If divisible by number
- }
- else
- {
- Console.Write(number + " is Odd number"); // if not divisble by number
- }
- }
Output

Check Prime Number
- public void PrimeNumber()
- {
- // Prime numbers are numbers divisible by 1 and itself.
- Console.Write("Enter the number: ");
- int number = Convert.ToInt32(Console.ReadLine());
- int i, m = 0, count = 0;
- m = number / 2;
- for (i = 2; i <= m; i++) // check number is divisible by any another number
- {
- if (number % i == 0) // If number is divisible by another number
- {
- count = 1; // reassign the count to 1
- break; // break the for loop
- }
- }
- if (number > 1 && count == 0) // number should be greater than 1.
- {
- Console.Write(number + " is prime number");
- }
- else
- {
- Console.Write(number + " is non prime number");
- }
- }
Output

Print prime number from 1 to 100
- public void PrintPrimeNumber1To100()
- {
- // Prime number are the number which is divisible by 1 or itself.
- bool isPrimeNumber = true; // check number is prime number
- int outerloop, innerloop; // declare to
- Console.WriteLine("Prime Numbers are : ");
- for (outerloop = 2; outerloop <= 100; outerloop++)
- {
- for (innerloop= 2; innerloop <= 100; innerloop++)
- {
- if (outerloop != innerloop && outerloop % innerloop== 0)
- {
- isPrimeNumber = false;
- break;
- }
- }
- if (isPrimeNumber) // If number is prime number print them on console.
- {
- Console.Write("\t" + outerloop);
- }
- isPrimeNumber = true;
- }
- Console.ReadLine();
- }
Output

Print factorial of input number
- public void Factorial()
- {
- // Factorial of number 4 is: 4*3*2*1 = 24
- int num, factorial = 1;
- Console.Write("Enter the number: ");
- num = Convert.ToInt32(Console.ReadLine());
- while (num >= 1) // check number is greater or equal to 1
- {
- factorial = factorial * num; // multiply the number
- num--; // decrement the number by 1
- }
- Console.Write("Factorial: " + factorial);
- }
Print the reverse number of input number
- public void ReverseNumber()
- {
- // ReverseNumber
- int originalNumber, reverseNumber = 0;
- Console.Write("Enter the number: ");
- originalNumber = Convert.ToInt32(Console.ReadLine());
- while (originalNumber >= 1)
- {
- reverseNumber = reverseNumber * 10 + originalNumber % 10; // multiply the reverseNumber with 10 and modulus on orginal number
- originalNumber = originalNumber / 10;
- }
- Console.Write("Reverse Number: " + reverseNumber);
- }
Output
Check if input number is palidrome or not
- public void CheckPalidromeNumber()
- {
- // Palidrome number is number that remains same when its digit are reversed.
- int numb, temp = 0, orginalNumb;
- Console.Write("Enter the number: ");
- numb = Convert.ToInt32(Console.ReadLine());
- orginalNumb = numb; // store the entered number into original number
- while (numb >= 1)
- {
- temp = temp * 10 + numb % 10;
- numb = numb / 10;
- }
- if (temp == orginalNumb) // check orginal number and temp are same.
- {
- Console.Write("Palidrome Number");
- }
- else
- {
- Console.Write("Not a Palidrome Number");
- }
- }
Output

Addition of input numbers
- public void SumOfNumber()
- {
- // sum of number => adding all the digits
- int num, sum = 0;
- Console.Write("Enter the number: ");
- num = Convert.ToInt32(Console.ReadLine());
- while (num >= 1)
- {
- sum = sum + num % 10;
- num = num / 10;
- }
- Console.Write("Sum of Number: " + sum);
- }
Output
Print the greatest common divisor (GCD) between two numbers
- public void GCDNumber()
- {
- // GCD Number are known as Greatest Common Divisor
- // number which is the largest number that divides both number
- // Example
- // 3 = 3
- // 6 = 2 * 3
- // So the GCD of 3 and 6 is: 3
- int num1, num2;
- Console.Write("Enter the First number: ");
- num1 = Convert.ToInt32(Console.ReadLine());
- Console.Write("Enter the second number: ");
- num2 = Convert.ToInt32(Console.ReadLine());
- while (num1 != num2) // numb1 not equal to num2
- {
- if (num1 > num2) // check num1 is greater than num2
- {
- num1 = num1 - num2; // subtract num2 form num1
- }
- else
- {
- num2 = num2 - num1; // else subtract num1 from num2
- }
- }
- if (num1 == 1)
- {
- Console.Write("No GCD Number");
- }
- else
- {
- Console.Write("GCD Number: " + num1);
- }
- }
Output

Print the least common multiplier (LCM) between two numbers
- public void LCMNumber()
- {
- // LCM Number are Least Common Multiplication
- // LCM of two number is the smallest number (not zero) that is a multiple of both.
- // 6 = 2 * 3
- // 8 = 2 * 2 * 2
- // So the GCD of 6 and 8 is: 24
- int num1, num2, a, b, lcmNum;
- Console.Write("Enter the First number: ");
- num1 = Convert.ToInt32(Console.ReadLine());
- Console.Write("Enter the second number: ");
- num2 = Convert.ToInt32(Console.ReadLine());
- a = num2;
- b = num1;
- while (num1 != num2) // check num1 is equal or not
- {
- if (num1 > num2) // check num1 is greater than num2
- {
- num1 = num1 - num2; // subtract num2 from num1 and save in num1
- }
- else
- {
- num2 = num2 - num1; // subtract num1 from num2 and save in num2
- }
- }
- lcmNum = (a * b) / num1; // multiply the orinal two number and divide with num1
- Console.Write("LCM number: " + lcmNum);
- }
Output
Print table from 1 to 10
- public void print1To10Table()
- {
- // Print table form 1 to 10
- for (int num1 = 1; num1 <= 10; num1++)
- {
- for (int num2 = 1; num2 <= 10; num2++)
- {
- Console.Write(num1 * num2 + "\t");
- }
- Console.WriteLine("");
- }
- }
Output
Fibonacci Series of input numbers
- public void FibonacciNumber()
- {
- // Fibonacci Series till number 10 is: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34
- int num, firstNum = 0, secondNum = 1, temp, thirdNum = 3;
- Console.Write("Enter the number: ");
- num = Convert.ToInt32(Console.ReadLine());
- Console.Write(firstNum + "\t" + secondNum); // print the first two number i.e 1 2
- while (thirdNum <= num) // check enter number is less than third number i.e 3
- {
- temp = firstNum + secondNum; // add firstNum and secondNum and stored in temp
- Console.Write("\t" + temp); // print the third numb
- firstNum = secondNum; // assing secondNumb to firstNum
- secondNum = temp; // assing temp number to secondNum
- thirdNum = thirdNum + 1; // Increment the thirdNum
- }
- }
Output
Swap numbers using a third variable
- public void SwapNumber()
- {
- // Swap Number
- // Orginal Number: x = 2 and y = 3
- // After Swapping: x = 3 and y = 2
- // This method will swap number using temp variable
- int num1, num2, temp;
- Console.Write("Enter the First number: ");
- num1 = Convert.ToInt32(Console.ReadLine());
- Console.Write("Enter the second number: ");
- num2 = Convert.ToInt32(Console.ReadLine());
- Console.WriteLine("Number Before Swap x: " + num1 + " y: " + num2);
- temp = num2; // assign the second number to temp
- num2 = num1; // assign the first number to second number
- num1 = temp; // assign temp to second number
- Console.WriteLine("Number After Swap x: " + num1 + " y: " + num2);
- }
Output
Swap number without using a third variable
- public void SwapNumberWithoutThirdVariable()
- {
- int num1, num2;
- Console.Write("Enter the First number: ");
- num1 = Convert.ToInt32(Console.ReadLine());
- Console.Write("Enter the second number: ");
- num2 = Convert.ToInt32(Console.ReadLine());
- Console.WriteLine("Number Before Swap x: " + num1 + " y: " + num2);
- num1 = num1 + num2; // add num1 and num2
- num2 = num1 - num2; // subtract num2 from num1 and assign to num2
- num1 = num1 - num2; // subtract num2 form num1 and assign to num1
- Console.WriteLine("Number After Swap x: " + num1 + " y: " + num2);
- }
Output
Print tranpose of a 3 by 3 matrix
- public void Print3By3MatrixTranspose()
- {
- // The transpose of a matrix is a new matrix whose rows are the columns of the original.
- int[,] pos = new int[3, 3]; // declare multidimensional array which accepts 3 rows by 3 columns
- int rows, cols; // initialize rows and columns
- Console.WriteLine("Enter value for Matrix:");
- for (rows = 0; rows < 3; rows++)
- {
- for (cols = 0; cols < 3; cols++)
- {
- pos[rows, cols] = Convert.ToInt32(Console.ReadLine()); // read the 9 digit in the matrix
- }
- }
- Console.WriteLine("Original Matrix:");
- for (rows = 0; rows < 3; rows++)
- {
- for (cols = 0; cols < 3; cols++)
- {
- Console.Write(pos[rows, cols] + "\t"); // print the original matrix
- }
- Console.WriteLine("");
- }
- Console.WriteLine("Transpose of Matrix:");
- for (rows = 0; rows < 3; rows++)
- {
- for (cols = 0; cols < 3; cols++)
- {
- Console.Write(pos[cols, rows] + "\t"); // change the row to column
- }
- Console.WriteLine("");
- }
- }
Output

Matrix multiplication between two 3 by 3 matrices
- public void Print3By3MatrixMultiplication()
- {
- /*
- * Step 1: Make sure that the the number of columns in the 1st one equals the number of rows in the 2nd one. (The pre-requisite to be able to multiply)
- * Step 2: Multiply the elements of each row of the first matrix by the elements of each column in the second matrix.
- * Step 3: Add the products.
- *
- * Example: Below matrix will be denoted as per following symbols
- *
- * FirstMatrix = fm
- * SecondMatrix = sm
- * ResultMatrix = rm
- *
- * rm[0,0] = fm[0,0]*sm[0,0] + fm[0,1]*sm[1,0] + fm[0,3]*sm[2,0]
- * rm[0,1] = fm[0,0]*sm[0,1] + fm[0,1]*sm[1,1] + fm[0,0]*sm[2,1]
- * rm[0,2] = fm[0,0]*sm[0,2] + fm[0,1]*sm[1,2] + fm[0,0]*sm[2,2]
- * *
- * *
- * *
- * rm[2,2] = fm[2,0]*sm[0,2] + fm[2,1]*sm[1,2] + fm[2,2]*sm[2,2]
- */
- // declare 3 multidimensional array
- int[,] fm = new int[3, 3];
- int[,] sm = new int[3, 3];
- int[,] rm = new int[3, 3];
- int rows, cols, temp, rowTemp;
- Console.WriteLine("Enter value for Matrix 1: ");
- for (rows = 0; rows < 3; rows++)
- {
- for (cols = 0; cols < 3; cols++)
- {
- fm[rows, cols] = Convert.ToInt32(Console.ReadLine()); // Read first matrix value and store into fm array
- }
- }
- Console.WriteLine("Enter value for Matrix 2: ");
- for (rows = 0; rows < 3; rows++)
- {
- for (cols = 0; cols < 3; cols++)
- {
- sm[rows, cols] = Convert.ToInt32(Console.ReadLine()); // Read secnd matrix value and store into sm
- }
- }
- Console.WriteLine("Original Matrix 1:");
- for (rows = 0; rows < 3; rows++)
- {
- for (cols = 0; cols < 3; cols++)
- {
- Console.Write(fm[rows, cols] + "\t"); // Print first matrix
- }
- Console.WriteLine("");
- }
- Console.WriteLine("Original Matrix 2:");
- for (rows = 0; rows < 3; rows++)
- {
- for (cols = 0; cols < 3; cols++)
- {
- Console.Write(sm[rows, cols] + "\t"); // print second matrix
- }
- Console.WriteLine("");
- }
- for (rows = 0; rows < 3; rows++)
- {
- for (cols = 0; cols < 3; cols++)
- {
- for (rowTemp = 0, temp = 0; temp < 3; temp++)
- {
- rowTemp = rowTemp + fm[rows, temp] * sm[temp, cols]; // fm[0,0]*sm[0,0] + fm[0,1]*sm[1,0] + fm[0,3]*sm[2,0]
- }
- rm[rows, cols] = rowTemp;// rm[0,0] = rowTemp
- }
- }
- Console.WriteLine("Multiplication of 2 Matrix is:");
- for (rows = 0; rows < 3; rows++)
- {
- for (cols = 0; cols < 3; cols++)
- {
- Console.Write(rm[rows, cols] + "\t"); // Print the result matrix.
- }
- Console.WriteLine("");
- }
- }
Output

Triangle number pyramid
Print the number pyramid which looks like following,
1
2 2
3 3 3
- public void TrianglePyramidNumber()
- {
- // print the Triangle pyaramid for 9 digit
- int rows, cols, temp, space = 8;
- for (rows = 1; rows <= 9; rows++)
- {
- for (cols = space; cols >= 1; cols--)
- {
- Console.Write(" "); // print space
- }
- for (temp = 1; temp <= rows; temp++)
- {
- Console.Write(rows + " "); // print rows value
- }
- Console.WriteLine("");
- space--;
- }
- }
Output
Print the number pyramid where the numbers start from the left side.
- public void leftPyramidNumber()
- {
- // Start the pyramid from left side.
- int rows, cols;
- for (rows = 1; rows <= 9; rows++)
- {
- for (cols = 1; cols <= rows; cols++)
- {
- Console.Write(rows + " "); // print rows value
- }
- Console.WriteLine("");
- }
- }
Output

Print the number pyramid where numbers start from the right side.
- public void RightPyramidNumber()
- {
- // start the pyramid from right side
- int rows, cols;
- for (rows = 1; rows <= 9; rows++)
- {
- for (cols = 1; cols <= 9 - rows; cols++)
- {
- Console.Write("\t"); // print number of space row - 1 = 9-1 => 8
- }
- for (cols = 1; cols <= rows; cols++)
- {
- Console.Write(rows + "\t"); // print rows value.
- }
- Console.WriteLine("");
- }
- }
Output

Find the source code as an attachment.

Join the conversation! Your thoughts help the community grow.