Download as pdf or txt
Download as pdf or txt
You are on page 1of 7

Document title goes here Author Name:

Date:

Basic C# Programs for reference:

Program 1: To print Helloworld

using System;
public class Hello
{
static void Main(string[] args)
{
Console.WriteLine("HelloWorld! ");
}
}

Program 2: Addition of two numbers

using System;
class Demo
{
public static void Main(string[] args)
{
int val1 , val2 ,sum;
val1 = 2;
val2 = 4;
sum = a + b;
Console.WriteLine("Value of addition is : "+sum);
}

Program 3: To read integer values entered by user.

using System;
public class add
{
public static void Main(string[] args)
{
int val1, val2, sum;
Console.Write("Enter first no: ");
val1 = Convert.ToInt32(Console.ReadLine());
Console.Write("Enter second no: ");
val2 = Convert.ToInt32(Console.ReadLine());
sum = val1 + val2;
Console.WriteLine("Addition of two no is: "+ sum);
}
}
Document title goes here Author Name:

Date:

Program 4: To read character value entered by user.

using System;
class Demo
{
public static void Main(string[] args)
{
char a;
Console.Write("Enter a character: ");
a = Convert.ToChar(Console.ReadLine());
Console.WriteLine("Character entered by user is :"+a);
}
}

Program 5: To read float values entered by user.

using System;
public class Demo
{
public static void Main(string[] args)
{
float val1, val2, sum;

Console.Write("Enter first no: ");


val1 = Convert.ToSingle(Console.ReadLine());

Console.Write("Enter second no: ");


Val2 = Convert.ToSingle(Console.ReadLine());

sum = val1 + val2;


Console.WriteLine("Addition of two no is: "+sum);
}
}

Program 6: To read double values entered by user.

using System;
public class Demo
{
public static void Main(string[] args)
{
double val1, val2, sum;

Console.Write("Enter first no: ");


Document title goes here Author Name:

Date:

val1 = Convert.ToDouble(Console.ReadLine());

Console.Write("Enter second no: ");


val2 = Convert.ToDouble(Console.ReadLine());

sum= val1 + val2;


Console.WriteLine("Addition of two no is: "+ sum);
}
}

Program 7: Program to understand class and object

using System;
class TennisPlayer
{
string Name;
int Rank;

public void PrintDetails()


{
Console.WriteLine("The Details of the Tennis Player are: ");
Console.WriteLine("Name: ");
Console.WriteLine(Name);
Console.WriteLine("Rank: ");
Console.WriteLine(Rank);
}

public void GetDetails()


{
Console.WriteLine("Enter the Details of the Tennis Player: ");
Console.WriteLine("\n Name: ");
Name=Console.ReadLine();
Console.WriteLine("Rank: ");
Rank=Convert.ToInt32(Console.ReadLine());
}

class Tennis
{
public static void Main(string[] args)
{
TennisPlayer P1=new TennisPlayer();
P1.GetDetails();
P1.PrintDetails();
}
}
}
Document title goes here Author Name:

Date:

Basic Programs on loops & Constructs

Program 1: To print even numbers between 0 to 100

using System;
class Variable
{
public static void Main(string[] args)
{
int var;
var = 0;
do
{
Console.WriteLine("Values of variable is: "+var);
var = var + 2;
} while(var<100);
}
}

Program 2: To print mathematic table of any given number.

using System;
class Variable
{
public static void Main(string[] args)
{
int a, val1, product;

Console.Write("Enter the Value for which you want table: ");


val1 = Convert.ToInt32(Console.ReadLine());

for(a = 1; a <= 10; a++)


{
product = a * val1;
Console.WriteLine(a + " * "+ val1 + " = " +product);
}
}
}

Program 3: To check whether entered character is vowel or not.

using System;
class Vowel
{
Document title goes here Author Name:

Date:

public static void Main(string[] args)


{
char V;
Console.WriteLine("Enter any alphabet: ");
V = Convert.ToChar(Console.ReadLine());

if(V=='a')
Console.WriteLine("alphabet is vowel: ");
else if(V=='e')
Console.WriteLine("alphabet is vowel: ");
else if(V=='i')
Console.WriteLine("alphabet is vowel: ");
else if(V=='o')
Console.WriteLine("alphabet is vowel: ");
else if(V=='u')
Console.WriteLine("alphabet is vowel: ");
else
Console.WriteLine("alphabet is not a vowel: ");
}
}

Program 4: To identify greatest of two numbers

using System;
class Greatest
{
public static void Main(String[] args)
{
int val1, val2;
Console.Write("Enter first No. ");
val1 = Convert.ToInt32(Console.ReadLine());
Console.Write("Enter second No. ");
Val2 = Convert.ToInt32(Console.ReadLine());

if(val1 > val2)


{
Console.WriteLine("a is greatest");
}
else
{
Console.WriteLine("b is greatest");
}
}
}
Document title goes here Author Name:

Date:

Program 5: Demo on switch case construct

using System;
class Days
{
public static void Main(String[] args)
{
char a;
Console.WriteLine("Enter the Corresponding Values for Days From 1 to 7: ");
a = Convert.ToChar(Console.ReadLine());
switch(a)
{
case '1' : Console.WriteLine("Sunday");
break;
case '2' : Console.WriteLine("Monday");
break;
case '3' : Console.WriteLine("Tuesday");
break;
case '4' : Console.WriteLine("Wednesday");
break;
case '5' : Console.WriteLine("Thursday");
break;
case '6' : Console.WriteLine("Friday");
break;
case '7' : Console.WriteLine("Saturday");
break;
default : Console.WriteLine("danger");
//break;
}
}
}

Program 6: To display factorial of given numbers

using System;
class Number
{
public int factorial(int n)
{
int result;
if(n==1)
return 1;
else
{
result = factorial(n-1) * n;
return result;
Document title goes here Author Name:

Date:

}
}

static void Main(string[] args)


{
Number obj = new Number();
Console.WriteLine("Factorial of 10 is : "+obj.factorial(10));
Console.WriteLine("Factorial of 5 is : "+obj.factorial(5));
Console.WriteLine("Factorial of 7 is : "+obj.factorial(7));
}
}

You might also like