0% found this document useful (0 votes)
3 views2 pages

CSharp Programming Experiments

The document contains four C# programming experiments. Experiment 1 checks if a year is a leap year, Experiment 2 calculates acceleration based on user input, Experiment 3 generates five random numbers, and Experiment 4 demonstrates the use of access specifiers in a class. Each experiment includes example inputs and expected outputs.
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)
3 views2 pages

CSharp Programming Experiments

The document contains four C# programming experiments. Experiment 1 checks if a year is a leap year, Experiment 2 calculates acceleration based on user input, Experiment 3 generates five random numbers, and Experiment 4 demonstrates the use of access specifiers in a class. Each experiment includes example inputs and expected outputs.
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
You are on page 1/ 2

C# Programming Experiments

Experiment 1: Check Whether the Entered Year is a Leap Year or Not


using System;
class LeapYearCheck {
static void Main() {
Console.Write("Enter a Year: ");
int year = int.Parse(Console.ReadLine());
if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
Console.WriteLine($"{year} is a Leap Year.");
else
Console.WriteLine($"{year} is not a Leap Year.");
}
// Input: 2024
// Output: 2024 is a Leap Year.
}

Experiment 2: Calculate Acceleration


using System;
class Acceleration {
static void Main() {
Console.Write("Enter Initial Velocity (u): ");
double u = double.Parse(Console.ReadLine());
Console.Write("Enter Final Velocity (v): ");
double v = double.Parse(Console.ReadLine());
Console.Write("Enter Time (t): ");
double t = double.Parse(Console.ReadLine());
double a = (v - u) / t;
Console.WriteLine($"Acceleration: {a} m/s²");
}
// Input: u = 0, v = 20, t = 4
// Output: Acceleration: 5 m/s²
}

Experiment 3: Generate Random Numbers


using System;
class RandomNumbers {
static void Main() {
Random rnd = new Random();
Console.WriteLine("5 Random Numbers:");
for (int i = 0; i < 5; i++)
Console.WriteLine(rnd.Next(1, 101));
}
// Output: Random numbers between 1 and 100
}
Experiment 4: Use of Access Specifiers
using System;
class AccessSpecifiers {
private int privateVar = 1;
public int publicVar = 2;
protected int protectedVar = 3;
internal int internalVar = 4;
void Display() {
Console.WriteLine($"Private: {privateVar}, Public: {publicVar},
Protected: {protectedVar}, Internal: {internalVar}");
}
static void Main() {
AccessSpecifiers obj = new AccessSpecifiers();
obj.Display();
}
// Output: All variables displayed since they are accessed within
the same class.
}

You might also like