Vardemo
using System;
class vardemo
{
static void Main(string[] args)
{
int x, y, z;
System.Console.Write("Enter the Value x:");
x = int.Parse(System.Console.ReadLine());
System.Console.Write("Enter the value y:");
y=int.Parse(System.Console.ReadLine());
z = x + y;
System.Console.WriteLine("Sum of {0} & {1} is :{2}",x,y,z);
System.Console.ReadLine();
}
}
IfDemo
using System;
namespace Sample_Programs
{
class IfDemo
{
static void Main(string[] args)
{
int x, y;
Console.WriteLine("Enter the values of X:");
x=int.Parse(Console.ReadLine());
Console.WriteLine("Enter the value of Y:");
y=int.Parse(Console.ReadLine());
if (x>y)
Console.WriteLine("X is greater");
else if (x<y)
Console.WriteLine("Y is grater");
else
Console.WriteLine("Both are Equal");
Console.ReadLine();
}
}
}
SwitchDemo
using System;
namespace Sample_Programs
{
class SwitchDemo
{
static void Main(string[] args)
{
Console.WriteLine("Enter The Student Number(1-3): ");
int sno=int.Parse(Console.ReadLine());
switch (sno)
{
case 1:
Console.WriteLine("Student number is 1");
break;
case 2:
Console.WriteLine("Student Number is 2");
break;
case 3:
Console.WriteLine("Student Number is 3");
break;
default:
Console.WriteLine("wrong Entered");
break;
}
Console.ReadLine();
}
}
}
Single Dimensional Array
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Sample_Programs
{
class SDArray
{
static void Main(string[] args)
{
int[] arr = new int[5];
arr[0] = 10; arr[1] = 20; arr[2] = 30; arr[3] = 40; arr[4] = 50;
for (int i = 0; i < 5; i++)
Console.Write(arr[i]+" ");
Console.WriteLine();
foreach (int i in arr)
Console.WriteLine(i+" ");
Console.WriteLine();
Console.ReadLine();
}
}
}
Single Dimensional Array2
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Sample_Programs
{
class SDArray2
{
static void Main(string[] args)
{
int[] arr = { 12, 45, 2, 4, 21,44,454,55,7,754};
for (int i = 0; i < arr.Length; i++)
Console.Write(arr[i]+" ");
Console.WriteLine();
Console.WriteLine("\n");
Array.Sort(arr);
foreach (int i in arr)
Console.Write(i+" ");
Console.WriteLine();
Console.WriteLine("\n");
Array.Reverse(arr);
foreach (int i in arr)
Console.Write(i + " ");
Console.WriteLine();
Console.WriteLine("\n");
int[] brr=new int[10];
Array.Copy(arr,brr,5);
foreach (int i in arr)
Console.Write(i + " ");
Console.WriteLine();
Console.ReadLine();
}
}
}
Two Dimensional Array
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Sample_Programs
{
class TDArray
{
static void Main(string[] args)
{
int[,] arr = new int[4, 5];
int x = 5;
//Assigning values to 2D Array
for (int i = 0; i < arr.GetLength(0); i++)
{
for (int j = 0; j < arr.GetLength(1); j++)
{
arr[i, j] = x;
x += 5;
}
}
//Printing values of 2D Array
for (int i = 0; i < arr.GetLength(0); i++)
{
for (int j = 0; j < arr.GetLength(1); j++)
{
Console.Write(arr[i,j]+" ");
}
Console.WriteLine();
}
Console.ReadLine();
}
}
}
Febonacci Series
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Sample_Programs
{
class feb
{
static void Main(string[] args)
{
int i = 0, j = 0, sum = 1, n;
Console.WriteLine("Enter the n value: ");
n=int.Parse(Console.ReadLine());
while (sum < n)
{
Console.WriteLine("Febonacci Series: "+sum);
i = j;
j = sum;
sum = i + j;
}
Console.ReadLine();
}
}
}
Interface
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Sample_Programs
{
class Interface:Interface1,Interface2
{
public void add(int x, int y)
{
Console.WriteLine("Addition of x,y: "+(x+y));
}
public void sub(int x, int y)
{
Console.WriteLine("Subtraction of x,y: " + (x - y));
}
public void mul(int x, int y)
{
Console.WriteLine("Multiplication of x,y: " + (x * y));
}
public void div(int x, int y)
{
Console.WriteLine("Division of x,y: " + (x / y));
}
void Interface1.test()
{
Console.WriteLine("Interface1 is Tested");
}
void Interface2.test()
{
Console.WriteLine("Interface2 is Tested");
}
static void Main(string[] args)
{
Interface c=new Interface();
c.add(15, 25); c.sub(45, 25); c.mul(15, 25); c.div(35, 25); Interface1 i1 = c; Interface2 i2 = c; i1.test();
i2.test();
Console.ReadLine();
}
}
}
Interface1
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Sample_Programs
{
interface Interface1
{
void add(int x, int y);
void sub(int x, int y);
void test();
}
}
Interface2
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Sample_Programs
{
interface Interface2
{
void mul(int x, int y);
void div(int x, int y);
void test();
}
}
MatrixMultiplication
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Sample_Programs
{
class MatrixMultiplication
{
static void Main(string[] args)
{
int[,] a = new int[2, 2];
int[,] b = new int[2, 2];
int[,] n = new int[2, 2];
for (int i = 0; i < a.GetLength(0); i++)
{
for (int j = 0; j < a.GetLength(1); j++)
{
Console.WriteLine("Enter 1st Matrix: ");
a[i, j] = Convert.ToInt32(Console.ReadLine());
}
} Console.WriteLine();
for (int i = 0; i < b.GetLength(0); i++)
{
for (int j = 0; j < b.GetLength(1); j++)
{
Console.WriteLine("Enter 2st Matrix: ");
b[i, j] = Convert.ToInt32(Console.ReadLine());
}
} Console.WriteLine();
Console.WriteLine("Matrix 1: ");
for (int i = 0; i < a.GetLength(0); i++)
{
for (int j = 0; j < a.GetLength(1); j++)
{
Console.Write(a[i, j] + " ");
} Console.WriteLine();
}
Console.WriteLine("Matrix 2: ");
for (int i = 0; i < b.GetLength(0); i++)
{
for (int j = 0; j < b.GetLength(1); j++)
{
Console.Write(b[i, j] + " ");
} Console.WriteLine();
}
Console.WriteLine("Matrix1*Matrix2: ");
for (int i = 0; i < n.GetLength(0); i++)
{
for (int j = 0; j < n.GetLength(1); j++)
{
n[i, j] = 0;
for (int k = 0; k < a.GetLength(1); k++)
{
n[i, j] =n[i,j] + a[i, k] * b[k, j];
} Console.Write(n[i, j] + " ");
} Console.WriteLine();
}
Console.ReadLine();
}
}
}
Numberpyramid
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Sample_Programs
{
class Numberpyramid
{
static void Main(string[] args)
{
Console.WriteLine("Enter the number of star lines you wish to print");
int n = Convert.ToInt32(Console.ReadLine());
for (int i = 0; i < n; i++)
{
int stars = 1 + 2 * (i);
int space = n - i;
for (int j = 0; j < space; j++)
{
Console.Write(" ");
}
for (int k = 0; k < stars; k++)
{
Console.Write(i + 1 + " ");
}
Console.WriteLine();
} Console.ReadLine();
}
}
}
//output:
//Enter the number of star lines you wish to print
//3
// 1
// 2 2 2
//3 3 3 3 3
PolymorphismDemo
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
namespace Sample_Programs
{
class PolymorphismDemo
{
public void show()
{
Console.WriteLine();
}
public void show(int x)
{
Console.WriteLine(x);
}
public void show(string s)
{
Console.WriteLine(s);
}
public void show(int x,string s)
{
Console.WriteLine(x+" "+s);
}
public void show(string s, int x)
{
Console.WriteLine(s+" "+x);
}
static void Main(string[] args)
{
Console.WriteLine();
PolymorphismDemo d=new PolymorphismDemo();
d.show();d.show(5);d.show("effone Tech");d.show(10,"effone");d.show("effone",10);
Console.ReadLine();
}
}
}
pyramid
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Sample_Programs
{
class pyramid
{
static void Main(string[] args)
{
Console.WriteLine("Enter the n:");
int n = Convert.ToInt32(Console.ReadLine());
int k = 1;
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= i; j++)
{
Console.Write(k + " ");
k = k + 1;
}
Console.WriteLine(" ");
} Console.ReadLine();
}
}
}
Rectangle:figure
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Sample_Programs
{
public class Rectangle:figure
{
public Rectangle(double width, double height)
{
this.width = width;
this.height = height;
}
public override double GetArea()
{
return(width*height) ;
}
public override double GetPerimeter()
{
return 2*(width+height);
}
static void Main()
{
Rectangle r = new Rectangle(100.00,50.56);
Console.WriteLine("Area of rectangle is: "+r.GetArea());
Console.WriteLine("\n");
Console.WriteLine("Perimeter of rectangle is: "+r.GetPerimeter());
Console.ReadLine();
}
}
}
StarAlphaDemo
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Sample_Programs
{
class StarAlpha
{
static void Main(string[] args)
{
Console.WriteLine("Enter the number N:");
int n = int.Parse(Console.ReadLine());
for (int i = 0; i < n; i++)
{
int stars = 1 + 2 * (i);
int spaces = n - i;
if (i == 4)
{
for (int j = 0; j < spaces; j++)
{
Console.Write(" ");
}
Console.WriteLine("u m a m a h e s h");
}
else
{
for (int j = 0; j < spaces; j++)
{
Console.Write(" ");
}
for (int k = 0; k < stars; k++)
{
Console.Write("*" + " ");
}
Console.WriteLine();
}
}
Console.ReadLine();
}
}
}
StarPyramid
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Sample_Programs
{
class StarPyramid
{
static void Main(string[] args)
{
Console.WriteLine("Enter the number of * lines you wish to print");
int n = Convert.ToInt32(Console.ReadLine());
for (int i = 0; i < n; i++)
{
int stars = 1 + 2 * (i);
int space = n - i;
for (int j = 0; j < space; j++)
{
Console.Write(" ");
}
for (int k = 0; k < stars; k++)
{
Console.Write("*" + " ");
}
Console.WriteLine();
}
Console.ReadLine();
}
}
}
//output:
//Enter the number of star lines you wish to print
//3
// *
// * * *
//* * * * *