PROGRAM 1
Q1. program to display the addition, subtraction,
multiplication and division of two numbers using console
application.
using System;
namespace ArithmeticOperations
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Basic Arithmetic Operations");
// Input from user
Console.Write("Enter first number: ");
double num1 = Convert.ToDouble(Console.ReadLine());
Console.Write("Enter second number: ");
double num2 = Convert.ToDouble(Console.ReadLine());
// Calculations
double addition = num1 + num2;
double subtraction = num1 - num2;
double multiplication = num1 * num2;
string division = num2 != 0 ? (num1 / num2).ToString() : "Undefined
(division by zero)";
// Output
Console.WriteLine($"\nAddition: {num1} + {num2} = {addition}");
Console.WriteLine($"Subtraction: {num1} - {num2} = {subtraction}");
Console.WriteLine($"Multiplication: {num1} * {num2} = {multiplication}");
Console.WriteLine($"Division: {num1} / {num2} = {division}");
Console.WriteLine("\nPress any key to exit...");
Console.ReadKey();
}
}
}
OUTPUT
Basic Arithmetic Operations
Enter first number: 12
Enter second number: 12
Addition: 12 + 12 = 24
Subtraction: 12 - 12 = 0
Multiplication: 12 * 12 = 144
Division: 12 / 12 = 1
Press any key to exit...
PROGRAM 2
Q2. Program to display the first 10 natural numbers and their
sum using console application.
using System;
class Program
{
static void Main()
{
int sum = 0;
Console.WriteLine("The first 10 natural numbers are:");
for (int i = 1; i <= 10; i++)
{
Console.Write(i + " ");
sum += i;
}
Console.WriteLine("\n\nThe sum of the first 10 natural numbers is: " + sum);
Console.ReadLine(); // To keep the console window open
}
}
OUTPUT
The first 10 natural numbers are:
1 2 3 4 5 6 7 8 9 10
The sum of the first 10 natural numbers is: 55
PROGRAM 3
Q3. Program to display the addition using the windows
application.
private void button1_Click(object sender, EventArgs e)
{
try
{
// Read values from the text boxes
int num1 = int.Parse(textBox1.Text);
int num2 = int.Parse(textBox2.Text);
// Perform addition
int sum = num1 + num2;
// Display result
labelResult.Text = "Result: " + sum.ToString();
}
catch (FormatException)
{
MessageBox.Show("Please enter valid integers in both text boxes.", "Input
Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
OUTPUT
TextBox1 TextBox2
[10] [15]
[ Add ] <-- button
Result: 25 <-- label
PROGRAM 4
Q4. Write a program to convert input string from lower to
upper and upper to lower case.
using System;
class Program
{
static void Main()
{
Console.Write("Enter a string: ");
string input = Console.ReadLine();
string converted = "";
foreach (char c in input)
{
if (char.IsUpper(c))
converted += char.ToLower(c);
else if (char.IsLower(c))
converted += char.ToUpper(c);
else
converted += c; // Keep other characters unchanged
}
Console.WriteLine("Converted string: " + converted);
}
}
OUTPUT
Enter a string: Hello World 123!
Converted string: hELLO wORLD 123!