Practical No. 1 Khan Sahibe Alam: Using Using Using Using Using Namespace Class Static Void String Int Int Float String

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 5

Practical No. 1 Khan Sahibe alam Div.

: B

Aim: Console based calculator.

Program Code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleCalC
{
class Program
{
static void Main(string[] args)
{
int a, b;
int choice = 0;
float result = 0;
string type = "";

Console.WriteLine("enter 1st number= ");


a = int.Parse(Console.ReadLine());
Console.WriteLine("enter 2nd number= ");
b = int.Parse(Console.ReadLine());

Console.WriteLine("1. Addition ");


Console.WriteLine("2. Subtraction ");
Console.WriteLine("3. Multiplication ");
Console.WriteLine("4. Division ");
Console.WriteLine("5. Average ");
Console.WriteLine("6. Percentage ");
Console.WriteLine("7. Exit ");
Console.WriteLine("8. Select your choice (1-7): ");
choice = Convert.ToInt32(Console.ReadLine());

switch (choice)
{
case 1:
result = a + b;
type = "+";
break;
case 2:
result = a - b;
type = "-";
break;
case 3:
result = a * b;
type = "*";
break;
Practical No. 1 Khan Sahibe alam Div.: B

case 4:
result = a / b;
type = "%";
break;
case 5:
result = (a + b) / 2;
type = "/";
break;
case 6:
result = (a / b) * 100;
type = "%";
break;
case 7:
result = 0;
break;
default:
Console.WriteLine("invalid input");
break;
}
Console.WriteLine("\n"+"Result: "+ a.ToString() + " " + type + " " + b.ToString() +
" = " + result.ToString());
Console.ReadKey();
}
}
}

Output:
Practical No. 1 Khan Sahibe alam Div.: B

Aim: Windows based Calculator.

Program Code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Calculator
{
public partial class Form1 : Form
{
Double resultValue = 0;
String operationPerformed = "";
bool isOperationPerformed = false;

public Form1()
{
InitializeComponent();
}

private void button_click(object sender, EventArgs e)


{
if ((textBox_Result.Text == "0") || (isOperationPerformed))
textBox_Result.Clear();

isOperationPerformed = false;
Button button = (Button)sender;

if (button.Text == ".")
{
if(!textBox_Result.Text.Contains("."))
textBox_Result.Text = textBox_Result.Text + button.Text;

}else
textBox_Result.Text = textBox_Result.Text + button.Text;
}
Practical No. 1 Khan Sahibe alam Div.: B

private void operator_click(object sender, EventArgs e)


{
Button button = (Button)sender;

if (resultValue != 0)
{
button15.PerformClick();
operationPerformed = button.Text;
labelCurrentOperation.Text = resultValue + " " + operationPerformed;
isOperationPerformed = true;
}
else
{

operationPerformed = button.Text;
resultValue = Double.Parse(textBox_Result.Text);
labelCurrentOperation.Text = resultValue + " " + operationPerformed;
isOperationPerformed = true;
}
}

private void button4_Click(object sender, EventArgs e)


{
textBox_Result.Text = "0";
}

private void button5_Click(object sender, EventArgs e)


{
textBox_Result.Text = "0";
resultValue = 0;
}

private void button15_Click(object sender, EventArgs e)


{
switch (operationPerformed)
{
case "+":
textBox_Result.Text = (resultValue +
Double.Parse(textBox_Result.Text)).ToString();
break;
Practical No. 1 Khan Sahibe alam Div.: B

case "-":
textBox_Result.Text = (resultValue -
Double.Parse(textBox_Result.Text)).ToString();
Break;
case "*":
textBox_Result.Text = (resultValue *
Double.Parse(textBox_Result.Text)).ToString();
break;
case "/":
textBox_Result.Text = (resultValue / Double.Parse(textBox_Result.Text)).ToString();
break;
default:
break;
}
resultValue = Double.Parse(textBox_Result.Text);
labelCurrentOperation.Text = "";
}
}
}

Output:

You might also like