C# File
C# File
, India
Lab Manual
Unity with C#
R1UC506C (PR)
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 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{
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;
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 discount = 0;
if(mrp>600){
discount = discountRate*mrp;
}
class InterestCalculator
{
static void Main()
{
FixedDeposit fd = new FixedDeposit();
Console.WriteLine("Enter principal for FD: ");
fd.Principal = Convert.ToDouble(Console.ReadLine());
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
{
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;
Source Code –
using UnityEngine;
public class MoveObject : MonoBehaviour
{
public float moveSpeed = 10f;
void Update()
{
float moveHorizontal = Input.GetAxis("Horizontal");
float moveVertical = Input.GetAxis("Vertical");
Source Code –
using UnityEngine;
public class DistanceCalculator : MonoBehaviour
{
public Transform object1;
public Transform object2;
void Update() {
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 –