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()
- {
-
-
- Console.Write("Enter the number: ");
- int number = Convert.ToInt32(Console.ReadLine());
- if (number % 2 == 0)
- {
- Console.Write(number + " is Even Number");
- }
- else
- {
- Console.Write(number + " is Odd number");
- }
- }
Output
Check Prime Number
- public void PrimeNumber()
- {
-
- 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++)
- {
- if (number % i == 0)
- {
- count = 1;
- break;
- }
- }
- if (number > 1 && count == 0)
- {
- 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()
- {
-
- bool isPrimeNumber = true;
- int outerloop, innerloop;
- 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)
- {
- Console.Write("\t" + outerloop);
- }
- isPrimeNumber = true;
- }
- Console.ReadLine();
- }
Output
Print factorial of input number
- public void Factorial()
- {
-
- int num, factorial = 1;
- Console.Write("Enter the number: ");
- num = Convert.ToInt32(Console.ReadLine());
- while (num >= 1)
- {
- factorial = factorial * num;
- num--;
- }
- Console.Write("Factorial: " + factorial);
- }
Output
Print the reverse number of input number
- public void ReverseNumber()
- {
-
- int originalNumber, reverseNumber = 0;
- Console.Write("Enter the number: ");
- originalNumber = Convert.ToInt32(Console.ReadLine());
- while (originalNumber >= 1)
- {
- reverseNumber = reverseNumber * 10 + originalNumber % 10;
- originalNumber = originalNumber / 10;
- }
- Console.Write("Reverse Number: " + reverseNumber);
- }
Output
Check if input number is palidrome or not
- public void CheckPalidromeNumber()
- {
-
- int numb, temp = 0, orginalNumb;
- Console.Write("Enter the number: ");
- numb = Convert.ToInt32(Console.ReadLine());
- orginalNumb = numb;
- while (numb >= 1)
- {
- temp = temp * 10 + numb % 10;
- numb = numb / 10;
- }
- if (temp == orginalNumb)
- {
- Console.Write("Palidrome Number");
- }
- else
- {
- Console.Write("Not a Palidrome Number");
- }
- }
Output
Addition of input numbers
- public void SumOfNumber()
- {
-
- 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()
- {
-
-
-
-
-
-
- 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)
- {
- if (num1 > num2)
- {
- num1 = num1 - num2;
- }
- else
- {
- num2 = num2 - num1;
- }
- }
- 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()
- {
-
-
-
-
-
- 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)
- {
- if (num1 > num2)
- {
- num1 = num1 - num2;
- }
- else
- {
- num2 = num2 - num1;
- }
- }
- lcmNum = (a * b) / num1;
- Console.Write("LCM number: " + lcmNum);
- }
Output
Print table from 1 to 10
- public void print1To10Table()
- {
-
- 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()
- {
-
- int num, firstNum = 0, secondNum = 1, temp, thirdNum = 3;
- Console.Write("Enter the number: ");
- num = Convert.ToInt32(Console.ReadLine());
- Console.Write(firstNum + "\t" + secondNum);
- while (thirdNum <= num)
- {
- temp = firstNum + secondNum;
- Console.Write("\t" + temp);
- firstNum = secondNum;
- secondNum = temp;
- thirdNum = thirdNum + 1;
- }
- }
Output
Swap numbers using a third variable
- public void SwapNumber()
- {
-
-
-
-
- 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;
- num2 = num1;
- num1 = temp;
- 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;
- num2 = num1 - num2;
- num1 = num1 - num2;
- Console.WriteLine("Number After Swap x: " + num1 + " y: " + num2);
- }
Output
Print tranpose of a 3 by 3 matrix
- public void Print3By3MatrixTranspose()
- {
-
- int[,] pos = new int[3, 3];
- int rows, cols;
- 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());
- }
- }
- Console.WriteLine("Original Matrix:");
- for (rows = 0; rows < 3; rows++)
- {
- for (cols = 0; cols < 3; cols++)
- {
- Console.Write(pos[rows, cols] + "\t");
- }
- 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");
- }
- Console.WriteLine("");
- }
- }
Output
Matrix multiplication between two 3 by 3 matrices
- public void Print3By3MatrixMultiplication()
- {
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- 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());
- }
- }
- 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());
- }
- }
- Console.WriteLine("Original Matrix 1:");
- for (rows = 0; rows < 3; rows++)
- {
- for (cols = 0; cols < 3; cols++)
- {
- Console.Write(fm[rows, cols] + "\t");
- }
- 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");
- }
- 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];
- }
- rm[rows, cols] = 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");
- }
- Console.WriteLine("");
- }
-
- }
Output
Triangle number pyramid
Print the number pyramid which looks like following,
1
2 2
3 3 3
- public void TrianglePyramidNumber()
- {
-
- int rows, cols, temp, space = 8;
- for (rows = 1; rows <= 9; rows++)
- {
- for (cols = space; cols >= 1; cols--)
- {
- Console.Write(" ");
- }
- for (temp = 1; temp <= rows; temp++)
- {
- Console.Write(rows + " ");
- }
- Console.WriteLine("");
- space--;
- }
- }
Output
Print the number pyramid where the numbers start from the left side.
- public void leftPyramidNumber()
- {
-
- int rows, cols;
- for (rows = 1; rows <= 9; rows++)
- {
- for (cols = 1; cols <= rows; cols++)
- {
- Console.Write(rows + " ");
- }
- Console.WriteLine("");
- }
- }
Output
Print the number pyramid where numbers start from the right side.
- public void RightPyramidNumber()
- {
-
- int rows, cols;
- for (rows = 1; rows <= 9; rows++)
- {
- for (cols = 1; cols <= 9 - rows; cols++)
- {
- Console.Write("\t");
- }
- for (cols = 1; cols <= rows; cols++)
- {
- Console.Write(rows + "\t");
- }
- Console.WriteLine("");
- }
-
- }
Output
Find the source code as an attachment.