KEMBAR78
C#Programs | PDF | C Sharp (Programming Language) | Constructor (Object Oriented Programming)
0% found this document useful (0 votes)
15 views18 pages

C#Programs

The document contains multiple C# programs demonstrating various programming concepts such as calculating averages, swapping numbers, checking odd/even status, finding the greatest of three numbers, checking for prime numbers, calculating factorials, and more. Each program includes code snippets and sample outputs for user inputs. The programs also illustrate method overloading, constructor overloading, static and non-static methods, access modifiers, and the use of 'this' and 'base' keywords.

Uploaded by

shubhamjoc2003
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)
15 views18 pages

C#Programs

The document contains multiple C# programs demonstrating various programming concepts such as calculating averages, swapping numbers, checking odd/even status, finding the greatest of three numbers, checking for prime numbers, calculating factorials, and more. Each program includes code snippets and sample outputs for user inputs. The programs also illustrate method overloading, constructor overloading, static and non-static methods, access modifiers, and the use of 'this' and 'base' keywords.

Uploaded by

shubhamjoc2003
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/ 18

LAB-01 PROGRAM 1

W.A.P in C# to find the average to given numbers.

CODE
class Program
{
public static void average()
{
double sum = 0;
Console.WriteLine("How many numbers? ");
int n = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Enter Each Number now: ");
double[] array = new double[n];
for (int i = 0; i < n; i++)
{
array[i] = Convert.ToDouble(Console.ReadLine());
sum += array[i];
}
Console.WriteLine("Average is = " + sum / n);

}
static void Main(string[] args)
{
average();
}
}
OUTPUT

How many numbers?


5
Enter Each Number now:
2
4
2
8
3
Average is = 3.8
LAB-01 PROGRAM 2
W.A.P in C# to swap two numbers.

CODE
class Program
{
public static void SwapTwoNumbers()
{
Console.WriteLine("Enter Value of A: ");
int a = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Enter Value of B: ");
int b = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Before Swap a = " + a + " and b = " + b);
int c = a;
a = b;
b = c;
Console.WriteLine("After Swap a = " + a + " and b = " + b);

}
static void Main(string[] args)
{
SwapTwoNumbers();
}
}
OUTPUT

Enter Value of A:
5
Enter Value of B:
6
Before Swap a = 5 and b = 6
After Swap a = 6 and b = 5
LAB-01 PROGRAM 3
W.A.P in C# to check if a number is odd or even.

CODE
class Program
{
public static void CheckOddEven()
{
Console.WriteLine("Enter Number: ");
int a = Convert.ToInt32(Console.ReadLine());
if (a % 2 == 0)
{
Console.WriteLine("It's a Event Number");
}
else
{
Console.WriteLine("It's a Odd Number");
}
}
static void Main(string[] args)
{
CheckOddEven();
}
}
OUTPUT

Enter Number:
67
It's a Odd Number
LAB-01 PROGRAM 4
W.A.P in C# to find the greatest of three numbers.

CODE
class Program
{
public static void GreatestOfThree()
{
Console.WriteLine("Enter 3 Number: ");
int a = Convert.ToInt32(Console.ReadLine());
int b = Convert.ToInt32(Console.ReadLine());
int c = Convert.ToInt32(Console.ReadLine());

int max = a;
if (b > max) max = b;
if (c > max) max = c;

Console.WriteLine("Max is = " + max);


}
static void Main(string[] args)
{
GreatestOfThree();
}
}
OUTPUT

Enter 3 Number:
8
4
2
Max is = 8
LAB-01 PROGRAM 5
W.A.P in C# to check if A number is prime or not.

CODE
class Program
{
public static void CheckPrime()
{
Console.WriteLine("Enter A Number");
int a = int.Parse(Console.ReadLine());
for (int i = 2; i < Math.Sqrt(a); i++)
{
if (a % i == 0)
{
Console.WriteLine("Not Prime");
return;
}
}
Console.WriteLine("Prime Number");
}
static void Main(string[] args)
{
CheckPrime();
}
}
OUTPUT

Enter A Number
5
Prime Number
LAB-01 PROGRAM 6
W.A.P in C# to find the factorial of a given number.

CODE
class Program
{
public static void FindFactorial()
{
Console.WriteLine("Enter Number");
int a = int.Parse(Console.ReadLine());
int ans = 1;
for (int i = 2; i <= a; i++)
{
ans *= i;
}
Console.WriteLine(a + "! = " + ans);
}
static void Main(string[] args)
{
FindFactorial();
}
}
OUTPUT

Enter Number
5
5! = 120
LAB-01 PROGRAM 7
W.A.P in C# to print your name 10 times.

CODE
class Program
{
static void Main(string[] args)
{
for(int i=0;i<10;i++)
{
Console.WriteLine("Hello Vijay!");
}
}
}
OUTPUT

Hello Vijay!
Hello Vijay!
Hello Vijay!
Hello Vijay!
Hello Vijay!
Hello Vijay!
Hello Vijay!
Hello Vijay!
Hello Vijay!
Hello Vijay!
LAB-02 PROGRAM 1
W.A.P in C# to find sum of even and odd numbers.

CODE
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Enter Size of Array: ");
int n = int.Parse(Console.ReadLine());
Console.WriteLine("Enter Array Elements");
int[] a = new int[n];
int sum1 = 0, sum2 = 0;
for (int i = 0; i < n; i++)
{
a[i] = Convert.ToInt32(Console.ReadLine());
if (a[i] % 2 == 0) { sum1 += a[i];}
else{sum2 += a[i];}
}
Console.WriteLine("Sum of Even Numbers: " + sum1);
Console.WriteLine("Sum of Odd Numbers: " + sum2);
}
}
OUTPUT
Enter Size of Array:
5
Enter Array Elements
1
2
3
4
5
Sum of Even Numbers: 6
Sum of Odd Numbers: 9
LAB-02 PROGRAM 2
W.A.P in C# to print sum of digit of number.

CODE
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Enter a Number:");
int n = int.Parse(Console.ReadLine());
int sum = 0;
while (n != 0)
{
sum += n % 10;
n /= 10;
}
Console.WriteLine("Sum of digit is = " + sum);
}
}
OUTPUT

Enter a Number:
543
Sum of digit is = 12
LAB-02 PROGRAM 3
W.A.P in C# to print reverse of a number.

CODE
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Enter a Number: ");
int n = int.Parse(Console.ReadLine());
int reverse = 0;
while (n != 0)
{
reverse = reverse * 10 + n % 10;
n /= 10;
}
Console.WriteLine("Reverse is " + reverse);
}
}
OUTPUT

Enter a Number:
3454
Reverse is 4543
LAB-02 PROGRAM 4
W.A.P in C# to sum digit of a number until it becomes single digit.

CODE
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Enter a Number: ");
int n = int.Parse(Console.ReadLine());
int sum = 0;
while (n >= 10)
{
int a = n;
sum = 0;
while (a != 0)
{
sum += a % 10;
a /= 10;
}
n = sum;
}
Console.WriteLine("Answer is = " + sum);
}
}
OUTPUT

Enter a Number:
19
Answer is = 1
LAB-02 PROGRAM 5
W.A.P in C# to check if a Number is palindrome or not.

CODE
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Enter a Number: ");
int n = int.Parse(Console.ReadLine());
int reverse = 0;
int a = n;
while (n != 0)
{
reverse = reverse * 10 + n % 10;
n /= 10;
}
if (a == reverse)
{
Console.WriteLine("Number is a Palindrome");
}
else
{
Console.WriteLine("Number is Not a Palindrome");
}
}
}
OUTPUT

Enter a Number:
546
Number is Not a Palindrome
LAB-03 PROGRAM 1
W.A.P in C# to show method overloading.

CODE
class Program
{
public static int sum(int a, int b)
{
return a + b;
}
public static int sum(int a, int b, int c)
{
return a + b + c;
}
static void Main(string[] args)
{
Console.WriteLine("Function sum(2,3) = " + sum(2, 3));
Console.WriteLine("Function sum(2,3,4) = " + sum(2, 3,4));

}
}
OUTPUT

Function sum(2,3) = 5
Function sum(2,3,4) = 9
LAB-03 PROGRAM 2
W.A.P in C# to show constructor overloading.

CODE
class Program
{
Program() {
Console.WriteLine("Constructor with no arguments");
}
Program(string name) {
Console.WriteLine("Hi "+ name + " Constructor with 1 arguments");

}
static void Main(string[] args)
{
Program obj1 = new Program();
Program obj2 = new Program("Vijay");

}
}
OUTPUT

Constructor with no arguments


Hi Vijay Constructor with 1 arguments
LAB-03 PROGRAM 3
W.A.P in C# to use static and non-static method.

CODE
class Program
{

public static void print1() {


Console.WriteLine("This is a static method");
}
public void print2() { Console.WriteLine("This is a non static method"); }
static void Main(string[] args)
{

Program obj1 = new Program();


print1();
obj1.print2();

}
}
OUTPUT

This is a static method


This is a non static method
LAB-03 PROGRAM 4
W.A.P in C# to display all access modifiers.

CODE
class Demo1
{
public void A()
{
Console.WriteLine("A public function");
}
private void B()
{
Console.WriteLine("A private function");
}
internal void C()
{
Console.WriteLine("A Internal Function");
}
protected void D()
{
Console.WriteLine("This is protected Function");
}
protected private void E()
{
Console.WriteLine("This is protected private/private protected function");
}
protected internal void F()
{
Console.WriteLine("This is protected internal Function");

}
}

class Demo2:Demo1
{
public void print()
{
Console.WriteLine("Inside Inhertied Class Protected can be accessed");
A();
C();
D();
E();
}
}
public class Program
{

static void Main(string[] args)


{
Demo1 demo1 = new Demo1();
demo1.A();
demo1.C();
demo1.F();
Demo2 demo2 = new Demo2();
demo2.print();
}

}
OUTPUT

A public function
A Internal Function
This is protected internal Function
Inside Inhertied Class Protected can be accessed
A public function
A Internal Function
This is protected Function
This is protected private/private protected function
LAB-03 PROGRAM 5
W.A.P in C# to use this and super keyword.

CODE
class SuperAndThis
{
public SuperAndThis() { }
protected int a;

public void print()


{
Console.WriteLine("Value of a = " + this.a);
}
}
class Demo : SuperAndThis
{
int b;
public Demo(int a, int b)
{
base.a = a;
this.b = b;
}
public void print()
{
Console.WriteLine("Value of base.a = " + base.a);
Console.WriteLine("Value of b = " + this.b);
}
}
public class Program
{
static void Main(string[] args)
{
Demo obj = new Demo(2, 3);
obj.print();
}
}
OUTPUT
Value of base.a = 2
Value of b = 3

You might also like