0% found this document useful (0 votes)
18 views

C# File

C# Program

Uploaded by

Rohit Tiwari
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

C# File

C# Program

Uploaded by

Rohit Tiwari
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 25

Plot No. 2, Sector 17-A, Yamuna Expressway, Greater Noida, Gautam Buddh Nagar, U.P.

, India

School of Computer Science and Engineering

Lab Manual

Unity with C#
R1UC506C (PR)

B.Tech. (5th Sem)


2024 – 2025

Submitted By: Submitted to:


Name: Bharat Kumar Dr. Bharat Bhushan Naib

Admission No.: 22SCSE1011020


Section: 01
Program No. 1
Objective – Write a program to calculate provident fund of employer
Source Code –
using System;
class SampleOne{
static void Main(String[] args){
const double employeePFRate = 0.12;
const double employerPFRate = 0.12;

Console.Write("Enter the basic salary of employee: ");


double basicSalary = Convert.ToDouble(Console.ReadLine());

double employeePF = basicSalary * employeePFRate;


double employerPF = basicSalary * employerPFRate;

Console.WriteLine($"Employee Provident Fund: {employeePF}");


Console.WriteLine($"Employer Provident Fund: {employerPF}");
}
}

Output –
Program No. 2
Objective – Write a C# Program to Find the Sum of Series 1+x+x^2+..+x^n
Source Code –
using System;
class SampleTwo{
public static void Main(string[] args)
{
Console.Write("Enter the value of x: ");
int x = Convert.ToInt32(Console.ReadLine());
Console.Write("Enter the power of x: ");
int n = Convert.ToInt32(Console.ReadLine());
Console.WriteLine($"Sum of series is: {CalcSum(x,n)}");
}
static double CalcSum(int x, int n){
double sum = 0;
for(int i=0; i<=n; i++){
sum+=Math.Pow(x,i);
}
Math.Round(sum,2);
return sum;
}
}

Output –
Program No. 3
Objective – Write a program to calculate simple interest and compound
interest.
Sample Code –
using System;
using System.Security.Principal;
class SampleThree{

static void Main(string[] args)


{
Console.Write("Enter Principle amount: ");
int p = Convert.ToInt32(Console.ReadLine());
Console.Write("Enter rate of interest: ");
int r = Convert.ToInt32(Console.ReadLine());
Console.Write("Enter time: ");
int t = Convert.ToInt32(Console.ReadLine());
Console.Write("Enter number of time interest paid in a year: ");
int n = Convert.ToInt32(Console.ReadLine());
int si = SimpleInterest(p,r,t);
double ci = CompoundInterest(p,r,t,n);
Console.WriteLine($"Amount after simple interest is: {si+p}");
Console.WriteLine($"Amount after compound interest is: {ci}");

}
static int SimpleInterest(int p, int r, int t){
int si = (p*r*t)/100;
return si;
}
static double CompoundInterest(int p, int r, int t, int n){
double rate = (double)r/100;
double ci = p*Math.Pow(1+(rate/n), n*t);
double result = Math.Round(ci, 2);
return result;
}
}

Output –
Program No. 4
Objective – Write a program to create an application to save employee
information using arrays.
Source Code –
using System;
class SampleFour{
public static void Main(string[] args)
{
Console.Write("Enter total number of employees: ");
int totalEmployee = Convert.ToInt32(Console.ReadLine());
int[] employeeID = new int[totalEmployee];
String[] employeeName = new String[totalEmployee];
String[] employeeDept = new String[totalEmployee];
SaveEmployeeData(employeeID, employeeName, employeeDept);
PrintEmployeeData(employeeID, employeeName, employeeDept);

}
static void SaveEmployeeData(int[] ID, String[] Name, String[] Dept){
for(int i=0; i<ID.Length; i++){
Console.Write($"Enter ID of employee {i}: ");
ID[i] = Convert.ToInt32(Console.ReadLine());
Console.Write($"Enter Name of employee {i}: ");
Name[i] = Console.ReadLine();
Console.Write($"Enter Department of employee {i}: ");
Dept[i] = Console.ReadLine();

}
}
static void PrintEmployeeData(int[] ID, String[] Name, String[] Dept){
Console.WriteLine("\n -----Printing Employees Data-----");
for(int i=0; i<ID.Length; i++){
Console.Write($"Employee {i}: ");
Console.WriteLine($"ID: {ID[i]} | Name: {Name[i]} | Department:
{Dept[i]}");
}
}
}

Output –
Program No. 5
Objective – Write a program to create a simple inventory control system for a
video rental store.
Source Code –
using System;
class SampleFive{
public static void Main(string[] args){
Console.Write("Enter total number of videos: ");
int num = Convert.ToInt32(Console.ReadLine());
int[] numOfCopies = new int[num];
string[] videoName = new string[num];
saveVideoData(num, videoName, numOfCopies);
printInventoryData(num, videoName, numOfCopies);
}
public static void saveVideoData(int n,string[] VideoName, int[] numOfCopies
){
Console.WriteLine("----Recording inventory data----");
for(int i=0; i<n; i++){
int j = i+1;
Console.Write("Enter name of video " + j + ": ");
VideoName[i] = Console.ReadLine();
Console.Write("Enter number of copies of video " + j + ": ");
numOfCopies[i] = Convert.ToInt32(Console.ReadLine());
}
}
public static void printInventoryData(int n, string[] videoName, int[] numOfC
opies){
Console.WriteLine("----Printing inventory data----");
for(int i=0; i<n; i++){
int j = i+1;
Console.WriteLine("Video " + j + ": " + videoName[i] + "\tNumber of copi
es: " + numOfCopies[i]);
}
}
}
Output –
Program No. 6
Objective – Write a program to calculate the cost of constructing a pitch and
outfield in a circular cricket ground.
Source Code –
using System;
class SampleSix{

static void Main(string[] args)


{
Console.Write("Enter the radius of the cricket ground (in meters): ");
double radius = Convert.ToDouble(Console.ReadLine());

Console.Write("Enter the cost per square meter for pitch construction: ");
double pitchCostPerSqm = Convert.ToDouble(Console.ReadLine());

Console.Write("Enter the cost per square meter for outfield construction: "
);
double outfieldCostPerSqm = Convert.ToDouble(Console.ReadLine());

double pitchArea = 20 * 3;

double outfieldArea = Math.PI * Math.Pow(radius, 2) - pitchArea;

double pitchCost = pitchArea * pitchCostPerSqm;

double outfieldCost = outfieldArea * outfieldCostPerSqm;

double totalCost = pitchCost + outfieldCost;

Console.WriteLine("Total cost for constructing the pitch and outfield: {0}", t


otalCost);
}
}
Output –
Program No. 7
Objective – There is a cricket ground in the form of circle. Management would
like to construct a pitch in the ground. WAP to accept radius of the ground
length and breadth of the pitch. Calculate the cost to construct the pitch at
the rate of 25 Rs/Sqm. Also find the cost to construct the outfield at the rate
of 50 Rs/Sqm.
Source Code –
using System;

class SampleSeven{
static void Main(){
const double pitchRate = 25.0;
const double outfieldRate = 50.0;
Console.WriteLine("Enter the radius of the cricket ground (in meters): ");

double radius = Convert.ToDouble(Console.ReadLine());

Console.WriteLine("Enter the length of the pitch (in meters): ");


double pitchLength = Convert.ToDouble(Console.ReadLine());

Console.WriteLine("Enter the breadth of the pitch (in meters): ");


double pitchBreadth = Convert.ToDouble(Console.ReadLine());

double groundArea = Math.PI * radius * radius;


double pitchArea = pitchLength * pitchBreadth;
double outfieldArea = groundArea - pitchArea;

double pitchCost = pitchArea * pitchRate;


double outfieldCost = outfieldArea * outfieldRate;

Console.WriteLine($"Cost to construct the pitch would be: , {pitchCost}");


Console.WriteLine($"Cost of construct the outfield would be: , {outfieldCo
st}");
}
}
Output –
Program No. 8
Objective – WAP to accept book name, author name and MRP of book from
the user. Calculate discount amount and selling price of book. Allow the
discount amount 15% of book in case MRP>600.
Source Code –
using System;
class SampleEight{
static void Main(){
const double discountRate = 0.15;
Console.Write("Enter book name: ");
string bookName = Console.ReadLine();

Console.Write("Enter author name: ");


string authorName = Console.ReadLine();

Console.Write("Enter MRP of book: ");


double mrp = Convert.ToDouble(Console.ReadLine());

double discount = 0;
if(mrp>600){
discount = discountRate*mrp;
}

double sellingPrice = mrp - discount;

Console.WriteLine($"Book name: {bookName}");


Console.WriteLine($"Author name: {authorName}");
Console.WriteLine($"Discount amount: {discount}");
Console.WriteLine($"Selling price: {mrp-discount}");
}
}
Output –
Program No. 9
Objective – WAP to Create an application to calculate interest for FDs, RDs
based on certain conditions using inheritance.
Source Code –
class Deposit
{
public double Principal;
public double Rate;
public int Time;

public virtual double CalculateInterest()


{
return 0;
}
}

class FixedDeposit : Deposit


{
public override double CalculateInterest()
{
return (Principal * Rate * Time) / 100;
}
}

class RecurringDeposit : Deposit


{
public override double CalculateInterest()
{
double n = 12;
return (Principal * Rate * Time * n) / 1200;
}
}

class InterestCalculator
{
static void Main()
{
FixedDeposit fd = new FixedDeposit();
Console.WriteLine("Enter principal for FD: ");
fd.Principal = Convert.ToDouble(Console.ReadLine());

Console.WriteLine("Enter rate for FD: ");


fd.Rate = Convert.ToDouble(Console.ReadLine());

Console.WriteLine("Enter time (in years) for FD: ");


fd.Time = Convert.ToInt32(Console.ReadLine());

Console.WriteLine($"Fixed Deposit Interest: {fd.CalculateInterest()}");

RecurringDeposit rd = new RecurringDeposit();


Console.WriteLine("Enter principal for RD: ");
rd.Principal = Convert.ToDouble(Console.ReadLine());

Console.WriteLine("Enter rate for RD: ");


rd.Rate = Convert.ToDouble(Console.ReadLine());

Console.WriteLine("Enter time (in years) for RD: ");


rd.Time = Convert.ToInt32(Console.ReadLine());

Console.WriteLine($"Recurring Deposit Interest: {rd.CalculateInterest()}");


}
}

Output –
Program No. 10
Objective – Write a program that displays the Gaming name on the screen in
Unity.
Source Code –
using UnityEngine;
using UnityEngine.UI;
public class GameNameDisplay : MonoBehaviour
{
public Text gameNameText;
void Start()
{
gameNameText.text = "My Awesome Game";
}
}
Output –
Program No. 11
Objective – Write a program which display Gaming object on the screen in
Unity.
Source Code –
using UnityEngine;
public class GameObjectDisplay : MonoBehaviour
{

public GameObject prefab;


void Start()
{
Instantiate(prefab, new Vector3(0, 0, 0), Quaternion.identity);
}
}

Output –
Program No. 12
Objective – Create a script that logs the position of an object in Unity's
console.

Source Code –
using UnityEngine;
public class LogPosition : MonoBehaviour
{

void Update()
{
Debug.Log("Position: " + transform.position);
}
}

Output –
Program No. 13
Objective – Create a script that logs the position of an object in Unity's
console.

Source Code –
using UnityEngine;

public class ChangeColorOnClick : MonoBehaviour{


private Renderer rend;
void Start(){
rend = GetComponent<Renderer>();
}
void OnMouseDown(){
rend.material.color = Random.ColorHSV();
}
}
Output –
Program No. 14
Objective – Create a script that moves a GameObject using user input (arrow
keys).

Source Code –
using UnityEngine;
public class MoveObject : MonoBehaviour
{
public float moveSpeed = 10f;
void Update()
{
float moveHorizontal = Input.GetAxis("Horizontal");
float moveVertical = Input.GetAxis("Vertical");

Vector3 movement = new Vector3(moveHorizontal, 0.0f,


moveVertical);
transform.Translate(movement * moveSpeed * Time.deltaTime,
Space.World);
}
}
Output –
Program No. 15
Objective – Calculate and display the distance between two GameObjects.

Source Code –
using UnityEngine;
public class DistanceCalculator : MonoBehaviour
{
public Transform object1;
public Transform object2;
void Update() {

float distance = Vector3.Distance(object1.position,


object2.position);
Debug.Log("Distance between objects: " + distance);
}
}

Output –
Program No. 16
Objective – Write a program that pawn enemies at random positions within a
defined area.

Source Code –
using UnityEngine;
public class EnemySpawner : MonoBehaviour{
public GameObject enemyPrefab;
public int numberOfEnemies = 10;
public float spawnAreaSize = 10f;
void Start(){
for (int i = 0; i < numberOfEnemies; i++){
Vector3 randomPosition = new Vector3(
Random.Range(-spawnAreaSize, spawnAreaSize),
0,
Random.Range(-spawnAreaSize, spawnAreaSize)
);
Instantiate(enemyPrefab, randomPosition,
Quaternion.identity);
}
}
}
Output –
Program No. 17
Objective – Write a program that implements a third-person character
controller with smooth camera follow.

Source Code –
using UnityEngine;
public class ThirdPersonController : MonoBehaviour{
public float moveSpeed = 5f;
public Transform cameraTransform;
public float smoothSpeed = 0.125f;
public Vector3 offset;
void Update(){
float horizontal = Input.GetAxis("Horizontal");
float vertical = Input.GetAxis("Vertical");
Vector3 move = new Vector3(horizontal, 0, vertical) *
moveSpeed * Time.deltaTime;
transform.Translate(move);
Vector3 desiredPosition = transform.position + offset;
Vector3 smoothedPosition =
Vector3.Lerp(cameraTransform.position, desiredPosition,
smoothSpeed);
cameraTransform.position = smoothedPosition;
}
}
Output –

You might also like