C# PROGRAM
C# PROGRAM
C# PRACTICAL
PRACTICAL NO: - 01
OUTPUT:-
1
1043006
PRACTICAL NO: - 02
TITLE:- Write a program to demonstrate the use of concept of Boxing and Unboxing
CODING:-
using System;
class edit
{
public static void Main()
{
int m=100;
object om=m;
m=20;
Console.WriteLine("The value of m is : "+m);
Console.WriteLine("The value of om is : "+om);
int n=(int)om;
Console.WriteLine("The value of n is : "+n);//n=100
}
}
OUTPUT:-
PRACTICAL NO: - 03
TITLE:- Write a program to read interactively two integers using the method
Console.ReadLine () and int.Parse and display their sum, difference, product, division and
modulus division.
2
1043006
CODING:
using System;
class edit
{
public static void Main()
{
int x,y;
Console.WriteLine("Enter the first number:");
x=(int.Parse)(Console.ReadLine());
Console.WriteLine("Enter the second number:");
y=(int.Parse)(Console.ReadLine());
int sum=x+y;
Console.WriteLine("The Sum of two number is : "+ sum);
int diff=x-y;
Console.WriteLine("The difference of two number is:" +diff);
int mul=x*y;
Console.WriteLine("The Multiplication of two number is: "+mul);
int div=x/y;
Console.WriteLine("The Division of two number is : "+ div);
int mod=x%y;
Console.WriteLine("The Modulus of two number is : "+ mod);
}
}
OUTPUT:-
PRACTICAL NO: - 04
TITLE :- State why the expression x-y=10 is invalid but the expression x-(y=100) execute
the program to demonstrate the answer.
3
1043006
CODING:
A) Expression x-y=10
using System;
class edit
{
public static void Main()
{
int x=0;
int y;
int r;
r=(x-y=100);
Console.WriteLine("The value is :" +r);
}
}
OUTPUT:
B) Expression x-(y=100)
CODING:
using System;
class edit
{
public static void Main()
{
int x=0;
int y;
int r;
4
1043006
r=x-(y=100);
Console.WriteLine("The value is :" +r);
}
}
OUTPUT:
PRACTICAL NO: - 05
TITLE:- Given the radius of 12.5cm.Write a program to compute it’s circumference and
area and gives its value.
CODING:
using System;
class edit
{
public static void Main()
{
float rad=12.5F;
double area,circum;
area=(3.14)*(rad*rad);
circum=2*3.14*rad;
5
1043006
OUTPUT :
PRACTICAL NO: - 06
TITLE = Write a program to add all the odd numbers from 0 to 20 use a simple if and
goto statements to form a loop of operations.
CODING:
using System;
class usegotoloop
{
public static void Main()
{
int x=0;
int count=0;
int sum=0;
ifgo: if(x<=20)
{
if(x%2!=0)
{
Sum=sum+x;
Count=count+1;
}
x=x+1;
6
1043006
goto ifgo;
}
Console.WriteLine ("The sum of odd numbers is:"+sum);
Console.WriteLine ("The total numbers of odd numberis:"+count);
}
}
OUTPUT:
PRACTICAL NO: - 07
CODING:
using System;
class edit
{
public static void Main()
{
int m,p,c;
int at,mpt;
Console.WriteLine("Enter the marks of the maths:");
m=(int.Parse)(Console.ReadLine());
7
1043006
at=(m+p+c);
mpt=(m+p);
if((m>=60 && p>=50 && c>=40) && (at>=200 || mpt>=150))
{
Console.WriteLine("You are eligible for the professional course admission ");
}
else
{
Console.WriteLine("You are not eligible for the professional course admission ");
}
}
}
OUTPUT:-
PRACTICAL NO: - 08
TITLE:- Write a Program to find total number of and sum of integer greater than 100 and
less than 200 that are divisible by 7..
CODING:-
using System;
class edit
{
public static void Main()
{
int count=1;
int sum=0;
for(int i=100;i<200;i++)
8
1043006
{
if(i%7==0)
{
count=count+1;
sum=sum+i;
}
}
Console.WriteLine("The total numbers that are divisible by 7 are : " + count);
Console.WriteLine("The sum of numbers that are divisible by 7 is : "+sum);
}
}
OUTPUT:-
PRACTICAL NO: - 09
TITLE = Write a Program to that will read the value of x and evaluates the function.
Y= {1if x>0}
{0 if x=0}
{-1 if x<0}
Using nested if statement else if statement ,conditional opearator.
CODING:
a) Using if statement
using System;
class nestedif
{
public static void Main()
{
int x,y;
9
1043006
OUTPUT:-
10
1043006
Console.WriteLine("y= "+0);
else
Console.WriteLine("y= "+(-1));
}
}
OUTPUT:-
OUTPUT:
11
1043006
PRACTICAL NO: - 10
CODING:
using System;
class DigitSum
{
public static void Main()
{
int x;
int rem;
int sum = 0;
Console.WriteLine("Enter The Number: ");
x = (int.Parse)(Console.ReadLine());
while (x > 0)
{
rem = x % 10;
sum = sum + rem;
x = x / 10;
}
Console.WriteLine("The addition of given digits of number is:" + sum);
}
}
12
1043006
OUTPUT:
TITLE:- Write a Program using do while loop to calculate and print the first m Fibonacci
numbers.
CODING:
using System;
class DigitSum
{
public static void Main()
{
int x,c=0;
int i=0;
int a=1;
int b=1;
Console.WriteLine("Enter The Number upto which you want to print the fibonacci
number:" );
x=(int.Parse)(Console.ReadLine());
Console.WriteLine("The fibonacci series is as follow:");
Console.Write(a+ " " + b + " ");
do
{
c=a+b;
a=b;
13
1043006
b=c;
Console.Write(c+ " ");
i++;
} while(i<(x-2));
}
}
OUTPUT:
TITLE:- Write a Program to print the following outputs using for loops
a) 1 c) 1 b)$ $ $ $
22 2 2 $$$
333 3 3 3 $$
4444 4 4 4 4 $
CODING:-
A)using System;
class pattern
{
public static void Main()
{
int x;
Console.Write("Enter The Number of rows : " );
x=(int.Parse)(Console.ReadLine());
for(int i=1;i<=x;i++)
{
14
1043006
for(int j=1;j<=i;j++)
{
Console.Write(i+ " ");
}
Console.WriteLine();
}
}
}
OUTPUT:
B)
CODING:
using System;
class pattern
{
public static void Main()
{
int x;
Console.Write("Enter The Number of rows : " );
x=(int.Parse)(Console.ReadLine());
for(int i=1;i<=x;i++)
{
for(int j=x;j>i;j--)
{
Console.Write(" ");
}
for(int k=1;k<=i;k++)
15
1043006
{
Console.Write(i+" ");
}
Console.WriteLine();
}
}
}
OUTPUT:
C) using System;
class pattern
{
public static void Main()
{
int x;
Console.Write("Enter The Number of rows : " );
x=(int.Parse)(Console.ReadLine());
for(int i=1;i<=x;i++)
{
for(int j=x;j>=i;j--)
{
Console.Write(" " + "$" + " ");
}
Console.WriteLine();
}
}
16
1043006
OUTPUT:
17
1043006
}
loop1:continue;
}
Console.WriteLine("Termination by BREAK");
}
}
OUTPUT:
TITLE:- Write a program using while loop to reverse the digit of a number.
CODING:-
using System;
class ContinueBreak
{
public static void Main()
{
int num;
int rev=0,rem;
Console.Write("Enter the number to reversed:");
num=(int.Parse)(Console.ReadLine());
while(num>0)
{
rem=num%10;
rev=rev*10+rem;
18
1043006
num=num/10;
}
Console.WriteLine("The reverse of the number is :"+rev);
}
}
OUTPUT:
CODING:-
using System;
class umat
{
public static void Main()
{
int num;
19
1043006
int k=1;
Console.Write("Enter the number of rows:");
num=(int.Parse)(Console.ReadLine());
for(int i=1;i<=num;i++)
{
for(int j=1;j<=i;j++)
{
Console.Write(k+" ");
k=k+1;
}
Console.WriteLine();
}
}
}
OUTPUT:
20
1043006
CODING:
using System;
class floyd
{
public static void Main()
{
int num;
Console.Write("Enter the number of rows:");
num=(int.Parse)(Console.ReadLine());
for(int i=1;i<=num;i++)
{
Console.Write(" ");
for(int j=1;j<=i;j++)
{
if(i%2==0)
{
if(j%2==0)
{
Console.Write(1+" ");
}
else
{
Console.Write(0+" ");
}
}
else
{
if(j%2==0)
{
Console.Write(0+" ");
}
else
{
Console.Write(1+" ");
}
}
}
Console.WriteLine();
}
}
}
21
1043006
OUTPUT :
CODING:
using System;
class umat
{
public static void Main()
{
int num;
int count=(num-1);
for(int i=1;i<=num;i++)
{
22
1043006
for(int j=1;j<=(i-1);j++)
{
Console.Write(0+" ");
}
Console.Write(1+" ");
for(int k=count;k>0;k--)
{
Console.Write(0+" ");
}
count=count-1;
Console.WriteLine();
}
}}
OUTPUT:
TITLE:- Write a program to calculate the standard deviation of an array of values. Use
methods “standard” and “mean” to calculate the standard deviation and mean of the values.
.
CODING:
using System;
class edit
{
public double mean(params int[] z)
{
23
1043006
int sum=0;
for(int i=0;i<z.Length;i++)
{
sum=sum+z[i];
}
double avg=sum/(double)(z.Length);
return avg;
}
int i;
int n=0;
int fx=0;
for(i=0;i<5;i++)
{
fx=fx+(f[i]*(x[i]*x[i]));
n=n+f[i];
}
24
1043006
double m=obj.mean(x);
Console.WriteLine("The mean is :" +m);
obj.standard(fx,n,m);
}
}
OUTPUT:
TITLE:- Write a program that uses to sort and array of integer ascending and descending
order.
.
CODING:
using System;
class umat
{
public static void Main()
{
int[] number={43,67,89,90,43,56};
int n=number.Length;
Console.Write("Given list:");
for(int i=0;i<n;i++)
{
Console.Write(" "+number[i]);
}
25
1043006
Console.Write("\n");
for(int i=0;i<n;i++)
{
for(int j=i+1;j<n;j++)
{
if(number[i]<number[j])
{
int temp=number[i];
number[i]=number[j];
number[j]=temp;
}
}
}
Console.Write("Sorted list:");
for(int i=0;i<n;i++)
{
Console.Write(" "+number[i]);
}
Console.WriteLine();
for(int j=n-1;j>=0;j--)
{
Console.Write(" "+number[j]);
}
Console.WriteLine();
}
}
26
1043006
OUTPUT:
TITLE:- Develop the program that uses a method to find a largest and a smallest number
from an array of integer.
CODING:
using System;
class umat
{
public static void Main()
{ umat obj=new umat();
int i;
int[] num=new int[5];
for(i=0;i<num.Length;i++)
{
Console.Write(" "+num[i]);
27
1043006
}
Console.WriteLine();
obj.find(num);
}
Public void find(params int[] num1)
{
int max=num1[0];
for(i=1;i<num1.Length;i++)
{
if(max < num1[i])
max=num1[i];
}
int min=num1[0];
for(i=1;i<num1.Length;i++)
{
if(min > num1[i])
min=num1[i];
}
}
}
OUTPUT:
28
1043006
TITLE:- Develop the program to search the number from array of integer.
CODING:
using System;
class umat
{
public static void Main()
{
int i;
int flag=1;
for(i=0;i<num.Length;i++)
{
if(num[i]==n)
{
flag=0;
break;
}
else
{
flag=1;
}
}
if(flag==0)
Console.WriteLine("The number is present in the array");
else
Console.WriteLine("The number is not present in the array");
}
}
OUTPUT:
29
1043006
TITLE:- Write a program that return a true if its argument is prime and return false
otherwise.
CODING:
using System;
class edit
{
public bool Prime(int x)
{
int flag=0;
bool r;
for(int i=2;i<x;i++)
{
if(x%i==0)
{
flag=1;
break;
}
else
{
flag=0;
30
1043006
}
}
if(flag==0)
r=true;
else
r=false;
return r;
Console.WriteLine(result);
}
}
OUTPUT:
31
1043006
TITLE:- Write a void type method that takes two int type values parameters and 1 int
type out parameter and return the product of two value parameter through the output
parameter.
CODING:
using System;
class edit
{
public void product(int x,int y, out int z)
{
z=(x*y);
}
public static void Main()
{
int a,b,c;
}
}
OUTPUT:
32
1043006
CODING:
using System;
class edit
{
public int Compare(params int[] z)
{
int min=z[0];
for(int i=1;i<z.Length;i++)
{
if(min > z[i])
min=z[i];
}
return min;
}
public static void Main()
{
int a,b,c;
}
}
OUTPUT:
33
1043006
CODING:
using System;
class edit
{
public int Compare(params int[] z)
{
int max=z[0];
for(int i=1;i<z.Length;i++)
{
if(max < z[i])
max=z[i];
}
return max;
}
public static void Main()
{
int a,b,c;
34
1043006
b=int.Parse(Console.ReadLine());
}
}
OUTPUT:
TITLE:- Write a method that takes an array as an input parameter uses 2 methods
a) To find largest array element
b) To compute the average of array element.
CODING:
using System;
class edit
{
public void avgmax(params int[] z)
35
1043006
int max=z[0];
int sum=z[0];
for(int i=1;i<z.Length;i++)
{
if(max < z[i])
{
max=z[i];
}
sum=sum+z[i];
}
Console.WriteLine("The maximum of the enter element is : "+max);
int avg=(sum)/(z.Length);
Console.WriteLine("The avarage of the entered number is :"+avg);
}
Console.WriteLine();
obj.avgmax(num);
}
}
OUTPUT:
36
1043006
TITLE:- Write a program to accept a shopping list of five items from the command line
and stored them in string type array and then print the list in alphabetical order.
CODING:
using System;
class edit
{
public static void Main(String [] arg)
{
int i;
if(arg.Length==5)
{
for(i=0;i<5;i++)
{
str[i]=arg[i];
}
}
else
{
Console.WriteLine("You can enter only five element at a time!!");
}
37
1043006
for(i=0;i<str.Length;i++)
{
Console.WriteLine(" "+str[i]);
}
Array.Sort(str);
for(i=0;i<str.Length;i++)
{
Console.WriteLine(" "+str[i]);
}
}
}
OUTPUT:
TITLE:- Create a class named Double. This class contains three double numbers.
Overload +,-,*,/ operators so that they can be applied to the objects of the class Double.
Write a c# program to test it.
CODING:-
CODING:-
using System;
class Double
38
1043006
{
double i;
double j;
Double(double d1, double d2)
{
this.i = d1;
this.j = d2;
}
public static Double operator +(Double r1,Double r2)
{
return new Double(r1.i+r2.i,r1.j+r2.j);
}
public static Double operator -(Double r1,Double r2)
{
return new Double(r1.i-r2.i,r1.j-r2.j);
}
public static Double operator *(Double r1,Double r2)
{
return new Double(r1.i*r2.i,r1.j*r2.j);
}
public static Double operator /(Double r1,Double r2)
{
return new Double(r1.i/r2.i,r1.j/r2.j);
}
public void displayOutput()
{
Console.WriteLine(i+ " " +j);
}
static void Main(string[] args)
{
Double s1 = new Double(5.7,16.5);
Double s2 = new Double(7.9,9.2);
Double s3 =s1 + s2;
Console.WriteLine("The sum of the two numbers: ");
s3.displayOutput();
s3=s1 - s2;
Console.WriteLine("The subtraction of the two numbers: ");
s3.displayOutput();
s3=s1 * s2;
Console.WriteLine("The multiplication of the two numbers: ");
s3.displayOutput();
s3=s1 / s2;
Console.WriteLine("The division of the two numbers: ");
s3.displayOutput();
}
}
39
1043006
OUTPUT:-
TITLE:- Create a class example to hold one integer value. Include a constructor to display
the message “Object is created” and a destructor to display the message “Object is
destroyed”. Write a c# program to demonstrate the creation and destruction of objects of
example class.
CODING:-
using System;
class example
{
int y;
example(int c)
{
y=c;
Console.WriteLine("\nOBJECT IS BORN.\n\n");
}
void x()
{
Console.WriteLine("value is " +y);
Console.WriteLine("NOW X IS ALIVE.\n");
}
~example()
{
Console.WriteLine("OBJECT DIES.");
}
static void Main(string[] args)
{
example z = new example(114);
40
1043006
z.x();
Console.ReadLine();
}
}
OUTPUT:-
CODING:-
using System;
class NumberNotInRange : System.Exception
{
public static void Main(string[] args)
{
int no = int.Parse(args[0]);
try
{
if(no<1 || no>100)
{
throw new NumberNotInRange();
}
}
catch(NumberNotInRange noExcep)
{
Console.WriteLine("Exception is caught");
Console.WriteLine("Exception message " + noExcep.Message);
}
41
1043006
}
}
OUTPUT:-
CODING:-
using System;
class FahrenCelcius
{
static void Main(string[] args)
{
double far,cel;
Console.WriteLine("Fahrenheit Celcius");
for(far = 98.5;far <= 102.0;far = far+0.1)
{
cel = (far - 32) / 1.8;
Console.WriteLine("{0,20}\t{1,20}",far, cel);
}
}
}
42
1043006
OUTPUT:-
CODING:-
using system;
class asterikprint
{
static void main(string[] args)
43
1043006
{
int i,sp,k,u;
u=5;
for(i=0;i<5;++i)
{
for(sp=5;sp>u;--sp)
{
console.write(" ");
}
for(k=sp;k>0;--k)
{
console.write(" *");
}
--u;
console.writeline();
}
}
}
OUTPUT:-
44
1043006
using System;
class StudMarksObt
{
public static void Main(string []args)
{
int[] students = { 45, 67, 89, 57, 34, 95, 0, 76, 24 };
int first = 0, second = 0, third = 0, fail = 0;
for (int i = 0; i < students.Length; i++)
{
if (students[i] >= 81 && students[i] <= 100)
first++;
if (students[i] >= 61 && students[i] <= 80)
second++;
if (students[i] >= 41 && students[i] <= 60)
third++;
if (students[i] >= 0 && students[i] <= 40)
fail++;
}
Console.WriteLine("No. of students obt. mrks bet 81 and 100 are :" + first);
Console.WriteLine("No. of students obt. mrks bet 61 and 80 are :" + second);
Console.WriteLine("No. of students obt. mrks bet 41 and 60 are :" + third);
Console.WriteLine("No. of students obt. mrks bet 0 and 40 are :" + fail);
Console.Read();
}
OUTPUT:-
45
1043006
CODING:-
using system;
interface addition
{
int add();
}
interface subtraction
{
int sub();
}
interface multiplication
{
int multiply();
}
interface division
{
int divide();
}
class calculator : addition, subtraction, multiplication, division
{
int a, b;
public calculator(int x, int y)
{
this.a = x;
this.b = y;
46
1043006
}
public int add()
{
return (a + b);
}
public int sub()
{
return (a - b);
}
public int multiply()
{
return (a * b);
}
public int divide()
{
return (a / b);
}
}
class prac40
{
public static void main()
{
calculator c = new calculator(8, 3);
addition ad1 = (addition)c;
console.writeline("addition={0}", ad1.add());
subtraction sub1 = (subtraction)c;
console.writeline("subtraction={0}", sub1.sub());
multiplication mul1 = (multiplication)c;
console.writeline("multiplication={0}", mul1.multiply());
division div1 = (division)c;
console.writeline("division={0}", div1.divide());
console.readline();
}
}
OUTPUT:-
47
1043006
CODING:-
using system;
class complex
{
private int x;
private int y;
public complex()
{
}
public complex(int i,int j)
{
x = i;
y = j;
}
public void showxy()
{
console.writeline("{0} {1}",x,y);
}
public static complex operator-(complex c)
{
complex temp=new complex();
temp.x=-c.x;
temp.y=-c.y;
return temp;
48
1043006
}
class prac41
{
public static void main()
{
complex c1=new complex(10, 20);
c1.showxy();
complex c2=new complex();
c2.showxy();
c2=-c1;
c2.showxy();
console.readline();
}
}
OUTPUT:-
CODING:-
using System;
using System.Collections;
class MainClass
{
public static void Main()
{
49
1043006
}
}
}
OUTPUT:-
TITLE:- Write a program to create a class which can hold a complex number (X+iY) and
using operator overloading perform the addition and subtraction of two complex numbers.
CODING:-
using System;
public struct Complex
{
public int real;
public int imaginary;
50
1043006
OUTPUT:-
51
1043006
CODING:-
using System;
interface power
{
int square(int a);
int cube(int b);
double pows(double m, int n);
}
class Demo : power
{
public int square(int a)
{
return(a * a);
}
public int cube(int b)
{
return(b * b * b);
}
public double pows(double g, int n)
{
52
1043006
for(int i=1;i<=n;i++)
g=g *i;
return(g);
}
}
class PowerInt
{
static void Main(string[] args)
{
Demo dem = new Demo();
power pow;
pow = dem as power;
Console.WriteLine("Square of no. ={0}" ,pow.square(3));
Console.WriteLine("cube of no = {0}",pow.cube(7));
Console.WriteLine("power of no = {0:0.00}" , pow.pows(2.7,2));
}
}
OUTPUT:-
TITLE:- Write a class Date that includes data members day, month and year and methods
that could implement the following tasks.
(i) Read a date from keyboard.
(ii) Display a date
(iii) Increment a date by one day.
(iv) Compare two dates to see which is greater.
Write a main method to check your code.
53
1043006
CODING:-
using System;
class Date
{
static void Main(string[] args)
{
Console.WriteLine("Please enter the date dd/mm/yyyy");
string k= Console.ReadLine();
Console.WriteLine("First Date entered is {0}", k);
string p= Console.ReadLine();
Console.WriteLine("Second Date entered is {0}", p);
if(k.CompareTo(p)>0)
Console.WriteLine("{0} is greater than {1} ", k, p);
else
Console.WriteLine("{0} is greater than {1} ", p, k);
Console.ReadLine();
}
}
OUTPUT:-
TITLE:- Given are two one dimensional arrays ‘A’ and ‘B’ which are in sorted order.
Write a program to merge them into a single sorted array ‘C’ that contains every item from
arrays ‘A’ and ‘ B’ in ascending order.
CODING:-
54
1043006
using System;
class arrayMerge
{
static void Main(string[] args)
{
int i,j,k;
int[] A={14,32,53,114,53};
int[] B={60,67,88,79,170};
int[] C=new int[10];
for(i=0;i<A.Length;i++)
{
C[i]=A[i];
}
for(j=i,k=0;j<(i+B.Length);j++,k++)
{
C[j]=B[k];
}
Array.Sort(C);
for(int h=0;h<C.Length;h++)
Console.Write(C[h] + " ");
}
}
OUTPUT:-
TITLE:- Create a class named ‘Integer’ to hold two integer values. Design a method ‘Swap’
that could take two integer objects as parameters and swap their contents. Write a main
program to implement class Integer and method Swap.
CODING:-
using System;
class Integer1
55
1043006
{
int g;
int h;
Integer1(int i1, int i2)
{
this.g = i1;
this.h = i2;
}
static void swap(Integer1 x, Integer1 y)
{
int temp1, temp2;
temp1 = x.g;
temp2 = x.h;
x.g=y.g;
x.h=y.h;
y.g=temp1;
y.h=temp2;
}
void display()
{
Console.WriteLine(g + " and " + h);
}
static void Main(string[] args)
{
Integer1 t1 = new Integer1(5,7);
Integer1 t2 = new Integer1(9,12);
t1.display();
t2.display();
swap(t1, t2);
t1.display();
t2.display();
}
}
OUTPUT:-
56
1043006
CODING:-
using System;
class NoMatchException : System.Exception
{
public static void Main(string[] args)
{
string str = Console.ReadLine();
try
{
if(!(str.Equals("TYBSCIT")))
{
throw new NoMatchException();
}
else
Console.WriteLine("You entered the string {0} ",str);
}
catch (NoMatchException noMExcep)
{
Console.WriteLine("Exception is caught");
{
Console.WriteLine("Exception message"+noMExcep.Message);
Console.WriteLine("Not entered TYBSCIT instead entered string{0}",str);
}
}
}
57
1043006
OUTPUT:-
58