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

C# Console Programming Examples

The document contains multiple C# and VB.NET code examples demonstrating various programming concepts such as conditional statements, control statements, Windows controls, multi-threading, subroutines, and database connectivity. It also includes examples for prime number checking, factorial calculation, palindrome checking, and summing digits. Each section provides a practical implementation of the concepts in console or Windows applications.

Uploaded by

Chaithra
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)
27 views28 pages

C# Console Programming Examples

The document contains multiple C# and VB.NET code examples demonstrating various programming concepts such as conditional statements, control statements, Windows controls, multi-threading, subroutines, and database connectivity. It also includes examples for prime number checking, factorial calculation, palindrome checking, and summing digits. Each section provides a practical implementation of the concepts in console or Windows applications.

Uploaded by

Chaithra
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

Part A:

1. Conditional stmt(C# -CONSOLE)

using System;
using [Link];
using [Link];
using [Link];
using [Link];
namespace ConsoleApp3
{
class Program1
{
static void Main(string[] args)
{
int x;
int a, b;
[Link]("Enter any two nymbers");
a = Convert.ToInt32([Link]());
b = Convert.ToInt32([Link]());
[Link]("Enter your choice");
[Link]("1. Simple if statement");
[Link]("2. if else statement");
[Link]("3. else if statement");
[Link]("4. Exit");
x = Convert.ToInt32([Link]());
switch (x)
{
case 1:
if (a < b)
{
[Link]("a is smallest");
}
break;
case 2:
if (a > b)
[Link]("a is largest");
else
[Link]("b is largest");
break;
case 3:
if (a == b)
[Link]("a is equal to b");
else if (a > b)
[Link]("a greater than b");
else
[Link]("b greater than a");
break;
case 4:
[Link]("Exit");
break;
default:
[Link]("invalid choice");
break;
}
[Link]();
}
}
}

2. control stmts (C# -CONSOLE)


using System;
using [Link];
using [Link];
using [Link];
using [Link];
namespace loopingstatement
{
class Program2
{
static void Main(string[] args)
{
int x;
int i;
int a = 1;
[Link]("Enter your choice");
[Link]("1. For loop");
[Link]("2. While loop");
[Link]("3. Do loop");
[Link]("4. For each loop");
x = Convert.ToInt32([Link]());
switch (x)
{
case 1:

[Link]("First 10 Natural Numbers are: ");


for (i = 1; i <= 10; i++)
[Link](i);
break;
case 2:
while (a <= 10)
{
[Link](a);
a++;
}
break;
case 3:
do
{
[Link](a);
a++;
}
while (a <= 10);
break;
case 4:
char[] myArray = { 'H', 'e', 'l', 'l', 'o' };
foreach (char ch in myArray)
{
[Link](ch);
}
break;
default:
[Link]("invalid choice");
break;
}
[Link]();
}
}
}

3. Windows control (C# -WINDOWS)

using System;
using [Link];
using [Link];
using [Link];
using [Link];
using [Link];
using [Link];
using [Link];
using [Link];
namespace WindowsFormsApplication2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
string gender, qualification;
if ([Link])
gender = "Male";
else
if ([Link])
gender = "FeMale";

else if ([Link])
gender = "Other";
else gender = "Gender not selected.";
if ([Link])
qualification = "Post Graduate";
else if ([Link])
qualification = "Graduate";
else if ([Link])
qualification = "PUC";
else qualification = "Not Checked";
[Link]("Name : " + [Link] + "\nAddress : " +
[Link] + "\nDOB :" + dateTimePicker1 + "\nGender : " +
gender + "\nState : " + [Link] + "\nQualification : " +
qualification);
}

private void button2_Click_1(object sender, EventArgs e)


{
Close();
}
}
}

4. Multi threading (C# -CONSOLE)

using System;
using [Link];
using [Link];
using [Link];
using [Link];
using [Link];
namespace ConsoleApp4
{
class Program4
{
public static void method1()
{
// It prints numbers from 0 to 10
for (int I = 0; I <= 10; I++)
{
[Link]("Method1 is : {0}", I);
// When the value of I is equal to 5 then
// this method sleeps for 6 seconds
if (I == 5)
{
[Link](6000);
}
}
}

// static method two


public static void method2()
{
// It prints numbers from 0 to 10
for (int J = 0; J <= 10; J++)
{
[Link]("Method2 is : {0}", J);
}
}
static void Main(string[] args)
{
// Creating and initializing threads
Thread thr1 = new Thread(method1);
Thread thr2 = new Thread(method2);
[Link]();
[Link]();
}
}
}

5. Subroutines and function(C# -CONSOLE)

using System;
using [Link];
using [Link];
using [Link];
using [Link];

namespace funsubdemo
{
class Program
{

//this is a subroutine
void AddValue(int a, int b)
{
[Link]("Value of a is: " + a);
[Link]("Value of b is: " + b);

[Link]("Sum: {0}", a + b);

//this is a method
int AddValues(int x, int y)
{
return x + y;
}

static void Main(string[] args)


{
int a = 100, b = 200;
Program obj1 = new Program();
[Link](a, b);

[Link]("The sum of a and b is " +


[Link](a, b));

[Link]();
}
}
}
6. Application- BUILT IN FUN([Link])

Public Class Form1

Private Sub Button1_Click(ByVal sender As Object, ByVal e As


EventArgs) Handles [Link]
Dim s As String
s = "welcome visual studio world"
Dim i As Integer
i = Len(s)
MsgBox(i)
End Sub
Private Sub Button2_Click(ByVal sender As Object, ByVal e As
EventArgs) Handles [Link]
Dim i As Integer
i = Asc("B")
MsgBox(i)
End Sub
Private Sub Button3_Click(ByVal sender As Object, ByVal e As
EventArgs) Handles [Link]
Dim i As String
i = Chr(66)
MsgBox(i)
End Sub
Private Sub Button4_Click(ByVal sender As Object, ByVal e As
EventArgs) Handles [Link]
Dim i As Boolean
Dim s As String
s = "welcome visual studio world"
i = IsNumeric(s)
MsgBox(i)
End Sub
Private Sub Button5_Click(ByVal sender As Object, ByVal e As
EventArgs) Handles [Link]
Dim i As Boolean
Dim s As String
s = "30.56"
i = IsNumeric(s)
MsgBox(i)
Dim decprice As Decimal
decprice = CDec(s)
MsgBox(decprice)
End Sub
End Class
7. MDI- Employee payroll([Link])

MDI coding
Public Class Form2

Private Sub EmployeeDetailsToolStripMenuItem_Click(ByVal


sender As [Link], ByVal e As [Link]) Handles
[Link]
[Link]()
End Sub
Private Sub ExitToolStripMenuItem_Click(ByVal sender As
[Link], ByVal e As [Link]) Handles
[Link]
End

End Sub
End Class
Child Class Coding
Public Class Form1

Private Sub Button1_Click(ByVal sender As [Link], ByVal


e As [Link]) Handles [Link]
Dim netpay As Double
Dim gross As Double
Dim week, hour, sun, sat, sathour, sunhour As Double
Dim dec, tax As Double
week = [Link]([Link]) * 125
sun = [Link]([Link]) * 80
sat = [Link]([Link]) * 160
hour = [Link]([Link]) * 180
sathour = [Link]([Link]) * 30
sunhour = [Link]([Link]) * 30
gross = (week + hour + sun + sat + sathour + sunhour)
dec = gross * (10 / 100)
tax = gross * (15 / 100)
netpay = gross - tax - dec
[Link] = [Link](gross)
[Link] = [Link](dec)
[Link] = [Link](tax)
[Link] = [Link](netpay)
End Sub

Private Sub Button2_Click(ByVal sender As [Link], ByVal


e As [Link]) Handles [Link]
End
End Sub
End Class

8. Console to demonstrate OOP Concept (C# -CONSOLE)

using System;
using [Link];
using [Link];
using [Link];

class Program
{
void print(int i, int j)
{
[Link]("Printing int: {0}", (i + j));
}
void print(string a, string b)
{
[Link]("Printing String " + a + b);
}
static void Main(string[] args)
{
Program prog = new Program();

// Call print for sum of integers


[Link](5, 6);

// Call to concatenate strings


[Link]("Hello", "World");

Hyundai hyn = new Hyundai();


String descp = [Link]();

[Link](descp);
[Link]();
}
}

abstract class Car


{
public virtual string Describe()
{
return "Description of the car";
}
}

class Hyundai : Car


{
public override string Describe()
{
return "Description of the car is now Hyundai";
}
}

[Link] Login Processing- Web application([Link])

Imports [Link]

Public Class _Default


Inherits [Link]

Protected Sub Page_Load(ByVal sender As Object, ByVal e As


[Link]) Handles [Link]

End Sub
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As
EventArgs) Handles [Link]
Dim con As New
OleDbConnection("Provider=[Link].12.0;Data
Source=C:\Users\indo\Desktop\rach-log\[Link]")
Dim cmd As OleDbCommand = New OleDbCommand("select *
from table1 where username='" + [Link] & "' and
password='" + [Link] + "'", con)
[Link]()
Dim dr As OleDbDataReader = [Link]
If [Link]() = True Then
MsgBox("login success")
Else
MsgBox("invalid")
End If

End Sub
End Class
10. Windows application- database connectivity for core banking
transaction([Link])
Public Class Form1

Private Sub Form1_Load(ByVal sender As [Link], ByVal e


As [Link]) Handles [Link]
'TODO: This line of code loads data into the
'[Link]' table. You can move, or remove it,
as needed.
[Link]([Link])

End Sub

Private Sub Button1_Click(ByVal sender As [Link], ByVal


e As [Link]) Handles [Link]
[Link]()

End Sub
Private Sub Button2_Click(ByVal sender As [Link], ByVal
e As [Link]) Handles [Link]

On Error GoTo SaveError

[Link]()
[Link]([Link])
MsgBox("Data has been saved")

SaveError:
Exit Sub

End Sub

Private Sub Button3_Click(ByVal sender As [Link], ByVal


e As [Link]) Handles [Link]
[Link]()

End Sub
End Class
Part B
1. Prime using [Link]

Public Class Form1


Private Sub Button1_Click(ByVal sender As Object, ByVal e As
EventArgs) Handles [Link]
Dim check As Integer
check = 1
Dim num As Integer
num = [Link]
For i = 2 To (num - 1)
If num Mod i = 0 Then
check = 0
Exit For
End If
Next
If check = 0 Then
[Link]("It is Not a prime")
Else
[Link]("It is a prime")
End If
End Sub
Private Sub Button2_Click(ByVal sender As Object, ByVal e As
EventArgs) Handles [Link]
End
End Sub
End Class

2. [Link] program to find factorial of a given number


Public Class Form1
Private Sub Button1_Click(ByVal sender As Object, ByVal e As
EventArgs) Handles [Link]
Dim n, f, i As Integer
f=1
n = Val([Link])
For i = 1 To n
f=f*i
Next
MsgBox("Factorial is :" & f, vbInformation + vbOKCancel,
"Factorial")
End Sub
Private Sub Button2_Click(ByVal sender As Object, ByVal e As
EventArgs) Handles [Link]
End
End Sub
End Class

3. Prime Number (C# -CONSOLE)

using System;
using [Link];
using [Link];
using [Link];

namespace ConsoleApplication22
{
public class PrimeNumber
{
public static void Main(string[] args)
{
int n, i, m = 0, flag = 0;
[Link]("Enter the Number to check Prime: ");
n = [Link]([Link]());
m = n / 2;
for (i = 2; i <= m; i++)
{
if (n % i == 0)
{
[Link]("Number is not Prime.");
flag = 1;
break;
}
}
if (flag == 0)
[Link]("Number is Prime.");
[Link]();
}
}

}
4. Palindrome(C# -CONSOLE)

using System;
using [Link];
using [Link];
using [Link];

namespace ConsoleApplication22
{
public class Palindrome
{
public static void Main(string[] args)
{
int n, r, sum = 0, temp;
[Link]("Enter the Number: ");
n = [Link]([Link]());
temp = n;
while (n > 0)
{
r = n % 10;
sum = (sum * 10) + r;
n = n / 10;
}
if (temp == sum)
[Link]("Number is Palindrome.");
else
[Link]("Number is not Palindrome");
[Link]();
}
}

5. Sum of digits program (C# -CONSOLE)

using System;
using [Link];
using [Link];
using [Link];
namespace ConsoleApplication22
{
public class Sumdigit
{
public static void Main(string[] args)
{
int n, sum = 2, m;

[Link]("Enter a number: ");


n = [Link]([Link]());
while (n > 0)
{
m = n % 10;
sum = sum + m;
n = n / 10;
}
[Link]("Sum is= " + sum);
[Link]();
}
}

You might also like