0% found this document useful (0 votes)
8 views27 pages

C# Practical

The document contains a series of practical exercises in C# programming, each with a specific aim and corresponding code implementation. Topics covered include checking number signs, printing student marksheets, character classification, calculating cubes, converting decimal to binary, finding even numbers, calculating factorials, using ArrayLists, foreach loops, class and object concepts, constructors, inheritance, interfaces, and abstract classes. Each practical includes sample outputs demonstrating the functionality of the code.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views27 pages

C# Practical

The document contains a series of practical exercises in C# programming, each with a specific aim and corresponding code implementation. Topics covered include checking number signs, printing student marksheets, character classification, calculating cubes, converting decimal to binary, finding even numbers, calculating factorials, using ArrayLists, foreach loops, class and object concepts, constructors, inheritance, interfaces, and abstract classes. Each practical includes sample outputs demonstrating the functionality of the code.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 27

Practical No:1

Aim:Write a program in c# to check whether it is positive negative,zero.


Developed by:
Coding:
using System; class
program
{
public static void Main()
{
Console.WriteLine("Enter a number: ");
int number= Convert.ToInt32(Console.ReadLine());
if (number>0)
{
Console.WriteLine(number+ " is positive");
}
else if (number == 0)
{
Console.WriteLine("Number is 0");
}
else
{
Console.WriteLine(number+ " is negative");
}
}
}
Output:
E:\>Pcg Enter a
number:
11
11 is positive

E:\>PcgEnter a
number:
0
Number is 0

E:\>Pcg Enter a
number:
-6
-6 is negative
Practical No:2
Aim:Write a program in c# to print marksheet of a student.
Developed by:
Coding:
using System;
namespace Marksheet1
{
class MarksheetProgram
{
public static void Main(string[] args)
{
int phy,che,maths,t;
float p;
string n,r;
Console.WriteLine("Enter Roll Number :");
r=Console.ReadLine();
Console.WriteLine("Enter student Name :");
n=Console.ReadLine();
Console.WriteLine("Mark of physics :");
phy = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Mark of chemistry :");
che = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Mark of Maths :");
maths = Convert.ToInt32(Console.ReadLine());
t = phy+che+maths;
p = t/3.0f;
Console.WriteLine("Total : " + t);
Console.WriteLine("Percentage : " + p);
if (p>= 35 && p<50)
{
Console.WriteLine("Grade is C");
}
if (p>=50 && p<=60)
{
Console.WriteLine("Grade is B");
}
if (p>60 && p<=80)
{
Console.WriteLine("Grade is A");
}
if (p>80 && p<=100)
{
Console.WriteLine("Grade is A+");
}
Console.ReadLine();
}
}
}
Output:
E:\>marksheet
Enter Roll Number :
40
Enter student Name :
Prajwal
Mark of physics :
90
Mark of chemistry :
94
Mark of Maths :
88
Total : 272
Percentage : 90.66666
Grade is A+
Practical No:03
Aim: Write a program in c# to check whether given character is
Consonant or Vowel Developed By:
Coding:
using System;
namespace Conditional
{
class SwitchCase
{
public static void Main(string[] args)
{
char ch;
Console.WriteLine("Enter an alphabet");
ch=Convert.ToChar(Console.ReadLine());
switch(Char.ToLower(ch))
{
case'a':
Console.WriteLine("Vowel";
break;
case 'e':
Console.WriteLine("Vowel";
break;
case 'i':
Console.WriteLine("Vowel";
break;
case 'o':
Console.WriteLine("Vowel";
break;
case'u':
Console.WriteLine("Vowel";
break;
default:
Console.WriteLine("Not a vowel , it is a consonant");
break;
}
}
}
}
Output: E:\>Practical3
Enter an alphabet
O
Vowel

E:\>Practical3
Enter an alphabet
R
Not a vowel , it is a consonant
Practical No:04
Aim: Write a program in c# to find a cube of given number Developed
By:
Coding:
using System;
public class Cube
{
public static void Main()
{
int i,ctr;
Console.Write("Display the cube of the number :\n");
Console.Write("----------------------------------");
Console.Write("\n\n");
Console.Write("Input number is :"); ctr =
Convert.ToInt32(Console.ReadLine());
for(i=1;i<=ctr;i++)
{
Console.Write("Number is : {0} and cube of the {1} is :{2} \n", i, i, (i*i*i));
}
}
}

Output:
E:\>Practical4
Display the cube of the number :
----------------------------------

Input number is :7
Number is : 1 and cube of the 1 is :1
Number is : 2 and cube of the 2 is :8
Number is : 3 and cube of the 3 is :27
Number is : 4 and cube of the 4 is :64
Number is : 5 and cube of the 5 is :125
Number is : 6 and cube of the 6 is :216
Number is : 7 and cube of the 7 is :343
Practical No:05
Aim: : Write a program in c# to find binary equivalent of given
decimal number.
Developed By:
Coding:
using System;
public class DecToBin
{
public static void Main()
{
int n,i,j,binno=0,dn;
Console.Write("Convert a decimal number to binary without using
array:\n");
Console.Write("---------------------------------------------------------\n\n");
Console.Write("Enter a number to convert :");
n=Convert.ToInt32(Console.ReadLine());
dn=n;
i=1;
for(j=n;j>0;j=j/2)
{
binno=binno + (n % 2)*i;
i = i* 10;
n = n / 2;
}
Console.Write("\nThe Binary of {0} is {1}.\n\n" , dn, binno);
}
}

Output:

E:\>Practical5
Convert a decimal number to binary without using array:
---------------------------------------------------------

Enter a number to convert :4

The Binary of 4 is 100.


Practical No:06
Aim: Write a Program in C# to find Even Number with the help of
while loop.
Developed By:
Coding:
using System;
namespace ControlFlowDemo
{
class Program
{
static void Main(string[] args)
{
int i, n;
Console.Write("Enter a Number:");
n=Convert.ToInt32(Console.ReadLine());
i=2;
while(i<=n)
{
Console.Write(" "+i+" ");
i=i+2;
}
Console.ReadKey();
}
}
}

Output:
E:\>Practical6
Enter a Number:24
2 4 6 8 10 12 14 16 18 20 22 24
Practical No:07
Aim: Write a Program in c# to find the Factorial of a given number
using do-while.
Developed By: Rohan Ravindra Deshpande
Coding:
using System;
class FindFactorialDoWhileLoop
{
static void Main()
{
Console.Write("Please Enter Your Number:");
int n= int.Parse(Console.ReadLine());
int factorial=1;
do
{
factorial *=n;
n--;
} while(n>0);
Console.WriteLine("n!={0}", factorial);
}
}

Output:

E:\>Practical7
Please Enter Your Number:8
n!=40320
Practical No:8
Aim: Write a program in c# for ArrayList Concept
Developed by:
Coding:
using System;
using System.Collections;
class Program
{
static void Main(string[] args)
{
//creating arraylist
ArrayList arrlist = new ArrayList();
arrlist.Add("DCPE HVPM");
arrlist.Add(33);
arrlist.Add(20.5f);
arrlist.Add("Om Gupta ");
//inserting element into arraylist
arrlist.Insert(0, 7507291222);
arrlist.Insert(1, "Amravati");
ArrayList arrlist2 = new ArrayList() { "JAVA", "C#"};
//inserting arrlist2 into arrlist at position 2
arrlist.InsertRange(2, arrlist2);
Console.WriteLine("**********ArrayList Element***********");
foreach(var item in arrlist)
{
Console.WriteLine(item);
}
Console.WriteLine("Numbers of elements :" + arrlist.Count);
arrlist.Remove(20.5f);
arrlist.RemoveAt(0);
arrlist.RemoveRange(3, 2);
Console.WriteLine("**********ArrayList Elements**********");
foreach(var item in arrlist)
{
Console.WriteLine(item);
}
Console.WriteLine("Number of Elements :" + arrlist.Count);
}
}

Output:
E:\C#>Practical8
**********ArrayList Element***********
7507291222
Amravati
JAVA
C#
DCPE HVPM
33
20.5
axyz
Numbers of elements :8
**********ArrayList Elements**********
Amravati
JAVA
C#
xyz
Number of Elements :4
Practical No:09
Aim:Write a program in C# for the concept of foreach loop.
Developed By:
Coding:
using System;
namespace Tutlane
{
class Program
{
public static void Main(string[] args)
{
string[] names = new string[3]{"xyzz", "xyzz", "xyz"};
foreach(string name in names)
{
Console.WriteLine(name);
}
char[] gender={'m','f','m','m','m','f','f','m','m','f'};
int male=0,female=0;
foreach(char g in gender)
{
if (g=='m')
male++;
else if(g=='f')
female++;
}
Console.WriteLine("Number of male={0}", male);
Console.WriteLine("Number of female={0}", female);
}
}
}
OUTPUT:

E:\>Practical9
Xyzz
Xyzz
xxtz
Number of male=6
Number of female=4

Practical No:10
Aim: Write a Program in C# on concept of class and object using Bank Account
details.
Developed by
Coding:
using System;
class BankAccount
{
private string accountName;
private string accountNumber;
private decimal balance;
public BankAccount(string accountName,string accountNumber,decimal balance)
{
this.accountName=accountName;
this.accountNumber=accountNumber;
}
public void Deposit(decimal amount)
{
balance +=amount;
}
public void Withdraw(decimal amount)
{
if(amount <=balance)
{
balance -=amount;
}
else
{
Console.WriteLine("Insufficient fund.");
}
}
public void PrintBalance()
{
Console.WriteLine("Account Holder Name:" + accountName);
Console.WriteLine("Account Number:" + accountNumber);
Console.WriteLine("Balance:" + balance);
}
}
class Program
{
static void Main(string[] args)
{
BankAccount myAccount= new BankAccount("xyx","153399770",1000);
myAccount.PrintBalance();
myAccount.Deposit(500);
myAccount.PrintBalance();
myAccount.Withdraw(200);
myAccount.PrintBalance();
myAccount.Withdraw(2000);
myAccount.PrintBalance();
}
}

OUTPUT:

E:\>bi10
Account Holder Name: xyz
Account Number:153399770
Balance:0
Account Holder Name:xyz
Account Number:153399770
Balance:500
Account Holder Name: xyz
Account Number:153399770
Balance:300
Insufficient fund.
Account Holder Name: xyz
Account Number:153399770
Balance:300

Practical No. 11
Aim : Write a program in C# to perform concept of Parameterized
Constructor. Developed by :
Coding :

using System;
public class Employee
{
public int id;
public String name; public
float salary;
public Employee(int i, String n, float s)
{
id = i;
name = n;
salary = s;
}
public void display()
{
Console.WriteLine(id + " " + name+" "+salary);
}
}
class TestEmployee{
public static void Main(string[] args)
{
Employee e1 = new Employee(100,"Tarkeshwar",700000);
Employee e2 = new Employee(105,"Yash",800000); e1.display();
e2.display();
}
}

OUTPUT :

D:\C# Programs>csp11
100 xyz700000
105 Yash 800000

Practical No. 12
Aim : Write a Program to achieve Multilevel Inheritance.
Developed by :
Coding :

using System;
public class Grandparent
{
public Grandparent()
{
Console.WriteLine("Constructor called at run-time");
}
public void DisplayGrandParents()
{
Console.WriteLine("Method of Grandparents class is running");
}
}
public class Parents:Grandparent
{
public void DisplayParents()
{
Console.WriteLine("Method of Parents class is running");
}
}
public class Child:Parents
{
public void DisplayChild()
{
Console.WriteLine("Method of Child class is running");
}
}
public class Program
{
public static void Main(string[] args)
{
Child objc=new Child();
objc.DisplayChild();
objc.DisplayParents();
objc.DisplayGrandParents();
}
}

OUTPUT :

D:\C# Programs>csp12
Constructor called at run-time
Method of Child class is running
Method of Parents class is running
Method of Grandparents class is running
Practical No. 13

Aim : Write a Program to achieve Mixed or Hybrid Inheritance.


Developed by :
Coding :

using System;
class GrandFather
{
public void GrandfatherProperty()
{
Console.WriteLine("GrandFather's assets");
}
}
class Father : GrandFather
{
public void home()
{
Console.WriteLine("Father's Home");
}
public void Car()
{
Console.WriteLine("Father's Car");
}
}
class Son: Father
{
public Son()
{
Console.WriteLine("Son...");
}
public void bike()
{
Console.WriteLine("Son's Bike");
}
}
class Daughter : Father
{
public Daughter()
{
Console.WriteLine("Daughter...");
}
public void mobile()
{
Console.WriteLine("Daughter's Mobile");
}
}
public class TestHybridInheritance
{
public static void Main(String[] args)
{
Son s=new Son();
s.GrandfatherProperty();
s.Car();
s.home();
s.bike();
Daughter d=new Daughter();
d.GrandfatherProperty();
d.Car();
d.home();
d.mobile();
}
}

OUTPUT :

D:\C# Programs>csp13
Son...
GrandFather's assets
Father's Car
Father's Home
Son's Bike
Daughter...
GrandFather's assets
Father's Car
Father's Home
Daughter's Mobile

Practical No. 14
Aim : Wap in C# to demonstrate the concept of Interface.
Developed by :
Coding :

using System;
namespace HelloWorld
{
interface ICompany
{
void companyname();
}
class Employee:ICompany
{
public void companyname()
{
Console.WriteLine("Company Name is : DCPE HVPM Amt.");
Console.WriteLine("Employee Name Name is : fhffhfhhfhfhf");
Console.WriteLine("Mobile Number is : 8378966635");
}
}
class Program
{
static void Main(string[] args)
{
Employee emp=new Employee();
emp.companyname();
}
}
}

OUTPUT :

D:\C# Programs>csp14
Company Name is : DCPE HVPM Amt.
Employee Name Name is : xdfhgfgffhffhfhfh
Mobile Number is : 837888635

Practical No. 15
Aim : Wap in C# to achieve Multiple Inheritance using Interface.
Developed by :
Coding :

using System;
namespace MyFamily
{
interface IMotherInterface
{
void Mother();
}
interface IFatherInterface
{
void Father();
}
class ChildClass : IMotherInterface,IFatherInterface
{
public void Mother()
{
Console.WriteLine("Mother's Name is Vaishali");
}
public void Father()
{
Console.WriteLine("Father's Name is Shrikrishna");
}
}
class Program
{
static void Main(string[] args)
{
ChildClass child=new ChildClass();
child.Mother();
child.Father();
}
}
}

OUTPUT :

D:\C# Programs>csp15
Mother's Name is Vaishali
Father's Name is Shrikrishna

Practical No. 16
Aim : Write a Program in C# to create our own namespace and access their classes.
Developed by :
Coding :

using System;
using First;
using Second;
namespace First
{
public class Hello
{
public void sayHello()
{
Console.WriteLine("Hello Namespace");
}
}
}
namespace Second
{
public class Welcome
{
public void sayWelcome()
{
Console.WriteLine("Welcome Namespace");
}
}
}
public class TestNamespace
{
public static void Main()
{
Hello hl=new Hello();
Welcome w1=new Welcome();
hl.sayHello();
w1.sayWelcome();
}
}

OUTPUT :

D:\C# Programs>csp16
Hello Namespace
Welcome Namespace

Practical No. 17
Aim : Write a Program in C# to demonstrate the concept of Abstract Class and
Abstract Method.
Developed by :
Coding :

using System;
namespace AbstractClassesAndMethods
{
public abstract class AbsParent
{
public void Add(int x, int y)
{
Console.WriteLine("Addition is: " +(x+y));
}
public void Sub(int x, int y)
{
Console.WriteLine("Subtraction is " +(x-y));
}
public abstract void Mul(int x,int y);
public abstract void Div(int x,int y);
}
public class AbsChild: AbsParent
{
public override void Mul(int x,int y)
{
Console.WriteLine("Multiplication is :" +(x*y));
}
public override void Div(int x,int y)
{
Console.WriteLine("Division is :" +(x/y));
}
class Program
{
public static void Main(string[] args)
{
AbsChild absChild=new AbsChild();
absChild.Add(10,5);
absChild.Sub(10,5);
absChild.Mul(10,5);
absChild.Div(10,2);
}
}
}
}

OUTPUT :
D:\C# Programs>csp17
Addition is: 15
Subtraction is 5
Multiplication is :50
Division is :5

Practical No : 18
Aim : Write a program in c# to achieve compile time polymorphism using method
overloading.
Developed By :
Coding :

using System;
namespace MethodOverloading
{
class Program
{
public void Add(int a,int b)
{
Console.WriteLine(a+b);
}
public void Add(float x,float y)
{
Console.WriteLine(x+y);
}
public void Add(string s1,string s2)
{
Console.WriteLine(s1+" "+s2);
}
static void Main(string[] args)
{
Program obj=new Program();
obj.Add(10,20);
obj.Add(10.5f,20.5f);
obj.Add("Tarkeshwar,"Gajare");
Console.ReadKey();
}
}
}

OUTPUT :

D:\C# Programs>c#p18
30
31
ugugugugugu

Practical No. 19
Aim : Write a Program in C# to achieve Run Time Polymorphism using Overriding.
Developed by :
Coding :
using System;
namespace MethodOverloading
{
public class Calculate
{
public void AddNumbers(int a, int b)
{
Console.WriteLine("a + b = {0}", a + b);
}
public void AddNumbers(int a, int b , int c)
{
Console.WriteLine("a + b + c = {0}", a + b + c);
}
}
class Program
{
static void Main(string[] args)
{
Calculate c = new Calculate();
c.AddNumbers(1, 2);
c.AddNumbers(1, 2, 3);
}
}
}

OUTPUT :

D:\C# Programs>csp19
a+b=3
a+b+c=6

Practical No. 20
Aim : Write a Program in C# to achieve the Exception Handling Concept.
Developed by :
Coding :

using System;
public class Program
{
static void Main(string[] args)
{
Console.WriteLine("Enter first number :");
int firstNumber=int.Parse(Console.ReadLine());
Console.WriteLine("Enter second number :");
int secondNumber=int.Parse(Console.ReadLine());
try
{
int divisionResult=firstNumber/secondNumber;
Console.WriteLine("Division of two numbers is :"+divisionResult);
}
catch(Exception e)
{
Console.WriteLine("An exception occurred :"+e.Message);
}
finally
{
Console.WriteLine("Sum of two numbers is: "+(firstNumber+secondNumber));
}
}
}

OUTPUT :

D:\C# Programs>csp20
Enter first number :
07
Enter second number :
28
Division of two numbers is :0
Sum of two numbers is: 35

D:\C# Programs>csp20
Enter first number :
28
Enter second number :
0
An exception occurred :Attempted to divide by zero.
Sum of two numbers is: 28

You might also like