0% found this document useful (0 votes)
432 views20 pages

Practical 1: Write C# Code To Display The Asterisk Pattern

The document contains 5 code examples that demonstrate various concepts in C#: 1. The first example displays asterisk patterns in square and triangle shapes based on user input. 2. The second example prompts the user to input their name and country, then displays it back to them. 3. The third example converts a digit number to its word equivalent. 4. The fourth example contains functions to convert between decimal, binary, and hexadecimal number systems. 5. The last example converts an infix notation mathematical expression to postfix notation.

Uploaded by

Sachin Sharma
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
432 views20 pages

Practical 1: Write C# Code To Display The Asterisk Pattern

The document contains 5 code examples that demonstrate various concepts in C#: 1. The first example displays asterisk patterns in square and triangle shapes based on user input. 2. The second example prompts the user to input their name and country, then displays it back to them. 3. The third example converts a digit number to its word equivalent. 4. The fourth example contains functions to convert between decimal, binary, and hexadecimal number systems. 5. The last example converts an infix notation mathematical expression to postfix notation.

Uploaded by

Sachin Sharma
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

Practical 1

Write C# code to display the asterisk pattern.


using System;

namespace Practical1
{
class Program
{
static void Main(string[] args)
{
string opt = "Yes";
while (opt == "yes" || opt == "Yes")
{
Console.Clear();
Console.WriteLine("1.Square Pattern");
Console.WriteLine("2.Triangle Pattern");
Console.Write("Enter Your Pattern:");
string input = Console.ReadLine();
Console.Write("Enter Your Pattern Size:");
int n = Convert.ToInt32(Console.ReadLine());
if (input == "Square" || input == "square")
{
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
Console.Write("*");
}
Console.Write("\n");
}
}
else if (input == "Triangle" || input == "triangle")
{
for (int i = 0; i < n; i++)
{
for (int j = 0; j <= i; j++)
{
Console.Write("*");
}
Console.Write("\n");
}
}
else
Console.WriteLine("Enter Correct Option!!");
Console.Write("Do You Wanna Do It Again (Yes/No):");
opt = Console.ReadLine();
}
}

}
}


 
Output :


 
Practical 2
Write C# code to prompt a user to input his/her name and
country name.
using System;
namespace Practical2
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello");
string name, country;
Console.Write("Enter your name");
name = Console.ReadLine();
Console.Write("Enter your country");
country = Console.ReadLine();
Console.Write("{0} is from {1}", name , country);
Console.ReadKey();
}
}
}
Output :


 
Practical 3
Write C# Program to Convert Digits to Words.
using System;

namespace Practical3
{
class Program
{
static void Main(string[] args)
{
Console.Write("Enter your no. :");
String input = Console.ReadLine();
string[] dict = { "Zero", "One", "Two", "Three", "Four", "Five", .
"Six","Seven","Eight", "Nine" };
string result = "";
for (int i=0;i<input.Length;i++)
{
int c = Convert.ToInt32(input[i])-48;
result += dict[c];
result += " ";
}
Console.WriteLine("{0} in Words Is {1}",input,result);
}
}
}

Output:


 
Practical 4
Write C# code to convert Following:-
1. To Convert Decimal To Binary No

2. To Convert Binary To Decimal No.

3. To Convert Decimal To HexaDecimal No.

4. To Convert HexaDecimal To Binary No.

using System;

namespace pratical4
{
class Program
{
static string DecToBinary(int a)
{
string k = "";
while (a >= 1)
{
int r = a % 2;
a = a / 2;
k += r;

}
string l = "";
for (int i = k.Length - 1; i >= 0; i--)
{
l += k[i];
}
return l;
}
static int BinToDecimal(int a)
{
int sum = 0,i=0;
while (a >= 1)
{
int r = a % 10;
sum += r * Convert.ToInt32(Math.Pow(2, i));
a = a / 10;
i++;

}
return sum;

}
static String DecToHexaDecimal(int a)
{
string k = "";
while (a >= 1)
{
int r = a % 16;
if (r == 10)
{
k += 'A';
a /= 16;
continue;


 
}
if (r == 11)
{
k += 'B';
a /= 16;
continue;

}
if (r == 12)
{
k += 'C';
a /= 16;
continue;

if (r == 13)
{
k += 'D';
a /= 16;
continue;

}
if (r == 14)
{
k += 'E';
a /= 16;
continue;

}
if (r == 15)
{
k += 'F';
a /= 16;
continue;

}
a = a / 16;
k += r;

}
string l = "";
for (int i = k.Length - 1; i >= 0; i--)
{
l += k[i];
}0
return l;
}
static int HexToDecimal(String a)
{
int sum = 0, j = 0;
for(int i=a.Length-1;i>=0;i-- )
{ if(a[i]=='A'||a[i]=='a')
{
sum += 10 * Convert.ToInt32(Math.Pow(16, j));
}
else if (a[i] == 'B' || a[i] == 'b')
{
sum += 11 * Convert.ToInt32(Math.Pow(16, j));
}
else if (a[i] == 'C' || a[i] == 'c')


 
{
sum += 12 * Convert.ToInt32(Math.Pow(16, j));
}
else if (a[i] == 'D' || a[i] == 'd')
{
sum += 13 * Convert.ToInt32(Math.Pow(16, j));
}
else if (a[i] == 'E' || a[i] == 'e')
{
sum += 14 * Convert.ToInt32(Math.Pow(16, j));
}
else if (a[i] == 'F' || a[i] == 'f')
{
sum += 15 * Convert.ToInt32(Math.Pow(16, j));
}
else
{
int k = Convert.ToInt32(a[i]);
sum +=(k-48)* Convert.ToInt32(Math.Pow(16, j));
}
j++;

}
return sum;
}
static void Main(string[] args)
{
String ans = "Yes";
while (ans == "Yes" || ans == "yes")
{
Console.Clear();
Console.WriteLine("1.To Convert Decimal To Binary No.");
Console.WriteLine("2.To Convert Binary To Decimal No.");
Console.WriteLine("3.To Convert Decimal To HexaDecimal No.");
Console.WriteLine("4.To Convert HexaDecimal To Binary No.");
Console.WriteLine("5.Exit");
Console.Write("Enter your Option(1-5):");
int opt = Convert.ToInt32(Console.ReadLine());

switch (opt)
{
case 1:
Console.Write("Enter Your No. in Decimal:");
int num = Convert.ToInt32(Console.ReadLine());
string res = DecToBinary(num);
Console.WriteLine("{0} in Binary Is {1}", num, res);
break;
case 2:
Console.Write("Enter Your No. in Binary:");
int num1 = Convert.ToInt32(Console.ReadLine());
int res1 = BinToDecimal(num1);
Console.WriteLine("{0} in Decimal Is {1}", num1, res1);
break;
case 3:
Console.Write("Enter Your No. in Decimal:");
int num2 = Convert.ToInt32(Console.ReadLine());
String res2 = DecToHexaDecimal(num2);
Console.WriteLine("{0} in HexaDecimal Is {1}", num2, res2);
break;
case 4:
Console.Write("Enter Your No. in HexaDecimal:");
String s = Console.ReadLine();
int res3 = HexToDecimal(s);


 
Console.WriteLine("{0} in Decimal Is {1}", s, res3);
break;
case 5:
System.Environment.Exit(0);
break;
default:
Console.Write("Enter Correct Option (1-5):");
break;
}
Console.WriteLine("Do You Wanna Calculate again?");
ans = Console.ReadLine();
}
}
}
}

Output :


 

 
Practical 5
Write C# code to Convert Infix to Postfix.
using System;

namespace Practical5
{
class Program
{
static int preference(char a)
{ if (a == '^')
return 3;
else if (a == '/' || a == '*')
return 2;
else if (a == '+' || a == '-')
return 1;
else
return 0;

}
static void Main(string[] args)
{
Console.Write("Enter Your Infix Statement :");
string Infix =Console.ReadLine();
Infix += ')';
string Postfix = "";
char[] Stack = new char[Infix.Length];
int top = 0;
Stack[top] = '(';
for(int i=0;i<Infix.Length;i++)
{ if (Infix[i]=='(')
{ top++;
Stack[top] = Infix[i];

}
else if(Infix[i]== '^'||Infix[i]=='/' ||Infix[i]=='*'||Infix[i]=='+'||Infix[i]=='-' )
{
int a = preference(Infix[i]);
int b = preference(Stack[top]);
top++;
Stack[top] = Infix[i];
if (a<=b)
{ while(preference(Stack[top])<=preference(Stack[top-1]))
{
Postfix += Stack[top - 1];
Stack[top - 1] = Stack[top];
top--;
}
}
}
else if (Infix[i]==')')
{
while(true)
{
Postfix += Stack[top];
top--;
if(Stack[top]=='(')
{
top--;
break;

10 
 
}
}
}
else
{
Postfix += Infix[i];
}
}
Console.WriteLine("Your Postfix Statement:{0}",Postfix);
}
}
}

Output :

11 
 
Practical 6
Write C# code to Perform Currency Conversions.
1. To Convert Dollar To Rupee.

2. To Convert Euro To Rupee.

3. To Convert Franc To Rupee.

using System;

namespace Practical6
{
class Program
{
static Double DollarToRupee(Double a)
{
Double b = a * 71.84;
return b;

}
static Double EuroToRupee(Double a)
{
Double b = a * 79.08;
return b;
}
static Double FrancToRupee(Double a)
{
Double b = a * 72.42;
return b;
}
static void Main(string[] args)
{
String ans = "Yes";
while (ans == "Yes" || ans == "yes")
{
Console.Clear();
Console.WriteLine("1.To Convert Dollar To Rupee");
Console.WriteLine("2.To Convert Euro To Rupee");
Console.WriteLine("3.To Convert Franc To Rupee");
Console.WriteLine("4.Exit");
Console.Write("Enter your Option(1-4):");
int opt = Convert.ToInt32(Console.ReadLine());

switch (opt)
{
case 1:
Console.Write("Enter Your Amount in Dollar:");
Double num = Convert.ToDouble(Console.ReadLine());
double res = DollarToRupee(num);
Console.WriteLine("${0} in Rupee Is {1}INR", num, res);
break;
case 2:
Console.Write("Enter Your Amount in Euro:");
Double num1 = Convert.ToDouble(Console.ReadLine());
Double res1 = EuroToRupee(num1);
Console.WriteLine("£{0} in Rupee Is {1}INR", num1, res1);

12 
 
break;
case 3:
Console.Write("Enter Your Amount in Franc:");
Double num2 = Convert.ToDouble(Console.ReadLine());
Double res2 = FrancToRupee(num2);
Console.WriteLine("Fr.{0} in Rupee Is {1}INR", num2, res2);
break;
case 4:
System.Environment.Exit(0);
break;
default:
Console.Write("Enter Correct Option (1-4):");
break;
}
Console.WriteLine("Do You Wanna Calculate again?");
ans = Console.ReadLine();
}
}
}
}

Output :

13 
 
Practical 7
Write C# Program to perform Temperature Conversion.
1. To Convert Celsius To Fahrenheit.

2. To Convert Fahrenheit To Celsius.

using System;

namespace Practical3
{
class Program
{

static float CelToFahr(float a)


{
float k;
k =((a * 9) / 5) + 32;
return k;
}
static float FahrToCel(float a)
{
float k;
k = ((a - 32) * 5) / 9;
return k;
}
static void Main(string[] args)
{
String ans = "Yes";
while (ans == "Yes" || ans == "yes")
{
Console.Clear();
Console.WriteLine("1.To Convert Celsius To Fahrenheit");
Console.WriteLine("2.To Convert Fahrenheit To Celsius");
Console.WriteLine("3.Exit");
Console.Write("Enter your Option(1-3):");
int opt = Convert.ToInt32(Console.ReadLine());

switch (opt)
{
case 1:
Console.Write("Enter Temperature in Celsius:");
float num = Convert.ToInt32(Console.ReadLine());
float res = CelToFahr(num);
Console.WriteLine("{0} in Fahrenheit Is {1}°F", num, res);
break;
case 2:
Console.Write("Enter Temperature in Fahrenheit:");
float num1 = Convert.ToInt32(Console.ReadLine());
float res1 = FahrToCel(num1);
Console.WriteLine("{0} in Celsius Is {1}°C", num1, res1);
break;
case 3:
System.Environment.Exit(0);
break;
default:
Console.Write("Enter Correct Option (1-3):");

14 
 
break;
}
Console.WriteLine("Do You Wanna Calculate again?");
ans = Console.ReadLine();
}
}
}
}

Output :

15 
 
Practical 8
Write C# Program to Create a Windows Form.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)


{

}
private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show("Successfully Updated!");
}
private void label2_Click(object sender, EventArgs e)
{

private void textBox3_TextChanged(object sender, EventArgs e)


{

private void label1_Click(object sender, EventArgs e)


{

}
private void textBox1_TextChanged(object sender, EventArgs e)
{

private void textBox2_TextChanged(object sender, EventArgs e)


{

}
private void label3_Click(object sender, EventArgs e)
{

}
}
}

16 
 
Output :

17 
 
Practical 9
Write ASP.NET Program to increase or decrease font size
programmatically.
using System;
using System.Web.UI.WebControls;

namespace WebApplication2
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
Label1.Font.Size = FontUnit.XLarge;
}
}
}

Output :

18 
 
Practical 10
Write ASP.NET Program to change colour of label text 
control programmatically. 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebApplication3
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
Label1.ForeColor = System.Drawing.Color.IndianRed;
}
}
}

Output :

19 
 
Practical 11
Write ASP.NET Program to enable‐disable textbox and 
change width of textbox programmatically
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebApplication5
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
TextBox1.Enabled = true;
}
protected void Button2_Click(object sender, EventArgs e)
{
TextBox1.Enabled = false;
}
protected void Button3_Click(object sender, EventArgs e)
{
TextBox1.Width = 500;
}
}
}

Output :

20 
 

You might also like