Wednesday, 4 December 2013

Introduction of C#

Exercise 1
Write a program to accept the values such as shape, color, area of the shape and print the values

Program:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Ch1_Ex1
{
    class Geometrical_Shapes
    {
        double Area;
        string Shape;
        string Color;
        public void GetInput()
        {
            Console.WriteLine("Enter the name of the shape:");
            Shape = Console.ReadLine();
            Console.WriteLine("Enter the color");
            Color = Console.ReadLine();
            Console.WriteLine("Enter the Area");
            Area = Convert.ToInt64(Console.ReadLine());
        }
        public void Display()
        {
            Console.WriteLine();
            Console.WriteLine("THIS IS WHAT YOU ENTERED: \n");
            Console.Write("Name of the Shape");
            Console.WriteLine(Shape);
            Console.Write("Color");
            Console.WriteLine(Color);
            Console.Write("Area");
            Console.WriteLine(Area);
        }
    }
    class Classy
    {
        static void Main(string[] args)
        {
            Geometrical_Shapes Small_rectangle = new Geometrical_Shapes();
            Small_rectangle.GetInput();
            Small_rectangle.Display();
            Console.ReadLine();
        }
    }
}




Exercise 2
Write a program to accept the details of Booking Request for video game parlour using methods.

Program:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Ch1_Ex2
{
    class Mclass
    {
        string Fname, Lname;
        int Noofplayers, Level;
        public void getdetails()
        {
            Console.Write("Enter your First Name ");
            Fname = Console.ReadLine();
            Console.Write("Enter your Last Name ");
            Lname = Console.ReadLine();
            Console.Write("Enter Number of Players ");
            Noofplayers = Convert.ToInt32(Console.ReadLine());
            Console.Write("Enter the Level ");
            Level = Convert.ToInt32(Console.ReadLine());
        }
        public void show()
        {
            Console.WriteLine("\nThe details you have entered are:");
            Console.WriteLine("First Name " + Fname);
            Console.WriteLine("Last Name " + Lname);
            Console.WriteLine("Number of players " + Noofplayers);
            Console.WriteLine("Level " + Level);
        }
        class output
        {
            public static void Main(string[] args)
            {
                Mclass gv = new Mclass();
                gv.getdetails();
                gv.show();
                Console.ReadLine();
            }
        }
    }
}


Exercise 3
Predict the output of the following program
Program
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Ch1_Ex3
{
    class Program
    {
        static void Main(string[] args)
        {
            string Answer = "Y";
            string Response_code = "66";
            int Counter = 60;
            Console.WriteLine(Answer);
            Console.WriteLine(Response_code);
            Console.WriteLine(Counter);
            Console.ReadLine();
        }
    }
}




Exercise 4
An automobile company has developed an application for maintaining the number of wheels in a wheels in a vehicle. Predict the output of the following code of the application.

Program:-
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Ch1_Ex4
{
    class Vehicle
    {
        public int Number_of_tyres;
    }
    class MyVehicle
    {
        public static void Main(string[] args)
        {
            Vehicle Motorcycle = new Vehicle();
            Vehicle Car = new Vehicle();
            Console.WriteLine("Enter the number of wheels in a car");
            Car.Number_of_tyres = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("Enter the number of wheels in a Motorcycle");
            Motorcycle.Number_of_tyres = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("\n");
            Console.WriteLine(Car.Number_of_tyres + ":is the number of wheels in a Car\n");
            Console.WriteLine(Motorcycle.Number_of_tyres + ":is the number of wheels in a Motorcycle\n");
            Console.ReadLine();
        }
    }
}
Output


Exercise 5
Identify the error and correct the code of TechnoSoft application

Program:-
using System;

namespace Ch1_Ex5
{
class Interchange
{
int Top_score,New_Score,Temp;
void Swap()
{
Top_Score=5;
New_Score=10;
Temp=Top_score;
New_score=Top_score;
Top_score=New_score;
}
void Display()
{
Console.WriteLine("The new value of top score is:");
Console.WriteLine(Top_score);
Console.WriteLine("The old value of top score is:");
Console.WriteLine(New_score);
}
public static void Main(string[] args)
{
Interchange I1;
I1.Swap();
I1.Display();
Console.ReadLine();
}
}
}



Correct Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Ch1_Ex5
{
    class Interchange
    {
        int Top_score, New_Score, Temp;
        void Swap()
        {
            Top_score = 5;
            New_Score = 10;
            Temp = Top_score;
            New_Score = Top_score;
            Top_score = New_Score;
        }
        void Display()
        {
            Console.WriteLine("The new value of top score is:");
            Console.WriteLine(Top_score);
            Console.WriteLine("The old value of top score is:");
            Console.WriteLine(New_Score);
        }
        public static void Main(string[] args)
        {
            Interchange I1  = new Interchange();
            I1.Swap();
            I1.Display();
            Console.ReadLine();
        }
    }
}




Exercise 6
As a part of the team that is developing software for library, you have been assigned the task of writing a program. The program should accept the following book details:

1. Book Number
2. Book Category(Fiction or Nonfiction)
3. Author
4. Number of copies available

Write a C# program to accept and display the preceding book details.

Program:-
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Ch1_Ex6
{
    class Book
    {
        string BookCat, FirstName, LastName;
        int IsbnNo, NoofCopies;

        public void Display()
        {
            Console.WriteLine("\nThe following are the details of the books");
            Console.WriteLine("Category of Books:" + BookCat);
            Console.WriteLine("Name of the Author:" + FirstName + LastName);
            Console.WriteLine("ISBN Number:" + IsbnNo);
            Console.WriteLine("No.of Copies:" + NoofCopies);
        }
        public void get()
        {
            Console.WriteLine("Please enter the details of the books");
            Console.Write("Enter the category Fiction/Non Fiction: ");
            BookCat = Console.ReadLine();
            Console.Write("First Name of the Author: ");
            FirstName = Console.ReadLine();
            Console.Write("Last Name of the Author: ");
            LastName = Console.ReadLine();
            Console.Write("ISBN Number: ");
            IsbnNo = Convert.ToInt32(Console.ReadLine());
            Console.Write("Number of copies: ");
            NoofCopies = Convert.ToInt32(Console.ReadLine());
        }
    }
    class MBooks
    {
        public static void Main(string[] args)
        {
            Book bs = new Book();
            bs.get();
            bs.Display();
            Console.ReadLine();
        }
    }
}
Output




Exercise 7
Write a program to accept the value of distance in Kilometer and display it in meters.

Program:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Convertor_km
{
    class Program
    {
        static void Main(string[] args)
        {
            int ip_km, ip_mts;
            Console.WriteLine("Converter KM into METERS");
            Console.WriteLine("Enter the value in Kilometer");
            ip_km = Convert.ToInt32(Console.ReadLine());
            ip_mts = ip_km * 1000;
            Console.WriteLine("The " + ip_km + "Kilometer is equal to " + ip_mts + " meters");
            Console.Read();
        }
    }
}
Output Screen




Exercise 8
Write a Program that display "Congratulation! You have cleared this Level. Entering Level 2 ..." in red coloras output shown below.

Program:-
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Ch1_Ex8
{
    class Game
    {
        public static void Main(string[] args)
        {
            Console.ForegroundColor = ConsoleColor.Red;
            Console.WriteLine("\n\n\n\n\n");
            Console.WriteLine("\t\tCongratulation! You have cleared this Level.");
            Console.WriteLine("\n\n");
            Console.WriteLine("\t\t\t\tEntering Level 2 ...");
            Console.ReadLine();
        }
    }
}


Output of the program



No comments:

Post a Comment