0% found this document useful (0 votes)
22 views28 pages

C# Basic Programming Examples

This document contains code for a C# calculator program with a graphical user interface. The program creates labels, textboxes and buttons at specified locations to display values, accept user input, and perform addition. When the user clicks the "Add" button, it converts the textbox values to integers, adds them, and displays the result in the third textbox. The "Show" button makes all the interface elements visible.

Uploaded by

DiveshDutt
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)
22 views28 pages

C# Basic Programming Examples

This document contains code for a C# calculator program with a graphical user interface. The program creates labels, textboxes and buttons at specified locations to display values, accept user input, and perform addition. When the user clicks the "Add" button, it converts the textbox values to integers, adds them, and displays the result in the third textbox. The "Show" button makes all the interface elements visible.

Uploaded by

DiveshDutt
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

1-PROGRAM TO ADDITION,SUBTRACTION,MULTIPICATION,DIVISION

using System;
using [Link];
using [Link];
using [Link];
namespace add_mul_sum_div
{
class Program
{
static void Main(string[] args)
{
int a, b;
a = 10;
b = 4;
[Link]("Addition={0}", a + b);
[Link]("Subtraction={0}", a - b);
[Link]("Multiply={0}", a * b);
[Link]("Division={0}",(float)a / b);
}
}
}

Output :

2-PROGRAM TO INTERCHANGE VALUE


using System;
using [Link];
using [Link];
using [Link];
namespace interchange_Num
{
class Program
{
static void Main(string[] args)
{
int a, b;
a = 20;
b = 5;
[Link]("Before Interchange A = {0} And B = {1}", a, b);
a = a + b;
b = a - b;
a = a - b;
[Link]("After Interchange A = {0} And B = {1}", a, b);
[Link]();
}
}
}

Output :

3 -PROGRAM TO SUM OF FIRST TEN EVEN NATURAL NO. SQUARE


using System;
using [Link];
using [Link];
using [Link];
namespace eventensqure
{
class Program
{
static void Main(string[] args)
{
int sum = 0;
int even = 2;
for (int i = 1; i <= 10; i++)
{
sum = sum + even * even;
[Link]( "Sum Of even natural number's Square = {0}", sum);
even += 2;
}
[Link]();
}
}
}

Output :

4 PROGRAM TO PRIME NUMBER


using System;
using [Link];
using [Link];
using [Link];
namespace Primenumber2
{
class Program
{
static void Main(string[] args)
{
[Link]("Enter the value");
int limit =Convert.ToInt32([Link]());
[Link]("Prime numbers between 1 and " + limit);
for(int i=1; i < limit; i++)
{
Boolean isPrime = true;
for(int j=2; j < i ; j++)
{
if(i % j == 0)
{
isPrime = false;
break;
}
}
if(isPrime)
[Link](i + " ");
}
}
}
}

Output :

5 PROGRAM TO SERIES OF NUMBER


using System;
using [Link];
using [Link];
using [Link];
namespace forbelow
{
class Program
{
static void Main(string[] args)
{
[Link]("Enter The value");
int n = Convert.ToInt32([Link]());
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= i; j++)
{
[Link]("{0,3}", j);
}
[Link]();
}
}
}
}

Output :

6 PROGRAM TO FACTORIAL OF N
using System;
using [Link];
using [Link];
using [Link];
namespace Dynamic_Factorial
{
class Program
{
static void Main(string[] args)
{
[Link]("Enter the number: ");
int a = Convert.ToInt32([Link]());
int result= fact(a);
[Link]("Factorial of the number is: " + result);
}
static int fact(int b)
{
if(b <= 1)
return 1;
else
return b * fact(b-1);
}
}
}

Output :

7 PROGRAM TO TRAINGALE
using System;
using [Link];
using [Link];
using [Link];
namespace Triangle
{
class Program
{
static void Main(string[] args)
{
[Link]("Enter the charecter\n");
string str1 = [Link]();
int i = 0;
char[] arr = new char[10];
foreach (char s in str1)
arr[i++] = s;
for (i = 0; i < [Link]; i++)
{
for (int j = 0; j <= i; j++)
[Link](arr[j]);
[Link]();
}
}
}
}

Output :

8 PROGRAM TO MULTILEVEL INHERITANCE


using System;
using [Link];
using [Link];
using [Link];
namespace Inheritance
{
class A
{
int i, j;
public void Nand()
{
[Link]("This is Inheritance \n");
for (i = 0; i < 5; i++)
{
for (j = 5; j > i; j--)
{
[Link](j);
}
[Link]();
}
}
}
class B : A
{
public void Saxena()
{
for (int i = 5; i > 0; i--)
{
for (int j = 0; j < i; j++)
{
[Link](j);
}
[Link]();
}
}
}
class C : B
{
public void Bramha()
{
int i, j;
for ( i = 1; i <= 5; i++)
{
for ( j = i; j >= 1; j--)
{
8

[Link](j);
}
[Link]();
}
}
}
class Program
{
static void Main(string[] args)
{
C obj = new C();
[Link]();
[Link]();
[Link]();
[Link]();
}
}
}

Output :

9 PROGRAM TO INTERFACE
using System;
using [Link];
using [Link];
using [Link];
namespace interface_me
{
interface A
{
void me();
}
interface B
{
void show();
}
class C : B, A
{
public void me()
{
[Link]("I LOVE MY India");
}
public void show()
{
[Link]("My Name is [Link]");
}
}
class Program
{
static void Main(string[] args)
{
C obj = new C();
[Link]();
[Link]();
[Link]();
}
}}

Output :
10

10 PROGRAM TO DEFAULT & ARGUMENT CONSTRUCTOR


using System;
using [Link];
using [Link];
using [Link];
namespace Argument_Constructor
{
class Mul
{
int a, b;
public Mul()
{
a = 12;
b = 20;
}
public void display()
{
[Link]("Default Constructor");
[Link](a);
[Link](b);
}
public Mul(int x, int y)
{
int z = x * y;
[Link]("This is Argument Constructor");
[Link](z);
}
}
class Program
{
static void Main(string[] args)
{
Mul obj = new Mul();
Mul ob = new Mul(4,5);
[Link]();
}
}}

Output :

11

11- PROGRAM TO COPY,DEFAULT & ARGUMENT CONSTRUCTOR


using System;
using [Link];
using [Link];
using [Link];
namespace Copy_Constructor
{
class BN
{
int a, b;
public BN()
{
a = 20;
b = 30;
}
public void display()
{
[Link]("Default Constructor");
[Link](a + b);
}
public BN(int x, int y)
{
int z = x - y;
[Link]("Argument Constructor =" + z);
}
public BN(BN obj)
{
int d = obj.a;
int e = obj.b;
[Link](d+"=Copy Constructor ="+e);
}
class Program
{
static void Main(string[] args)
{
BN obj = new BN();
[Link]();
BN obj1 = new BN(100, 60);
BN obj2 = new BN(obj);
} } } }

Output :
12

12 PROGRAM TO ABSTRACT
using System;
using [Link];
using [Link];
using [Link];
namespace abstracclass
{
using System;
using [Link];
using [Link];
using [Link];
namespace Abstrt
{
abstract class GN
{
public abstract void nand();
public abstract void show();
}
class BN : GN
{
int i, j;
public override void nand()
{
[Link]("THis is Solution");
}
public override void show()
{
for (i = 5; i >= 0; i--)
{
for (j = 0; j <= i; j++)
{
[Link](i);
}
[Link]();
}
}
}
class ZN : BN
{
int x, y;
public override void nand()
{
x = 12;
y = 11;
}
13

public override void show()


{
[Link]("This is value ");
[Link]("x=" + x);
[Link]("This is value ");
[Link]("y=" + y);
}
}
class Program
{
static void Main(string[] args)
{
BN obj = new BN();
[Link]();
[Link]();
ZN obj1 = new ZN();
[Link]();
[Link]();
[Link]();
}
}
}
}

Output :

14

13- PROGRAM TO DELEGATE


using System;
using [Link];
using [Link];
using [Link];
delegate int Area(int x,int y);
delegate int Area1(int x);
namespace delegate
{
class B
{
public static int Rectangle(int x, int y)
{
return (x * y);
}
public static int Triangle(int x, int y)
{
int s=((1/2)*(x * y));
return s;
}
public static int Circle(int x)
{
int z = (22 / 7) * (x * x);
return z;
}
}
class Program
{
static void Main(string[] args)
{
Area obj1 = new Area([Link]);
Area obj2 = new Area([Link]);
Area1 obj3 = new Area1([Link]);
int a=obj1(4,5);
int b=obj2(2,3);
int c=obj3(7);
[Link]("Ractangle="+a);
[Link]("Triangle="+b);
[Link]("Circle="+c);
[Link]();
}
}
}

15

Output :

16

14 - PROGRAM TO MULTICAST DELEGATES


using System;
using [Link];
using [Link];
using [Link];
delegate void Mdelegate();
namespace Multidelegate
{
class Bramha
{
public static void Nand()
{
[Link]("Lucknow");
}
public static void Show()
{
[Link]("Mumbai");
}
}
class Program
{
static void Main(string[] args)
{
Mdelegate m1 = new Mdelegate([Link]);
Mdelegate m2 = new Mdelegate([Link]);
Mdelegate m3 = m1 + m2;
Mdelegate m4 = m1 - m2;
[Link]("This is Multicast delegates \n");
m3();
m4();
[Link]();
}
}
}

Output :

17

15 PROGRAM TO UNARY OPERATOR


using System;
using [Link];
using [Link];
using [Link];
namespace [Link]
{
class Calu
{
public int number1, number2;
public Calu(int num1, int num2)
{
number1 = num1;
number2 = num2;
}
public static Calu operator-(Calu c1)
{
c1.number1 = -c1.number1;
c1.number2 = -c1.number2;
return c1;
}
public void show()
{
[Link]("Number1=" + number1);
[Link]("Number2=" + number2);
}
}
class Program
{
static void Main(string[] args)
{
Calu obj = new Calu(20, -30);
[Link]("Unary Operator \n");
obj = -obj;
[Link]();
[Link]();
}
}
}

Output :

18

16 - PROGRAM TO CALCULATOR
using System;
using [Link];
using [Link];
using [Link];
using [Link];
using [Link];
using [Link];
using [Link];
namespace Excel
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
Label l;
Label p;
Label q;
TextBox t;
TextBox r;
TextBox s;
private void Form1_Load(object sender, EventArgs e)
{
l = new Label();
[Link] = new Point(100, 70);
[Link]=("Value1");
[Link](l);

p = new Label();
[Link] = new Point(100, 110);
[Link] = ("Value2");
[Link](p);
q = new Label();
[Link] = new Point(100, 150);
[Link] = ("Result");
[Link](q);
t = new TextBox();
[Link] = new Point(200, 70);
[Link] = (" ");
[Link](t);
r = new TextBox();
19

[Link] = new Point(200, 110);


[Link] = (" ");
[Link](r);
s = new TextBox();
[Link] = new Point(200, 150);
[Link] = (" ");
[Link](s);
[Link] = false;
[Link] = false;
[Link] = false;
[Link] = false;
[Link] = false;
[Link] = false;
}
private void button1_Click(object sender, EventArgs e)
{
int a, b, c;
a = Convert.ToInt32([Link]);
b = Convert.ToInt32([Link]);
c = a + b;
[Link] = [Link]();
}
private void button5_Click(object sender, EventArgs e)
{
[Link] = true;
[Link] = true;
[Link] = true;
[Link] = true;
[Link] = true;
[Link] = true;
}
private void button2_Click(object sender, EventArgs e)
{
int a, b, c;
a = Convert.ToInt32([Link]);
b = Convert.ToInt32([Link]);
c = a - b;
[Link] = [Link]();
}
20

private void button3_Click(object sender, EventArgs e)


{
int a, b, c;
a = Convert.ToInt32([Link]);
b = Convert.ToInt32([Link]);
c = a * b;
[Link] = [Link]();
}
private void button4_Click(object sender, EventArgs e)
{
int a, b;
float c;
a = Convert.ToInt32([Link]);
b = Convert.ToInt32([Link]);
c = a / b;
[Link] = [Link]();
}
}
}

Output :

21

17- PROGRAM TO CHANGE DIFFERENT TYPE COLOR


using System;
using [Link];
using [Link];
using [Link];
using [Link];
using [Link];
using [Link];
using [Link];
namespace Color
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
ColorDialog c = new ColorDialog();
[Link]();
[Link] = [Link];
[Link] = [Link];
}
}
}

Output :

22

18- PROGRAM TO CALCULATE BILL


using System;
using [Link];
using [Link];
using [Link];
using [Link];
using [Link];
using [Link];
using [Link];
namespace Bill
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
[Link]("Chinees");
[Link]("South Indian");
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if ([Link] == "Chinees")
{
[Link]();
[Link]("CHOWMEEN");
[Link]("XYZ");
}
if ([Link] == "South Indian")
{
[Link]();
[Link]("IDLI");
[Link]("DOSHA");
}
}
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
[Link] = [Link];
[Link] = [Link];
if ([Link]() == "CHOWMEEN")
[Link] = [Link](80);
else if ([Link]() == "XYZ")
23

[Link] = [Link](100);
else if ([Link]() == "IDLI")
[Link] = [Link](60);
else if ([Link]() == "DOSHA")
[Link] = [Link](70);
else
[Link] = [Link];
}
private void button1_Click(object sender, EventArgs e)
{
if ([Link] != "")
[Link]([Link]);
else
[Link]("Please first calculate amount");
}
private void button2_Click(object sender, EventArgs e)
{
int sum = 0;
foreach (string n in [Link])
{
sum = sum + Convert.ToInt32(n);
}
[Link] = "Rs. "+[Link]()+" Only";
}
private void tbqty_TextChanged(object sender, EventArgs e)
{
if ([Link] != "")
{
int a, b, c;
a = Convert.ToInt32([Link]);
b = Convert.ToInt32([Link]);
c = a * b;
[Link] = [Link]();
}
else
[Link] = [Link];
}
private void button5_Click(object sender, EventArgs e)
{
[Link] = [Link];
[Link] = [Link];
[Link]();
[Link] = [Link];
}
24

private void tbqty_Leave(object sender, EventArgs e)


{
[Link]([Link]);
}
}
}

Output :

25

18 PROGRAM TO LISTBOX
using System;
using [Link];
using [Link];
using [Link];
using [Link];
using [Link];
using [Link];
using [Link];
namespace ListBoxDemo
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
[Link]([Link]());
[Link]([Link]);
[Link] = true;
}
private void button2_Click(object sender, EventArgs e)
{
[Link]([Link]());
[Link]([Link]);
}
private void button3_Click(object sender, EventArgs e)
{
foreach(string n in [Link])
{
[Link]([Link]());
}
[Link]();
}
private void button4_Click(object sender, EventArgs e)
{
foreach (string n in [Link])
{
[Link]([Link]());
}
26

[Link]();
}
private void Form1_Load(object sender, EventArgs e)
{
[Link] = true;
}
}
}

Output :

27

20 PROGRAM TO IMAGE SHOW


using System;
using [Link];
using [Link];
using [Link];
using [Link];
using [Link];
using [Link];
using [Link];
namespace picture
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
[Link] = new Bitmap(@"C:\Users\BRAMHA\Pictures\New folder\[Link]");
[Link] = true;
}
private void pictureBox1_Click(object sender, EventArgs e)
{
[Link] = false;
}
}
} Output

28

Common questions

Powered by AI

The program encapsulates methods for calculating areas of a rectangle, triangle, and circle within class 'B'. Delegates named 'Area' and 'Area1' provide a way to reference these methods. Encapsulation is shown by hiding the complexities of area calculations and delegating this responsibility through delegates, providing an interface to execute these methods externally without exposing the implementation details .

The triangle area method in the delegate calculation uses integer division `(1/2)*(x * y)`, which results in zero due to integer truncation. To correct this calculation error, floating-point division must be used, either by casting integers to float or using a multiplication factor like `0.5f`, changing the expression to `(0.5f)*(x * y)` . This ensures the division results in correct decimal output instead of truncation.

The program demonstrates the use of abstract classes through 'GN', which has abstract methods 'nand()' and 'show()'. These methods are overridden in the derived class 'BN'. Additionally, 'ZN' further inherits from 'BN', showing a layered abstraction. Although no interfaces are explicitly used, abstract classes function similarly by defining a contract for subclasses . Interfaces could solidify this structure by allowing multiple inheritance, integrating seamlessly with abstract classes.

The program uses a multilevel inheritance hierarchy where class 'C' inherits from class 'B', which inherits from class 'A'. Each class can call its parent's methods. In the 'Program' class, an instance of 'C' is created which allows invoking methods from all three levels: 'obj.Nand()' (from 'A'), 'obj.Saxena()' (from 'B'), and 'obj.Bramha()' (from 'C'). Method override occurs when 'C' and 'B' classes modify the behavior of the inherited methods.

The GUI-based calculator extends basic arithmetic by allowing users to input values via text boxes and perform operations through button inputs corresponding to arithmetic functions (addition, subtraction, multiplication, division). Each operation is linked to a button's click event handler which processes user input, performs calculations, and displays results in another text box . This interactive approach enhances user engagement by visualizing calculation processes typically done within console applications.

The program demonstrates variable interchange by altering the values of variables 'a' and 'b' using arithmetic operations. Initially, 'a' is set to 20 and 'b' to 5. The values are swapped by incrementing 'a' by 'b', setting 'b' to the new 'a' minus 'b', and finally setting 'a' to 'a' minus the new 'b' . This approach exemplifies the arithmetic swapping technique.

The program demonstrates the impact of using default and argument constructors by initializing class 'Mul' in two ways. The default constructor sets fields 'a' and 'b' to predefined values (12 and 20), displayed by 'display()'. The argument constructor, explicitly called with values 4 and 5, computes a product but these values are not stored within object state, only printed . This flexibility illustrates constructors' role in object creation and state initialization.

The program illustrates event-driven programming by using a ColorDialog component to change the application's background and foreground text colors upon a button click event. The `button1_Click` method, tied to the button event, launches a color picker dialog, and once a color is chosen, it updates the form's background and text box's forecolor to the selected color . This demonstrates user interaction with GUI elements in a C# application environment.

Mathematical operations in the program are executed by handlers for different button click events, each encapsulated within a method performing operations like addition, subtraction, multiplication, and division; polymorphism is achieved by these methods operating on shared UI components like text boxes. When buttons are clicked, the relevant method is invoked dynamically to perform the operation using the entered numbers, showcasing polymorphism through method overloading in interface-based commands .

Polymorphism is illustrated through event handling methods assigned to user actions such as button clicks, each method behaving differently based on the context (e.g., moving items between list boxes). The `button1_Click` and `button2_Click` methods respectively transfer items from one list box to another and vice versa, overriding behavior based on the event invoked . This dynamic method overriding exemplifies polymorphism in action.

You might also like