20 C# Programs on Variables
This document includes 20 beginner-to-intermediate level programs in C# focusing on variables,
input/output, constants, arithmetic, and real-world applications.
1. Basic Variable Declaration and Output
using System; class Program { static void Main() { int age = 20; string
name = "Ariyan"; double height = 5.9; [Link]("Name: " + name);
[Link]("Age: " + age); [Link]("Height: " + height);
} }
2. Arithmetic Operations Using Variables
using System; class Program { static void Main() { int num1 = 10; int
num2 = 5; int sum = num1 + num2; int diff = num1 - num2; int product =
num1 * num2; double quotient = (double)num1 / num2;
[Link]("Sum: " + sum); [Link]("Difference: " +
diff); [Link]("Product: " + product);
[Link]("Quotient: " + quotient); } }
3. User Input Example
using System; class Program { static void Main() { [Link]("Enter
your name: "); string name = [Link](); [Link]("Enter
your age: "); int age = Convert.ToInt32([Link]());
[Link]($"Hello {name}, you are {age} years old!"); } }
4. Swapping Two Variables Using Temp Variable
using System; class Program { static void Main() { int a = 10, b = 20,
temp; [Link]($"Before Swap: a = {a}, b = {b}"); temp = a; a =
b; b = temp; [Link]($"After Swap: a = {a}, b = {b}"); } }
5. Swapping Without Temp Variable
using System; class Program { static void Main() { int x = 15, y = 25;
[Link]($"Before Swap: x = {x}, y = {y}"); x = x + y; y = x - y;
x = x - y; [Link]($"After Swap: x = {x}, y = {y}"); } }
6. Area of a Circle
using System; class Program { static void Main() { const double pi =
3.14159; [Link]("Enter radius: "); double radius =
[Link]([Link]()); double area = pi * radius * radius;
[Link]($"Area of Circle: {area}"); } }
7. Simple Interest Calculator
using System; class Program { static void Main() { [Link]("Enter
Principal: "); double principal = [Link]([Link]());
[Link]("Enter Rate: "); double rate =
[Link]([Link]()); [Link]("Enter Time (years):
"); double time = [Link]([Link]()); double interest =
(principal * rate * time) / 100; [Link]($"Simple Interest =
{interest}"); } }
8. Average of Three Numbers
using System; class Program { static void Main() { [Link]("Enter
three numbers: "); double a = [Link]([Link]());
double b = [Link]([Link]()); double c =
[Link]([Link]()); double avg = (a + b + c) / 3;
[Link]($"Average = {avg}"); } }
9. Temperature Conversion (Celsius to Fahrenheit)
using System; class Program { static void Main() { [Link]("Enter
temperature in Celsius: "); double celsius =
[Link]([Link]()); double fahrenheit = (celsius * 9 /
5) + 32; [Link]($"Temperature in Fahrenheit: {fahrenheit}"); }
}
10. Area of Rectangle
using System; class Program { static void Main() { [Link]("Enter
length: "); double length = [Link]([Link]());
[Link]("Enter width: "); double width =
[Link]([Link]()); double area = length * width;
[Link]($"Area of Rectangle = {area}"); } }
11. Perimeter of a Rectangle
using System; class Program { static void Main() { double length = 10,
width = 5; double perimeter = 2 * (length + width);
[Link]($"Perimeter = {perimeter}"); } }
12. Area of Triangle
using System; class Program { static void Main() { [Link]("Enter
base: "); double b = [Link]([Link]());
[Link]("Enter height: "); double h =
[Link]([Link]()); double area = 0.5 * b * h;
[Link]($"Area of Triangle = {area}"); } }
13. Convert Days into Years, Weeks, and Days
using System; class Program { static void Main() { [Link]("Enter
days: "); int days = Convert.ToInt32([Link]()); int years =
days / 365; int weeks = (days % 365) / 7; int remainingDays = (days % 365)
% 7; [Link]($"{years} Years, {weeks} Weeks, {remainingDays}
Days"); } }
14. Currency Conversion (USD to PKR)
using System; class Program { static void Main() { const double rate =
278.5; [Link]("Enter amount in USD: "); double usd =
[Link]([Link]()); double pkr = usd * rate;
[Link]($"{usd} USD = {pkr} PKR"); } }
15. Profit and Loss Calculator
using System; class Program { static void Main() { [Link]("Enter
Cost Price: "); double cp = [Link]([Link]());
[Link]("Enter Selling Price: "); double sp =
[Link]([Link]()); double profit = sp - cp; if (profit
> 0) [Link]($"Profit = {profit}"); else
[Link]($"Loss = {-profit}"); } }
16. Find Square and Cube of a Number
using System; class Program { static void Main() { [Link]("Enter a
number: "); int n = Convert.ToInt32([Link]());
[Link]($"Square = {n * n}, Cube = {n * n * n}"); } }
17. Convert Hours into Minutes and Seconds
using System; class Program { static void Main() { [Link]("Enter
hours: "); int hours = Convert.ToInt32([Link]()); int minutes =
hours * 60; int seconds = minutes * 60; [Link]($"{hours} hours
= {minutes} minutes = {seconds} seconds"); } }
18. Volume of Cube
using System; class Program { static void Main() { [Link]("Enter
side of cube: "); double side = [Link]([Link]());
double volume = [Link](side, 3); [Link]($"Volume of Cube =
{volume}"); } }
19. Calculate Total and Percentage of Marks
using System; class Program { static void Main() { [Link]("Enter
marks of 5 subjects: "); double m1 =
[Link]([Link]()); double m2 =
[Link]([Link]()); double m3 =
[Link]([Link]()); double m4 =
[Link]([Link]()); double m5 =
[Link]([Link]()); double total = m1 + m2 + m3 + m4 +
m5; double percentage = total / 5; [Link]($"Total = {total},
Percentage = {percentage}%"); } }
20. Simple Calculator
using System; class Program { static void Main() { [Link]("Enter
first number: "); double a = [Link]([Link]());
[Link]("Enter operator (+, -, *, /): "); char op =
[Link]([Link]()); [Link]("Enter second number:
"); double b = [Link]([Link]()); double result = 0;
switch (op) { case '+': result = a + b; break; case '-': result = a - b;
break; case '*': result = a * b; break; case '/': result = a / b; break;
default: [Link]("Invalid operator"); return; }
[Link]($"Result = {result}"); } }